Skip to content

Commit f047422

Browse files
rscbradfitz
authored andcommitted
cmd/go: fix -covermode=atomic use of sync/atomic in -coverpkg matches
If we're using -covermode=atomic with -coverpkg, to add coverage to more than just the package being tested, then we need to make sure to make sync/atomic available to the compiler for every package being recompiled for coverage. Fixes #22728. Change-Id: I27f88f6a62e37d4a7455554cd03c8ca2b21f81a4 Reviewed-on: https://go-review.googlesource.com/81497 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 66fcf45 commit f047422

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

src/cmd/go/go_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,14 @@ func TestCoverageUsesAtomicModeForRace(t *testing.T) {
24162416
checkCoverage(tg, data)
24172417
}
24182418

2419+
func TestCoverageSyncAtomicImport(t *testing.T) {
2420+
tg := testgo(t)
2421+
defer tg.cleanup()
2422+
tg.parallel()
2423+
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2424+
tg.run("test", "-short", "-cover", "-covermode=atomic", "-coverpkg=coverdep/p1", "coverdep")
2425+
}
2426+
24192427
func TestCoverageImportMainLoop(t *testing.T) {
24202428
tg := testgo(t)
24212429
defer tg.cleanup()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,9 @@ func runTest(cmd *base.Command, args []string) {
670670
coverFiles = append(coverFiles, p.CgoFiles...)
671671
coverFiles = append(coverFiles, p.TestGoFiles...)
672672
p.Internal.CoverVars = declareCoverVars(p.ImportPath, coverFiles...)
673+
if testCover && testCoverMode == "atomic" {
674+
ensureImport(p, "sync/atomic")
675+
}
673676
}
674677
}
675678

src/cmd/go/testdata/src/coverdep/p.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package p
2+
3+
import _ "coverdep/p1"
4+
5+
func F() {
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package p1
2+
3+
import _ "errors"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package p
2+
3+
import "testing"
4+
5+
func Test(t *testing.T) {
6+
F()
7+
}

0 commit comments

Comments
 (0)