bzlmod: Fix repo name used by gopackagesdriver (#3516)
* Reference `rulesGoRepositoryName` (sourced from environment variable) instead of hard-coding `@io_bazel_rules_go` repo name.
* gopackagesdriver: Inject rules_go repo name via x_defs
This ensures that we always specify the correct repo name when invoking
the aspect, even with Bzlmod (where users usually refer to rules_go as
`@rules_go`, not `@io_bazel_rules_go`, but could choose any apparent
repository name for it).
---------
Co-authored-by: Brice <33697112+bricedp@users.noreply.github.com>
diff --git a/go/tools/gopackagesdriver/BUILD.bazel b/go/tools/gopackagesdriver/BUILD.bazel
index 46aa746..542b75a 100644
--- a/go/tools/gopackagesdriver/BUILD.bazel
+++ b/go/tools/gopackagesdriver/BUILD.bazel
@@ -1,4 +1,5 @@
load("//go:def.bzl", "go_binary", "go_library")
+load(":aspect.bzl", "bazel_supports_canonical_label_literals")
go_library(
name = "gopackagesdriver_lib",
@@ -20,5 +21,19 @@
go_binary(
name = "gopackagesdriver",
embed = [":gopackagesdriver_lib"],
+ x_defs = {
+ # Determine the name of the rules_go repository as we need to specify it when invoking the
+ # aspect.
+ # If canonical label literals are supported, we can use a canonical label literal (starting
+ # with @@) to pass the repository_name() through repo mapping unchanged.
+ # If canonical label literals are not supported, then bzlmod is certainly not enabled and
+ # we can assume that the repository name is not affected by repo mappings.
+ # If run in the rules_go repo itself, repository_name() returns "@", which is equivalent to
+ # "@io_bazel_rules_go" since Bazel 6:
+ # https://github.com/bazelbuild/bazel/commit/7694cf75e6366b92e3905c2ad60234cda57627ee
+ # TODO: Once we drop support for Bazel 5, we can remove the feature detection logic and
+ # use "@" + repository_name().
+ "rulesGoRepositoryName": "@" + repository_name() if bazel_supports_canonical_label_literals() else repository_name(),
+ },
visibility = ["//visibility:public"],
)
diff --git a/go/tools/gopackagesdriver/aspect.bzl b/go/tools/gopackagesdriver/aspect.bzl
index e291ae8..665e0fe 100644
--- a/go/tools/gopackagesdriver/aspect.bzl
+++ b/go/tools/gopackagesdriver/aspect.bzl
@@ -34,6 +34,9 @@
"compilers",
]
+def bazel_supports_canonical_label_literals():
+ return str(Label("//:bogus")).startswith("@@")
+
def is_file_external(f):
return f.owner.workspace_root != ""
diff --git a/go/tools/gopackagesdriver/bazel_json_builder.go b/go/tools/gopackagesdriver/bazel_json_builder.go
index 3067667..04810e3 100644
--- a/go/tools/gopackagesdriver/bazel_json_builder.go
+++ b/go/tools/gopackagesdriver/bazel_json_builder.go
@@ -31,9 +31,7 @@
requests []string
}
-const (
- RulesGoStdlibLabel = "@io_bazel_rules_go//:stdlib"
-)
+var RulesGoStdlibLabel = rulesGoRepositoryName + "//:stdlib"
var _defaultKinds = []string{"go_library", "go_test", "go_binary"}
diff --git a/go/tools/gopackagesdriver/main.go b/go/tools/gopackagesdriver/main.go
index 06da648..0e7fab0 100644
--- a/go/tools/gopackagesdriver/main.go
+++ b/go/tools/gopackagesdriver/main.go
@@ -49,9 +49,9 @@
}
var (
- // It seems https://github.com/bazelbuild/bazel/issues/3115 isn't fixed when specifying
- // the aspect from the command line. Use this trick in the mean time.
- rulesGoRepositoryName = getenvDefault("GOPACKAGESDRIVER_RULES_GO_REPOSITORY_NAME", "@io_bazel_rules_go")
+ // Injected via x_defs.
+
+ rulesGoRepositoryName string
goDefaultAspect = rulesGoRepositoryName + "//go/tools/gopackagesdriver:aspect.bzl%go_pkg_info_aspect"
bazelBin = getenvDefault("GOPACKAGESDRIVER_BAZEL", "bazel")
bazelStartupFlags = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_FLAGS"))
diff --git a/tools/gopackagesdriver.sh b/tools/gopackagesdriver.sh
index b29cc41..4f84de4 100755
--- a/tools/gopackagesdriver.sh
+++ b/tools/gopackagesdriver.sh
@@ -1,3 +1,2 @@
#!/usr/bin/env bash
-export GOPACKAGESDRIVER_RULES_GO_REPOSITORY_NAME=
exec bazel run --tool_tag=gopackagesdriver -- //go/tools/gopackagesdriver "${@}"