Skip to content

Commit fc928a6

Browse files
fmeumbricedp
authored andcommitted
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 <[email protected]>
1 parent 2c1e034 commit fc928a6

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

go/tools/gopackagesdriver/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//go:def.bzl", "go_binary", "go_library")
2+
load(":aspect.bzl", "bazel_supports_canonical_label_literals")
23

34
go_library(
45
name = "gopackagesdriver_lib",
@@ -20,5 +21,19 @@ go_library(
2021
go_binary(
2122
name = "gopackagesdriver",
2223
embed = [":gopackagesdriver_lib"],
24+
x_defs = {
25+
# Determine the name of the rules_go repository as we need to specify it when invoking the
26+
# aspect.
27+
# If canonical label literals are supported, we can use a canonical label literal (starting
28+
# with @@) to pass the repository_name() through repo mapping unchanged.
29+
# If canonical label literals are not supported, then bzlmod is certainly not enabled and
30+
# we can assume that the repository name is not affected by repo mappings.
31+
# If run in the rules_go repo itself, repository_name() returns "@", which is equivalent to
32+
# "@io_bazel_rules_go" since Bazel 6:
33+
# https://github.com/bazelbuild/bazel/commit/7694cf75e6366b92e3905c2ad60234cda57627ee
34+
# TODO: Once we drop support for Bazel 5, we can remove the feature detection logic and
35+
# use "@" + repository_name().
36+
"rulesGoRepositoryName": "@" + repository_name() if bazel_supports_canonical_label_literals() else repository_name(),
37+
},
2338
visibility = ["//visibility:public"],
2439
)

go/tools/gopackagesdriver/aspect.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ PROTO_COMPILER_ATTRS = [
3434
"compilers",
3535
]
3636

37+
def bazel_supports_canonical_label_literals():
38+
return str(Label("//:bogus")).startswith("@@")
39+
3740
def is_file_external(f):
3841
return f.owner.workspace_root != ""
3942

go/tools/gopackagesdriver/bazel_json_builder.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ type BazelJSONBuilder struct {
3131
requests []string
3232
}
3333

34-
const (
35-
RulesGoStdlibLabel = "@io_bazel_rules_go//:stdlib"
36-
)
34+
var RulesGoStdlibLabel = rulesGoRepositoryName + "//:stdlib"
3735

3836
var _defaultKinds = []string{"go_library", "go_test", "go_binary"}
3937

go/tools/gopackagesdriver/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ type driverResponse struct {
4949
}
5050

5151
var (
52-
// It seems https://github.com/bazelbuild/bazel/issues/3115 isn't fixed when specifying
53-
// the aspect from the command line. Use this trick in the mean time.
54-
rulesGoRepositoryName = getenvDefault("GOPACKAGESDRIVER_RULES_GO_REPOSITORY_NAME", "@io_bazel_rules_go")
52+
// Injected via x_defs.
53+
54+
rulesGoRepositoryName string
5555
goDefaultAspect = rulesGoRepositoryName + "//go/tools/gopackagesdriver:aspect.bzl%go_pkg_info_aspect"
5656
bazelBin = getenvDefault("GOPACKAGESDRIVER_BAZEL", "bazel")
5757
bazelStartupFlags = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_FLAGS"))

tools/gopackagesdriver.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/usr/bin/env bash
2-
export GOPACKAGESDRIVER_RULES_GO_REPOSITORY_NAME=
32
exec bazel run --tool_tag=gopackagesdriver -- //go/tools/gopackagesdriver "${@}"

0 commit comments

Comments
 (0)