]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: if -race, don't run coverage on runtime packages
authorIan Lance Taylor <iant@golang.org>
Sat, 17 Feb 2018 03:08:59 +0000 (19:08 -0800)
committerIan Lance Taylor <iant@golang.org>
Tue, 27 Mar 2018 22:41:14 +0000 (22:41 +0000)
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
src/cmd/go/go_test.go
src/cmd/go/internal/test/test.go

index 74254230a9860cbf9cab6dcd5dbf4437c958598d..f8975edda53f3931a903aec5ab9325e57f58d2b8 100644 (file)
@@ -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()
index 9a53a197313a2a1c8f7926dbcb652f0b13766d7c..42bff352c508064fdcca070f79f4b45d4606ca92 100644 (file)
@@ -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)
                        }