Skip to content

Commit 11bf21d

Browse files
Using srcModTime instead of time.Now
1 parent 96b85d9 commit 11bf21d

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

build/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
10351035
fmt.Println(pkg.ImportPath)
10361036
}
10371037

1038-
s.buildCache.StoreArchive(archive, time.Now())
1038+
s.buildCache.StoreArchive(archive, pkg.SrcModTime)
10391039
s.UpToDateArchives[pkg.ImportPath] = archive
10401040

10411041
return archive, nil

build/cache/cache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ func (bc BuildCache) String() string {
9292

9393
// StoreArchive compiled archive in the cache. Any error inside this method
9494
// will cause the cache not to be persisted.
95+
//
96+
// The passed in buildTime is used to determine if the archive is out-of-date when reloaded.
97+
// Typically it should be set to the srcModTime or time.Now().
9598
func (bc *BuildCache) StoreArchive(a *compiler.Archive, buildTime time.Time) {
9699
if bc == nil {
97100
return // Caching is disabled.

compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func ReadArchive(importPath string, r io.Reader, srcModTime time.Time, imports m
317317
// WriteArchive writes compiled package archive on disk for later reuse.
318318
//
319319
// The passed in buildTime is used to determine if the archive is out-of-date.
320-
// It should be set to time.Now() typically but it exposed for testing purposes.
320+
// Typically it should be set to the srcModTime or time.Now() but it is exposed for testing purposes.
321321
func WriteArchive(a *Archive, buildTime time.Time, w io.Writer) error {
322322
exportData := new(bytes.Buffer)
323323
if a.Package != nil {

compiler/compiler_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func TestArchiveSelectionAfterSerialization(t *testing.T) {
428428
origJS := renderPackage(t, origArchives[rootPath], false)
429429
readJS := renderPackage(t, readArchives[rootPath], false)
430430

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

445445
outputReversed := compile(t, sourceFiles, minify)
446446

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

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

463-
b := renderPackage(t, a, minify)
464-
if len(b) == 0 {
465-
t.Fatal(`compile had no output`)
466-
}
467-
return b
463+
return renderPackage(t, a, minify)
468464
}
469465

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

566-
func renderPackage(t *testing.T, archive *Archive, minify bool) []byte {
562+
func renderPackage(t *testing.T, archive *Archive, minify bool) string {
567563
t.Helper()
568564

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

581-
return buf.Bytes()
577+
b := buf.String()
578+
if len(b) == 0 {
579+
t.Fatal(`render package had no output`)
580+
}
581+
return b
582582
}
583583

584584
type selectionTester struct {

0 commit comments

Comments
 (0)