Skip to content

Commit ab4ed3c

Browse files
jayconrodArielleA
authored andcommitted
Turn on race and msan tags when those modes are enabled (bazel-contrib#1562)
Also: clean up the race tests, move to tests/core, and test that the race tag is set. Fixes bazel-contrib#1559
1 parent 102f1e8 commit ab4ed3c

15 files changed

+128
-73
lines changed

go/private/context.bzl

+6-1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ def go_context(ctx, attr = None):
224224

225225
context_data = attr._go_context_data
226226
mode = get_mode(ctx, host_only, toolchain, context_data)
227+
tags = list(context_data.tags)
228+
if mode.race:
229+
tags.append("race")
230+
if mode.msan:
231+
tags.append("msan")
227232
binary = _get_go_binary(context_data)
228233

229234
stdlib = getattr(attr, "_stdlib", None)
@@ -268,7 +273,7 @@ def go_context(ctx, attr = None):
268273
coverage_enabled = ctx.configuration.coverage_enabled,
269274
coverage_instrumented = ctx.coverage_instrumented(),
270275
env = env,
271-
tags = context_data.tags,
276+
tags = tags,
272277
# Action generators
273278
archive = toolchain.actions.archive,
274279
asm = toolchain.actions.asm,

tests/core/README.rst

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Contents
99
.. Child list start
1010
1111
* `Basic go_path functionality <go_path/README.rst>`_
12+
* `Basic -buildmode=plugin functionality <go_plugin/README.rst>`_
1213
* `Basic go_binary functionality <go_binary/README.rst>`_
1314
* `Basic go_library functionality <go_library/README.rst>`_
1415
* `Cross compilation <cross/README.rst>`_
@@ -19,6 +20,7 @@ Contents
1920
* `Import maps <importmap/README.rst>`_
2021
* `Basic go_test functionality <go_test/README.rst>`_
2122
* `Basic cgo functionality <cgo/README.rst>`_
23+
* `race instrumentation <race/README.rst>`_
2224
* `go_proto_library importmap <go_proto_library_importmap/README.rst>`_
2325

2426
.. Child list end
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,57 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test", "go_binary", "go_source")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_source", "go_test")
22
load("@io_bazel_rules_go//tests:bazel_tests.bzl", "bazel_test")
3-
load(":generate_test.bzl", "generate_script")
43

5-
go_library(
6-
name = "go_default_library",
7-
srcs = ["race.go"],
8-
importpath = "github.com/bazelbuild/rules_go/tests/race",
9-
deps = ["//tests/legacy/race/racy:go_default_library"],
10-
)
11-
12-
go_binary(
13-
name = "race_main",
14-
srcs = ["race_main.go"],
15-
deps = [":go_default_library"],
4+
go_test(
5+
name = "race_auto_test",
6+
size = "small",
7+
embed = [":race_test_src"],
168
)
179

1810
go_source(
19-
name = "race_test",
11+
name = "race_test_src",
2012
srcs = ["race_test.go"],
2113
embed = [":go_default_library"],
2214
)
2315

2416
go_test(
25-
name = "race_auto_test",
26-
size = "small",
27-
embed = [":race_test"],
17+
name = "race_on_test",
18+
srcs = [
19+
"race_off_fail_test.go",
20+
"race_on_test.go",
21+
],
22+
args = [
23+
"$(location :race_on_tester)",
24+
"$(location :race_bin)",
25+
],
26+
data = [
27+
":race_bin",
28+
":race_on_tester",
29+
],
30+
race = "on",
31+
rundir = ".",
32+
deps = ["//tests/core/race/racy:go_default_library"],
2833
)
2934

3035
go_test(
3136
name = "race_on_tester",
32-
size = "small",
33-
embed = [":race_test"],
37+
embed = [":race_test_src"],
3438
race = "on",
3539
tags = ["manual"],
3640
)
3741

38-
generate_script(
39-
name = "race_on_test_script",
40-
testonly = True,
41-
binary = ":race_on_tester",
42-
)
43-
44-
sh_test(
45-
name = "race_on_test",
46-
size = "small",
47-
srcs = [":race_on_test_script"],
48-
data = [":race_on_tester"],
42+
go_binary(
43+
name = "race_bin",
44+
srcs = ["race_main.go"],
45+
race = "on",
46+
deps = [":go_default_library"],
4947
)
5048

5149
bazel_test(
52-
name = "race_feature",
50+
name = "race_feature_test",
5351
args = ["--features=race"],
5452
check = """
5553
if [ "$result" -ne 3 ]; then
5654
echo "error: bazel test failure expected (code 3). Got code $result" >&2
57-
cat bazel-output.txt
5855
exit 1
5956
else
6057
result=0
@@ -63,3 +60,10 @@ fi
6360
command = "test",
6461
targets = [":race_auto_test"],
6562
)
63+
64+
go_library(
65+
name = "go_default_library",
66+
srcs = ["race.go"],
67+
importpath = "github.com/bazelbuild/rules_go/tests/core/race",
68+
deps = ["//tests/core/race/racy:go_default_library"],
69+
)

tests/core/race/README.rst

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
race instrumentation
2+
====================
3+
4+
race_auto_test
5+
--------------
6+
7+
Triggers a data race inside of a test but does not explicitly enable race mode.
8+
Should pass if race instrumentation is disabled.
9+
10+
race_on_test
11+
------------
12+
13+
Checks that race mode is actually enabled in binaries and tests that enable
14+
it through an attribute by running a binary (``race_bin``) and a test
15+
(``race_on_tester``) and verifying that they fail.
16+
17+
Also checks that the ``race`` tag is enabled in the test itself and in
18+
a library.
19+
20+
race_feature_test
21+
-----------------
22+
23+
Checks that race mode can be enabled with ``--features=race`` by running
24+
``race_auto_test`` with that flag and verifying that it fails.
25+

tests/legacy/race/race.go renamed to tests/core/race/race.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package race
22

33
import (
4-
"github.com/bazelbuild/rules_go/tests/race/racy"
4+
"github.com/bazelbuild/rules_go/tests/core/race/racy"
55
)
66

77
func TriggerRace() {

tests/legacy/race/race_main.go renamed to tests/core/race/race_main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"github.com/bazelbuild/rules_go/tests/race"
4+
"github.com/bazelbuild/rules_go/tests/core/race"
55
)
66

77
func main() {

tests/core/race/race_off_fail_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// +build !race
2+
3+
package race
4+
5+
this should not be compiled

tests/core/race/race_on_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package race
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"testing"
7+
8+
"github.com/bazelbuild/rules_go/tests/core/race/racy"
9+
)
10+
11+
func TestRaceTest(t *testing.T) {
12+
checkRaceBinary(t, os.Args[1])
13+
}
14+
15+
func TestRaceBin(t *testing.T) {
16+
checkRaceBinary(t, os.Args[2])
17+
}
18+
19+
func TestRaceTag(t *testing.T) {
20+
if !racy.RaceEnabled {
21+
t.Error("RaceEnabled: got false, want true")
22+
}
23+
}
24+
25+
func checkRaceBinary(t *testing.T, bin string) {
26+
err := exec.Command(bin).Run()
27+
if _, ok := err.(*exec.ExitError); !ok {
28+
t.Errorf("want ExitError; got %v", err)
29+
}
30+
}
File renamed without changes.

tests/core/race/racy/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = [
6+
"race_off.go",
7+
"race_on.go",
8+
"racy.go",
9+
],
10+
importpath = "github.com/bazelbuild/rules_go/tests/core/race/racy",
11+
visibility = ["//visibility:public"],
12+
)

tests/core/race/racy/race_off.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// +build !race
2+
3+
package racy
4+
5+
const RaceEnabled = false

tests/core/race/racy/race_on.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// +build race
2+
3+
package racy
4+
5+
const RaceEnabled = true
File renamed without changes.

tests/legacy/race/generate_test.bzl

-30
This file was deleted.

tests/legacy/race/racy/BUILD.bazel

-8
This file was deleted.

0 commit comments

Comments
 (0)