diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index d09b190781..ea31e44779 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -4120,3 +4120,21 @@ func TestCgoFlagContainsSpace(t *testing.T) { tg.cd(tg.path("src/cgo")) tg.run("run", "main.go") } + +// Issue #20435. +func TestGoTestRaceCoverModeFailures(t *testing.T) { + if !canRace { + t.Skip("skipping because race detector not supported") + } + + tg := testgo(t) + tg.parallel() + defer tg.cleanup() + tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata")) + + tg.run("test", "testrace") + + tg.runFail("test", "-race", "-covermode=set", "testrace") + tg.grepStderr(`-covermode must be "atomic", not "set", when -race is enabled`, "-race -covermode=set was allowed") + tg.grepBothNot("PASS", "something passed") +} diff --git a/src/cmd/go/internal/test/testflag.go b/src/cmd/go/internal/test/testflag.go index fb76a34a77..bff8656a4c 100644 --- a/src/cmd/go/internal/test/testflag.go +++ b/src/cmd/go/internal/test/testflag.go @@ -193,6 +193,10 @@ func testFlags(args []string) (packageNames, passToTest []string) { } } + if cfg.BuildRace && testCoverMode != "atomic" { + base.Fatalf(`-covermode must be "atomic", not %q, when -race is enabled`, testCoverMode) + } + // Tell the test what directory we're running in, so it can write the profiles there. if testProfile && outputDir == "" { dir, err := os.Getwd()