Skip to content

Commit b4c84a1

Browse files
committed
cmd/go: fix corner case missed rebuild of binary
If the only thing changing in the binary is the embedded main.a action ID, go install was declining to install the binary, but go list could see that the binary needed reinstalling (was stale). Fixes #22531. Change-Id: I4a53b0ebd4c34aad907bab7da571fada545f3c6f Reviewed-on: https://go-review.googlesource.com/76014 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: David Crawshaw <[email protected]>
1 parent e247f46 commit b4c84a1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/cmd/go/go_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,7 +4740,34 @@ func TestBuildCache(t *testing.T) {
47404740

47414741
tg.run("build", "-o", os.DevNull, "-x", "complex")
47424742
tg.grepStderr(`[\\/]link|gccgo`, "did not run linker")
4743+
}
47434744

4745+
func TestIssue22531(t *testing.T) {
4746+
if strings.Contains(os.Getenv("GODEBUG"), "gocacheverify") {
4747+
t.Skip("GODEBUG gocacheverify")
4748+
}
4749+
tg := testgo(t)
4750+
defer tg.cleanup()
4751+
tg.parallel()
4752+
tg.makeTempdir()
4753+
tg.setenv("GOPATH", tg.tempdir)
4754+
tg.setenv("GOCACHE", filepath.Join(tg.tempdir, "cache"))
4755+
tg.tempFile("src/m/main.go", "package main /* c1 */; func main() {}\n")
4756+
tg.run("install", "-x", "m")
4757+
tg.run("list", "-f", "{{.Stale}}", "m")
4758+
tg.grepStdout("false", "reported m as stale after install")
4759+
tg.run("tool", "buildid", filepath.Join(tg.tempdir, "bin/m"+exeSuffix))
4760+
4761+
// The link action ID did not include the full main build ID,
4762+
// even though the full main build ID is written into the
4763+
// eventual binary. That caused the following install to
4764+
// be a no-op, thinking the gofmt binary was up-to-date,
4765+
// even though .Stale could see it was not.
4766+
tg.tempFile("src/m/main.go", "package main /* c2 */; func main() {}\n")
4767+
tg.run("install", "-x", "m")
4768+
tg.run("list", "-f", "{{.Stale}}", "m")
4769+
tg.grepStdout("false", "reported m as stale after reinstall")
4770+
tg.run("tool", "buildid", filepath.Join(tg.tempdir, "bin/m"+exeSuffix))
47444771
}
47454772

47464773
func TestTestCache(t *testing.T) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,11 @@ func (b *Builder) linkActionID(a *Action) cache.ActionID {
690690
}
691691
fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(buildID))
692692
}
693+
// Because we put package main's full action ID into the binary's build ID,
694+
// we must also put the full action ID into the binary's action ID hash.
695+
if p1.Name == "main" {
696+
fmt.Fprintf(h, "packagemain %s\n", a1.buildID)
697+
}
693698
if p1.Shlib != "" {
694699
fmt.Fprintf(h, "pakageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
695700
}

0 commit comments

Comments
 (0)