Skip to content

Commit

Permalink
Using srcModTime instead of time.Now
Browse files Browse the repository at this point in the history
  • Loading branch information
grantnelson-wf committed Dec 12, 2024
1 parent 96b85d9 commit 11bf21d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
fmt.Println(pkg.ImportPath)
}

s.buildCache.StoreArchive(archive, time.Now())
s.buildCache.StoreArchive(archive, pkg.SrcModTime)
s.UpToDateArchives[pkg.ImportPath] = archive

return archive, nil
Expand Down
3 changes: 3 additions & 0 deletions build/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (bc BuildCache) String() string {

// StoreArchive compiled archive in the cache. Any error inside this method
// will cause the cache not to be persisted.
//
// The passed in buildTime is used to determine if the archive is out-of-date when reloaded.
// Typically it should be set to the srcModTime or time.Now().
func (bc *BuildCache) StoreArchive(a *compiler.Archive, buildTime time.Time) {
if bc == nil {
return // Caching is disabled.
Expand Down
2 changes: 1 addition & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func ReadArchive(importPath string, r io.Reader, srcModTime time.Time, imports m
// WriteArchive writes compiled package archive on disk for later reuse.
//
// The passed in buildTime is used to determine if the archive is out-of-date.
// It should be set to time.Now() typically but it exposed for testing purposes.
// Typically it should be set to the srcModTime or time.Now() but it is exposed for testing purposes.
func WriteArchive(a *Archive, buildTime time.Time, w io.Writer) error {
exportData := new(bytes.Buffer)
if a.Package != nil {
Expand Down
20 changes: 10 additions & 10 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func TestArchiveSelectionAfterSerialization(t *testing.T) {
origJS := renderPackage(t, origArchives[rootPath], false)
readJS := renderPackage(t, readArchives[rootPath], false)

if diff := cmp.Diff(string(origJS), string(readJS)); diff != "" {
if diff := cmp.Diff(origJS, readJS); diff != "" {
t.Errorf("the reloaded files produce different JS:\n%s", diff)
}
}
Expand All @@ -444,12 +444,12 @@ func compareOrder(t *testing.T, sourceFiles []srctesting.Source, minify bool) {

outputReversed := compile(t, sourceFiles, minify)

if diff := cmp.Diff(string(outputNormal), string(outputReversed)); diff != "" {
if diff := cmp.Diff(outputNormal, outputReversed); diff != "" {
t.Errorf("files in different order produce different JS:\n%s", diff)
}
}

func compile(t *testing.T, sourceFiles []srctesting.Source, minify bool) []byte {
func compile(t *testing.T, sourceFiles []srctesting.Source, minify bool) string {
t.Helper()
rootPkg := srctesting.ParseSources(t, sourceFiles, nil)
archives := compileProject(t, rootPkg, minify)
Expand All @@ -460,11 +460,7 @@ func compile(t *testing.T, sourceFiles []srctesting.Source, minify bool) []byte
t.Fatalf(`root package not found in archives: %s`, path)
}

b := renderPackage(t, a, minify)
if len(b) == 0 {
t.Fatal(`compile had no output`)
}
return b
return renderPackage(t, a, minify)
}

// compileProject compiles the given root package and all packages imported by the root.
Expand Down Expand Up @@ -563,7 +559,7 @@ func reloadCompiledProject(t *testing.T, archives map[string]*Archive, rootPkgPa
return reloadCache
}

func renderPackage(t *testing.T, archive *Archive, minify bool) []byte {
func renderPackage(t *testing.T, archive *Archive, minify bool) string {
t.Helper()

sel := &dce.Selector[*Decl]{}
Expand All @@ -578,7 +574,11 @@ func renderPackage(t *testing.T, archive *Archive, minify bool) []byte {
t.Fatal(err)
}

return buf.Bytes()
b := buf.String()
if len(b) == 0 {
t.Fatal(`render package had no output`)
}
return b
}

type selectionTester struct {
Expand Down

0 comments on commit 11bf21d

Please sign in to comment.