@@ -418,11 +418,19 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string,
418
418
a .buildID = actionID + buildIDSeparator + mainpkg .buildID + buildIDSeparator + contentID
419
419
}
420
420
421
- // Check to see if target exists and matches the expected action ID.
422
- // If so, it's up to date and we can reuse it instead of rebuilding it.
423
- var buildID string
424
- if target != "" && ! cfg .BuildA {
425
- buildID , _ = buildid .ReadFile (target )
421
+ // If user requested -a, we force a rebuild, so don't use the cache.
422
+ if cfg .BuildA {
423
+ if p := a .Package ; p != nil && ! p .Stale {
424
+ p .Stale = true
425
+ p .StaleReason = "build -a flag in use"
426
+ }
427
+ // Begin saving output for later writing to cache.
428
+ a .output = []byte {}
429
+ return false
430
+ }
431
+
432
+ if target != "" {
433
+ buildID , _ := buildid .ReadFile (target )
426
434
if strings .HasPrefix (buildID , actionID + buildIDSeparator ) {
427
435
a .buildID = buildID
428
436
if a .json != nil {
@@ -433,18 +441,13 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string,
433
441
a .Target = "DO NOT USE - " + a .Mode
434
442
return true
435
443
}
436
- }
437
-
438
- // Special case for building a main package: if the only thing we
439
- // want the package for is to link a binary, and the binary is
440
- // already up-to-date, then to avoid a rebuild, report the package
441
- // as up-to-date as well. See "Build IDs" comment above.
442
- // TODO(rsc): Rewrite this code to use a TryCache func on the link action.
443
- if target != "" && ! cfg .BuildA && ! b .NeedExport && a .Mode == "build" && len (a .triggers ) == 1 && a .triggers [0 ].Mode == "link" {
444
- buildID , err := buildid .ReadFile (target )
445
- if err == nil {
446
- id := strings .Split (buildID , buildIDSeparator )
447
- if len (id ) == 4 && id [1 ] == actionID {
444
+ // Special case for building a main package: if the only thing we
445
+ // want the package for is to link a binary, and the binary is
446
+ // already up-to-date, then to avoid a rebuild, report the package
447
+ // as up-to-date as well. See "Build IDs" comment above.
448
+ // TODO(rsc): Rewrite this code to use a TryCache func on the link action.
449
+ if ! b .NeedExport && a .Mode == "build" && len (a .triggers ) == 1 && a .triggers [0 ].Mode == "link" {
450
+ if id := strings .Split (buildID , buildIDSeparator ); len (id ) == 4 && id [1 ] == actionID {
448
451
// Temporarily assume a.buildID is the package build ID
449
452
// stored in the installed binary, and see if that makes
450
453
// the upcoming link action ID a match. If so, report that
@@ -488,7 +491,7 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string,
488
491
// then to avoid the link step, report the link as up-to-date.
489
492
// We avoid the nested build ID problem in the previous special case
490
493
// by recording the test results in the cache under the action ID half.
491
- if ! cfg . BuildA && len (a .triggers ) == 1 && a .triggers [0 ].TryCache != nil && a .triggers [0 ].TryCache (b , a .triggers [0 ]) {
494
+ if len (a .triggers ) == 1 && a .triggers [0 ].TryCache != nil && a .triggers [0 ].TryCache (b , a .triggers [0 ]) {
492
495
// Best effort attempt to display output from the compile and link steps.
493
496
// If it doesn't work, it doesn't work: reusing the test result is more
494
497
// important than reprinting diagnostic information.
@@ -505,63 +508,51 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string,
505
508
return true
506
509
}
507
510
508
- if b .IsCmdList {
509
- // Invoked during go list to compute and record staleness.
510
- if p := a .Package ; p != nil && ! p .Stale {
511
- p .Stale = true
512
- if cfg .BuildA {
513
- p .StaleReason = "build -a flag in use"
514
- } else {
515
- p .StaleReason = "build ID mismatch"
516
- for _ , p1 := range p .Internal .Imports {
517
- if p1 .Stale && p1 .StaleReason != "" {
518
- if strings .HasPrefix (p1 .StaleReason , "stale dependency: " ) {
519
- p .StaleReason = p1 .StaleReason
520
- break
521
- }
522
- if strings .HasPrefix (p .StaleReason , "build ID mismatch" ) {
523
- p .StaleReason = "stale dependency: " + p1 .ImportPath
524
- }
525
- }
511
+ // Check to see if the action output is cached.
512
+ if c := cache .Default (); c != nil {
513
+ if file , _ , err := c .GetFile (actionHash ); err == nil {
514
+ if buildID , err := buildid .ReadFile (file ); err == nil {
515
+ if printOutput {
516
+ showStdout (b , c , a .actionID , "stdout" )
517
+ }
518
+ a .built = file
519
+ a .Target = "DO NOT USE - using cache"
520
+ a .buildID = buildID
521
+ if a .json != nil {
522
+ a .json .BuildID = a .buildID
526
523
}
524
+ if p := a .Package ; p != nil && target != "" {
525
+ p .Stale = true
526
+ // Clearer than explaining that something else is stale.
527
+ p .StaleReason = "not installed but available in build cache"
528
+ }
529
+ return true
527
530
}
528
531
}
529
-
530
- // Fall through to update a.buildID from the build artifact cache,
531
- // which will affect the computation of buildIDs for targets
532
- // higher up in the dependency graph.
533
532
}
534
533
535
- // Check the build artifact cache.
536
- // We treat hits in this cache as being "stale" for the purposes of go list
537
- // (in effect, "stale" means whether p.Target is up-to-date),
538
- // but we're still happy to use results from the build artifact cache.
539
- if c := cache .Default (); c != nil {
540
- if ! cfg .BuildA {
541
- if file , _ , err := c .GetFile (actionHash ); err == nil {
542
- if buildID , err := buildid .ReadFile (file ); err == nil {
543
- if printOutput {
544
- showStdout (b , c , a .actionID , "stdout" )
534
+ // If we've reached this point, we can't use the cache for the action.
535
+ if p := a .Package ; p != nil && ! p .Stale {
536
+ p .Stale = true
537
+ p .StaleReason = "build ID mismatch"
538
+ if b .IsCmdList {
539
+ // Since we may end up printing StaleReason, include more detail.
540
+ for _ , p1 := range p .Internal .Imports {
541
+ if p1 .Stale && p1 .StaleReason != "" {
542
+ if strings .HasPrefix (p1 .StaleReason , "stale dependency: " ) {
543
+ p .StaleReason = p1 .StaleReason
544
+ break
545
545
}
546
- a .built = file
547
- a .Target = "DO NOT USE - using cache"
548
- a .buildID = buildID
549
- if a .json != nil {
550
- a .json .BuildID = a .buildID
546
+ if strings .HasPrefix (p .StaleReason , "build ID mismatch" ) {
547
+ p .StaleReason = "stale dependency: " + p1 .ImportPath
551
548
}
552
- if p := a .Package ; p != nil {
553
- // Clearer than explaining that something else is stale.
554
- p .StaleReason = "not installed but available in build cache"
555
- }
556
- return true
557
549
}
558
550
}
559
551
}
560
-
561
- // Begin saving output for later writing to cache.
562
- a .output = []byte {}
563
552
}
564
553
554
+ // Begin saving output for later writing to cache.
555
+ a .output = []byte {}
565
556
return false
566
557
}
567
558
0 commit comments