bazel-contrib / bazel-contrib/rules_go
Feature request: Provide full path to go binary via an environment variable
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 1.5k
- Forks
- 762
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 12
Description
What version of rules_go are you using?
0.50.1
What version of gazelle are you using?
0.39.0
What version of Bazel are you using?
7.3.2
Does this issue reproduce with the latest releases of all the above?
Yes
What operating system and processor architecture are you using?
linux/amd64
Any other potentially useful information about your toolchain?
What did you do?
For reasons, we have an internal library that heavily depends on go binary (it runs go commands via os/exec, etc.). In its provided test lib, when setting up a test it also depends on the go binary to do the setup.
Without bazel, it could just assume go is in $PATH and run it blindly. But with bazel that assumption is broken and it needs to know where to find the go binary. This isn't a problem when developers run unit tests locally because we can still assume they have go in $PATH even if they run bazel to build/test locally, but it's a problem for CI because on CI we use a bazel based docker image without go in $PATH.
Thus why it would be great if rules_go can inject that info via some environment variable, so it can gets that info via env var (only needed in tests if we don't want to inject it everywhere).
I tried to check whether go toolchain itself does this, with test code to print everything from os.Environ, and found out that $_ would be pointing to the go binary when running go test.
e.g. With this test code:
package test
import (
"os"
"testing"
)
func TestPrintEnvs(t *testing.T) {
t.Errorf("$_ = %q", os.Getenv("_"))
}
via go test it prints out:
$ go test
--- FAIL: TestPrintEnvs (0.00s)
go_test.go:9: $_ = "/usr/lib/go-1.23/bin/go"
FAIL
exit status 1
but via bazel test :test_test it prints out something else:
INFO: From Testing //test:test_test:
==================== Test output for //test:test_test:
--- FAIL: TestPrintEnvs (0.00s)
go_test.go:9: $_ = "/home/fishy/.cache/bazel/_bazel_fishy/d935e50f2b77a06b1180e48ced8db199/sandbox/linux-sandbox/1173/execroot/_main/bazel-out/k8-fastbuild/bin/test/test_test_/test_test.runfiles/_main/test/test_test_/test_test"
FAIL
================================================================================
What did you expect to see?
What did you see instead?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how rules_go configures the Go toolchain and the environment for Bazel tests. Reproduce the difference between go test and bazel test :test_test, then define the expected environment variable behavior and verify it with the provided environment-printing test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100