Skip to content

Commit

Permalink
bzlmod: Fix repo name used by gopackagesdriver (#3516)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
2 people authored and linzhp committed Apr 20, 2023
1 parent 2c1e034 commit fc928a6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
15 changes: 15 additions & 0 deletions go/tools/gopackagesdriver/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -20,5 +21,19 @@ go_library(
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"],
)
3 changes: 3 additions & 0 deletions go/tools/gopackagesdriver/aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ PROTO_COMPILER_ATTRS = [
"compilers",
]

def bazel_supports_canonical_label_literals():
return str(Label("//:bogus")).startswith("@@")

def is_file_external(f):
return f.owner.workspace_root != ""

Expand Down
4 changes: 1 addition & 3 deletions go/tools/gopackagesdriver/bazel_json_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ type BazelJSONBuilder struct {
requests []string
}

const (
RulesGoStdlibLabel = "@io_bazel_rules_go//:stdlib"
)
var RulesGoStdlibLabel = rulesGoRepositoryName + "//:stdlib"

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

Expand Down
6 changes: 3 additions & 3 deletions go/tools/gopackagesdriver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ type driverResponse struct {
}

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"))
Expand Down
1 change: 0 additions & 1 deletion tools/gopackagesdriver.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/usr/bin/env bash
export GOPACKAGESDRIVER_RULES_GO_REPOSITORY_NAME=
exec bazel run --tool_tag=gopackagesdriver -- //go/tools/gopackagesdriver "${@}"

0 comments on commit fc928a6

Please sign in to comment.