Skip to content

Commit 232b2e3

Browse files
committed
cmd/go: fix reuse of cached objects during cover
The cover variable indices could vary from build to build, but they were not included in the build ID hash, so that reusing the previously built package was not safe. Make the indices no longer vary from build to build, so that caching is safe. Fixes #22652. Change-Id: Ie26d73c648aadd285f97e0bf39619cabc3da54f2 Reviewed-on: https://go-review.googlesource.com/81515 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 76dc4b1 commit 232b2e3

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/cmd/go/go_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,6 +4908,22 @@ func TestCacheOutput(t *testing.T) {
49084908
}
49094909
}
49104910

4911+
func TestCacheCoverage(t *testing.T) {
4912+
if strings.Contains(os.Getenv("GODEBUG"), "gocacheverify") {
4913+
t.Skip("GODEBUG gocacheverify")
4914+
}
4915+
4916+
tg := testgo(t)
4917+
defer tg.cleanup()
4918+
tg.parallel()
4919+
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
4920+
tg.makeTempdir()
4921+
4922+
tg.setenv("GOCACHE", filepath.Join(tg.tempdir, "c1"))
4923+
tg.run("test", "-cover", "strings")
4924+
tg.run("test", "-cover", "math", "strings")
4925+
}
4926+
49114927
func TestIssue22588(t *testing.T) {
49124928
// Don't get confused by stderr coming from tools.
49134929
tg := testgo(t)

src/cmd/go/internal/test/test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,6 @@ func recompileForTest(pmain, preal, ptest *load.Package) {
11731173
}
11741174
}
11751175

1176-
var coverIndex = 0
1177-
11781176
// isTestFile reports whether the source file is a set of tests and should therefore
11791177
// be excluded from coverage analysis.
11801178
func isTestFile(file string) bool {
@@ -1186,6 +1184,7 @@ func isTestFile(file string) bool {
11861184
// to the files, to be used when annotating the files.
11871185
func declareCoverVars(importPath string, files ...string) map[string]*load.CoverVar {
11881186
coverVars := make(map[string]*load.CoverVar)
1187+
coverIndex := 0
11891188
for _, file := range files {
11901189
if isTestFile(file) {
11911190
continue

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,10 @@ func (b *Builder) updateBuildID(a *Action, target string, rewrite bool) error {
474474
if a.output == nil {
475475
panic("internal error: a.output not set")
476476
}
477-
c.Put(a.actionID, r)
477+
outputID, _, err := c.Put(a.actionID, r)
478+
if err == nil && cfg.BuildX {
479+
b.Showcmd("", "%s # internal", joinUnambiguously(str.StringList("cp", target, c.OutputFile(outputID))))
480+
}
478481
c.PutBytes(cache.Subkey(a.actionID, "stdout"), a.output)
479482
r.Close()
480483
}

0 commit comments

Comments
 (0)