Skip to content

Commit 11d7a5a

Browse files
rscandybons
authored andcommitted
[release-branch.go1.10] cmd/go: fix go list .Stale computation
If X depends on Y and X was installed but Y is only present in the cache (as happens when you "go install X") then we should report X as up-to-date, not as stale. This applies whether X is a package or a main binary. Fixes #25026 Fixes #25032 Change-Id: I26a0b375b1f7f7ac909cc0db68e92f4e04529208 Reviewed-on: https://go-review.googlesource.com/107957 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> (cherry picked from commit 9e0e698) Reviewed-on: https://go-review.googlesource.com/110076 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent abccc7a commit 11d7a5a

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

misc/cgo/testshared/shared_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ func TestRebuilding(t *testing.T) {
790790
// If the .a file is newer than the .so, the .so is rebuilt (but not the .a)
791791
t.Run("newarchive", func(t *testing.T) {
792792
resetFileStamps()
793+
AssertNotRebuilt(t, "new .a file before build", filepath.Join(gopathInstallDir, "depBase.a"))
793794
goCmd(t, "list", "-linkshared", "-f={{.ImportPath}} {{.Stale}} {{.StaleReason}} {{.Target}}", "depBase")
794795
AssertNotRebuilt(t, "new .a file before build", filepath.Join(gopathInstallDir, "depBase.a"))
795796
cleanup := touch(t, filepath.Join(gopathInstallDir, "depBase.a"))

src/cmd/go/go_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5074,6 +5074,28 @@ func TestCacheOutput(t *testing.T) {
50745074
}
50755075
}
50765076

5077+
func TestCacheListStale(t *testing.T) {
5078+
tooSlow(t)
5079+
if strings.Contains(os.Getenv("GODEBUG"), "gocacheverify") {
5080+
t.Skip("GODEBUG gocacheverify")
5081+
}
5082+
tg := testgo(t)
5083+
defer tg.cleanup()
5084+
tg.parallel()
5085+
tg.makeTempdir()
5086+
tg.setenv("GOCACHE", tg.path("cache"))
5087+
tg.tempFile("gopath/src/p/p.go", "package p; import _ \"q\"; func F(){}\n")
5088+
tg.tempFile("gopath/src/q/q.go", "package q; func F(){}\n")
5089+
tg.tempFile("gopath/src/m/m.go", "package main; import _ \"q\"; func main(){}\n")
5090+
5091+
tg.setenv("GOPATH", tg.path("gopath"))
5092+
tg.run("install", "p", "m")
5093+
tg.run("list", "-f={{.ImportPath}} {{.Stale}}", "m", "q", "p")
5094+
tg.grepStdout("^m false", "m should not be stale")
5095+
tg.grepStdout("^q true", "q should be stale")
5096+
tg.grepStdout("^p false", "p should not be stale")
5097+
}
5098+
50775099
func TestCacheCoverage(t *testing.T) {
50785100
tooSlow(t)
50795101

src/cmd/go/internal/work/buildid.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,7 @@ func (b *Builder) useCache(a *Action, p *load.Package, actionHash cache.ActionID
397397
// If so, it's up to date and we can reuse it instead of rebuilding it.
398398
var buildID string
399399
if target != "" && !cfg.BuildA {
400-
var err error
401-
buildID, err = buildid.ReadFile(target)
402-
if err != nil && b.ComputeStaleOnly {
403-
if p != nil && !p.Stale {
404-
p.Stale = true
405-
p.StaleReason = "target missing"
406-
}
407-
return true
408-
}
400+
buildID, _ = buildid.ReadFile(target)
409401
if strings.HasPrefix(buildID, actionID+buildIDSeparator) {
410402
a.buildID = buildID
411403
a.built = target
@@ -482,7 +474,10 @@ func (b *Builder) useCache(a *Action, p *load.Package, actionHash cache.ActionID
482474
}
483475
}
484476
}
485-
return true
477+
478+
// Fall through to update a.buildID from the build artifact cache,
479+
// which will affect the computation of buildIDs for targets
480+
// higher up in the dependency graph.
486481
}
487482

488483
// Check the build artifact cache.
@@ -510,6 +505,10 @@ func (b *Builder) useCache(a *Action, p *load.Package, actionHash cache.ActionID
510505
a.built = file
511506
a.Target = "DO NOT USE - using cache"
512507
a.buildID = buildID
508+
if p := a.Package; p != nil {
509+
// Clearer than explaining that something else is stale.
510+
p.StaleReason = "not installed but available in build cache"
511+
}
513512
return true
514513
}
515514
}
@@ -520,6 +519,10 @@ func (b *Builder) useCache(a *Action, p *load.Package, actionHash cache.ActionID
520519
a.output = []byte{}
521520
}
522521

522+
if b.ComputeStaleOnly {
523+
return true
524+
}
525+
523526
return false
524527
}
525528

src/cmd/go/internal/work/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ func BuildInstallFunc(b *Builder, a *Action) (err error) {
10931093
// We want to hide that awful detail as much as possible, so don't
10941094
// advertise it by touching the mtimes (usually the libraries are up
10951095
// to date).
1096-
if !a.buggyInstall {
1096+
if !a.buggyInstall && !b.ComputeStaleOnly {
10971097
now := time.Now()
10981098
os.Chtimes(a.Target, now, now)
10991099
}

0 commit comments

Comments
 (0)