Skip to content

Commit

Permalink
cmd/go: if -race, don't run coverage on runtime packages
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ianlancetaylor committed Mar 27, 2018
1 parent bd18c09 commit 976a852
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5771,6 +5771,21 @@ func TestAtomicCoverpkgAll(t *testing.T) {
}
}

// Issue 23882.
func TestCoverpkgAllRuntime(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
tg.parallel()

tg.tempFile("src/x/x.go", `package x; import _ "runtime"; func F() {}`)
tg.tempFile("src/x/x_test.go", `package x; import "testing"; func TestF(t *testing.T) { F() }`)
tg.setenv("GOPATH", tg.path("."))
tg.run("test", "-coverpkg=all", "x")
if canRace {
tg.run("test", "-coverpkg=all", "-race", "x")
}
}

func TestBadCommandLines(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
Expand Down
8 changes: 8 additions & 0 deletions src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,14 @@ func runTest(cmd *base.Command, args []string) {
continue
}

// If using the race detector, silently ignore
// attempts to run coverage on the runtime
// packages. It will cause the race detector
// to be invoked before it has been initialized.
if cfg.BuildRace && p.Standard && (p.ImportPath == "runtime" || strings.HasPrefix(p.ImportPath, "runtime/internal")) {
continue
}

if haveMatch {
testCoverPkgs = append(testCoverPkgs, p)
}
Expand Down

0 comments on commit 976a852

Please sign in to comment.