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

Feature request: Provide full path to go binary via an environment variable #4138

Open
fishy opened this issue Oct 8, 2024 · 2 comments
Open

Comments

@fishy
Copy link
Contributor

fishy commented Oct 8, 2024

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?

@fmeum
Copy link
Member

fmeum commented Oct 8, 2024

Since this appears to be a very special case, how about adding a Go SDK to the runfiles of your tests and then passing its go binary to the test?

That's something you can do without requiring a rules_go change. Even if we wanted to, we couldn't just ship a full Go SDK with every test without regressing performance for everyone.

@fishy
Copy link
Contributor Author

fishy commented Oct 9, 2024

@fmeum thanks but apologize in advance that I'm not sure how to achieve what you suggested.

  1. it doesn't look like go_test rule has attributes like runfile or runfiles. It does have data but I'm not sure if that would work for this purpose.
  2. how do I reference the go_sdk there? our go_sdk is declared in MODULE.bazel like this:
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(
    name = "go_sdk",
    sdks = {
        "darwin_amd64": (
            "go%s.darwin-amd64.tar.gz" % GO_VERSION,
            GO_DARWIN_AMD64_SHA256,
        ),
        "darwin_arm64": (
            "go%s.darwin-arm64.tar.gz" % GO_VERSION,
            GO_DARWIN_ARM64_SHA256,
        ),
        "linux_amd64": (
            "go%s.linux-amd64.tar.gz" % GO_VERSION,
            GO_LINUX_AMD64_SHA256,
        ),
    },
    urls = [
        "https://go.dev/dl/{}",
        "https://dl.google.com/go/{}",
        # TODO: Add an internal mirror here
    ],
    version = GO_VERSION,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants