bazel-contrib / rules_go

Go rules for Bazel
Apache License 2.0
1.38k stars 656 forks source link

go_host_sdk is wrongly called in bzlmod #4011

Closed fishy closed 2 months ago

fishy commented 2 months ago

What version of rules_go are you using?

0.48.1

What version of gazelle are you using?

0.38.0

What version of Bazel are you using?

7.2.1

Does this issue reproduce with the latest releases of all the above?

Those are latest releases

What operating system and processor architecture are you using?

linux/amd64

Any other potentially useful information about your toolchain?

What did you do?

We used to have rules_go in WORKSPACE with this code to define go sdk:

GO_VERSION = "1.22.5"
GO_LINUX_AMD64_SHA256 = "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0"
GO_DARWIN_AMD64_SHA256 = "95d9933cdcf45f211243c42c7705c37353cccd99f27eb4d8e2d1bf2f4165cb50"
GO_DARWIN_ARM64_SHA256 = "4cd1bcb05be03cecb77bccd765785d5ff69d79adf4dd49790471d00c06b41133"

...

go_rules_dependencies()

go_download_sdk(
    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,
)

go_register_toolchains()

This works fine with environment without go toolchain installed locally (for example, CI environment with bazel base image), which is expected as we instructed it to download go sdk at the specific version.

But when I try to move it into bzlmod, with:

...
bazel_dep(name = "rules_go", version = "0.48.1")
...
GO_VERSION = "1.22.5"
GO_LINUX_AMD64_SHA256 = "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0"
GO_DARWIN_AMD64_SHA256 = "95d9933cdcf45f211243c42c7705c37353cccd99f27eb4d8e2d1bf2f4165cb50"
GO_DARWIN_ARM64_SHA256 = "4cd1bcb05be03cecb77bccd765785d5ff69d79adf4dd49790471d00c06b41133"

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,
)

go_sdk.host()
...

It works when there's go installed locally, but fails when there's not (in CI), with this error:

INFO: Repository rules_go~~go_sdk~main___host_0 instantiated at:
  <builtin>: in <toplevel>
Repository rule go_host_sdk_rule defined at:
  /root/.cache/bazel/_bazel_root/b2fa2b4102717e3751086b071dc87499/external/rules_go~/go/private/sdk.bzl:30:35: in <toplevel>
ERROR: An error occurred during the fetch of repository 'rules_go~~go_sdk~main___host_0':
   Traceback (most recent call last):
    File "/root/.cache/bazel/_bazel_root/b2fa2b4102717e3751086b071dc87499/external/rules_go~/go/private/sdk.bzl", line 24, column 30, in _go_host_sdk_impl
        goroot = _detect_host_sdk(ctx)
    File "/root/.cache/bazel/_bazel_root/b2fa2b4102717e3751086b071dc87499/external/rules_go~/go/private/sdk.bzl", line 553, column 13, in _detect_host_sdk
        fail("Could not detect host go version")
Error in fail: Could not detect host go version
ERROR: /path/to/BUILD.bazel:28:8: While resolving toolchains for target //path/to/foo:foo_test (0024871): invalid registered toolchain '@go_toolchains//:all': while parsing '@go_toolchains//:all': no such package '@@rules_go~~go_sdk~main___host_0//': Could not detect host go version

It seems that it wrongly referenced go_host_sdk which doesn't work without go toolchain installed locally.

What did you expect to see?

What did you see instead?

fishy commented 2 months ago

Nevermind I read the doc wrong. I read https://github.com/bazelbuild/rules_go/blob/5ec14eeda14c221a03b314ba9ac315a4ef909fc0/docs/go/core/bzlmod.md#go-sdks as I need to call go_sdk.host() after called go_sdk.download(), but it's just one of the alternatives in parallel with the 2 different ways to call go_sdk.download().