Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting coverage mode to atomic when race detection is enabled #2502

Merged
merged 5 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ def emit_compilepkg(
if cover and go.coverdata:
inputs.append(go.coverdata.data.file)
args.add("-arc", _archive(go.coverdata))
args.add("-cover_mode", "set")
if go.mode.race:
args.add("-cover_mode", "atomic")
else:
args.add("-cover_mode", "set")
args.add_all(cover, before_each = "-cover")
args.add_all(archives, before_each = "-arc", map_each = _archive)
if importpath:
Expand Down
6 changes: 5 additions & 1 deletion go/private/actions/cover.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def emit_cover(go, source):
args.add("-var", cover_var)
args.add("-src", src)
args.add("-srcname", srcname)
args.add("-mode", "set")

if go.mode.race:
args.add("-mode", "atomic")
else:
args.add("-mode", "set")
go.actions.run(
inputs = [src] + go.sdk.tools,
outputs = [out],
Expand Down
3 changes: 3 additions & 0 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func compileArchive(
imports["unsafe"] = nil
}
if coverMode != "" {
if coverMode == "atomic" {
jayconrod marked this conversation as resolved.
Show resolved Hide resolved
imports["sync/atomic"] = nil
}
const coverdataPath = "github.com/bazelbuild/rules_go/go/tools/coverdata"
var coverdata *archive
for i := range deps {
Expand Down
46 changes: 46 additions & 0 deletions tests/core/race/race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ go_binary(
race = "on",
)

go_library(
name = "coverrace",
srcs = ["coverrace.go"],
importpath = "example.com/coverrace",
)

go_test(
name = "coverrace_test",
srcs = ["coverrace_test.go"],
embed = [":coverrace"],
race = "on",
)
-- race_off.go --
// +build !race

Expand Down Expand Up @@ -150,6 +162,36 @@ func TestRace(t *testing.T) {
package main

func main() {}

-- coverrace.go --
package coverrace
// copied from https://hermanschaaf.com/running-the-go-race-detector-with-cover/
func add100() int {
total := 0
c := make(chan int, 1)
for i := 0; i < 100; i++ {
go func(chan int) {
c <- 1
}(c)
}
for u := 0; u < 100; u++ {
total += <-c
}
return total
}

-- coverrace_test.go --
package coverrace
// copied from https://hermanschaaf.com/running-the-go-race-detector-with-cover/

import "testing"

func TestCoverRace(t *testing.T) {
got := add100()
if got != 100 {
t.Errorf("got %d, want %d", got, 100)
}
}
`,
})
}
Expand Down Expand Up @@ -199,6 +241,10 @@ func Test(t *testing.T) {
cmd: "build",
target: "//:pure_race_bin",
wantBuildFail: true,
}, {
desc: "cover_race",
cmd: "coverage",
target: "//:coverrace_test",
},
} {
t.Run(test.desc, func(t *testing.T) {
Expand Down