Skip to content

Commit 976a852

Browse files
cmd/go: if -race, don't run coverage on runtime packages
Don't compile the runtime packages with coverage when using the race detector. The user can, perhaps accidentally, request coverage for the runtime by using -coverpkg=all. If using the race detector, the runtime package coverage will call into the race detector before it has been initialized. This will cause the program to crash mysteriously on startup. Fixes #23882 Change-Id: I9a63867a9138797d8b8afb0856ae21079accdb27 Reviewed-on: https://go-review.googlesource.com/94898 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Andrew Bonventre <[email protected]>
1 parent bd18c09 commit 976a852

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/cmd/go/go_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5771,6 +5771,21 @@ func TestAtomicCoverpkgAll(t *testing.T) {
57715771
}
57725772
}
57735773

5774+
// Issue 23882.
5775+
func TestCoverpkgAllRuntime(t *testing.T) {
5776+
tg := testgo(t)
5777+
defer tg.cleanup()
5778+
tg.parallel()
5779+
5780+
tg.tempFile("src/x/x.go", `package x; import _ "runtime"; func F() {}`)
5781+
tg.tempFile("src/x/x_test.go", `package x; import "testing"; func TestF(t *testing.T) { F() }`)
5782+
tg.setenv("GOPATH", tg.path("."))
5783+
tg.run("test", "-coverpkg=all", "x")
5784+
if canRace {
5785+
tg.run("test", "-coverpkg=all", "-race", "x")
5786+
}
5787+
}
5788+
57745789
func TestBadCommandLines(t *testing.T) {
57755790
tg := testgo(t)
57765791
defer tg.cleanup()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ func runTest(cmd *base.Command, args []string) {
674674
continue
675675
}
676676

677+
// If using the race detector, silently ignore
678+
// attempts to run coverage on the runtime
679+
// packages. It will cause the race detector
680+
// to be invoked before it has been initialized.
681+
if cfg.BuildRace && p.Standard && (p.ImportPath == "runtime" || strings.HasPrefix(p.ImportPath, "runtime/internal")) {
682+
continue
683+
}
684+
677685
if haveMatch {
678686
testCoverPkgs = append(testCoverPkgs, p)
679687
}

0 commit comments

Comments
 (0)