bazel-contrib / bazel-contrib/rules_go
Flag `--@io_bazel_rules_go//go/config:race` doesn't work when detecting race issues in unit tests
- Dominant language
- Go
- Stars
- 1.5k
- Forks
- 760
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 12
Description
### What version of rules_go are you using?
v0.39.0
### What version of gazelle are you using?
v0.30.0
### What version of Bazel are you using?
5.4.0
### Does this issue reproduce with the latest releases of all the above?
Yes
### What operating system and processor architecture are you using?
20.04.1-Ubuntu x86_64 GNU/Linux
### Any other potentially useful information about your toolchain?
No
### What did you do?
- I added race = "on" to the go_test() section of BUILD.bazel files, like:
``` bazel
go_test(
name = "a_test",
srcs = ["a_test.go"],
embed = [":a"],
race = "on",
)
```
then i run bazel test, race detection works, which can detect race if race exists.
- However, when i just run the bazel test with `--@io_bazel_rules_go//go/config:race` and without modifying BUILD files, it gets errors:
``` shell
➜ git:(master) bazel test --@io_bazel_rules_go//go/config:race //...
ERROR:
.../external/io_bazel_rules_go/BUILD.bazel:42:7: in stdlib rule @io_bazel_rules_go//:stdlib:
Traceback (most recent call last):
File ".../io_bazel_rules_go/go/private/rules/stdlib.bzl", line 34, column 20, in _stdlib_impl
go = go_context(ctx)
File ".../io_bazel_rules_go/go/private/context.bzl", line 411, column 20, in go_context
mode = get_mode(ctx, toolchain, cgo_context_info, go_config_info)
File ".../io_bazel_rules_go/go/private/mode.bzl", line 92, column 13, in get_mode
fail("race instrumentation can't be enabled when cgo is disabled. Check that pure is not set to \"off\" and a C/C++ toolchain is configured.")
Error in fail: race instrumentation can't be enabled when cgo is disabled. Check that pure is not set to "off" and a C/C++ toolchain is configured.
```
I then added `--@io_bazel_rules_go//go/config:pure=false`, same error messages displayed.
- It seems that if we want to use flags to detect race issues, we have to enable cgo and have a C/C++ toolchain. If we add `race = "on"` to the go_test() section of BUILD.bazel files, which doesn't require C/C++ toolchains. It doesn't make sense.
### What did you expect to see?
We want to use flags and not need toolchains to detect race issues. For we don't want to enable race all the time, modifying BUILD files isn't a good resolution.
### What did you see instead?
I see the toolchain is required when using flags to trigger race detection, and is not required when race declared in the go_test() section of the BUILD files.
Contributor guide
Research direction
Reproduce the issue with Bazel 5.4.0 using --@io_bazel_rules_go//go/config:race, then read go/private/rules/stdlib.bzl, go/private/context.bzl, and go/private/mode.bzl around the reported call chain. Compare this behavior with go_test(race = "on"); done means the command-line flag provides race detection without requiring an otherwise unnecessary C/C++ toolchain.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- build-system, testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100