Skip to content

Commit

Permalink
Replace go_name_hack with a usage of Label
Browse files Browse the repository at this point in the history
The Label constructor can be used to transform a label string such as
@io_bazel_rules_go//foo:bar into the canonical label referencing this
target from the repository in which it is called.

For example, `str(Label("@io_bazel_rules_go//foo:bar"))` returns
`"@io_bazel_rules_go//foo:bar"` if rules_go is used as an external
repository from another main workspace, but returns `"//foo:bar"` when
rules_go is the main repository (e.g., when running the tests in the
CI).

This requires raising the minimum version of Bazel from 4.2.0 to 4.2.1
due to bazelbuild/bazel#13890.
  • Loading branch information
fmeum committed Dec 20, 2021
1 parent d0d326e commit be988df
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
tasks:
ubuntu1804_bazel400:
platform: ubuntu1804
bazel: 4.2.0 # test minimum supported version of bazel
bazel: 4.2.1 # test minimum supported version of bazel
shell_commands:
- tests/core/cgo/generate_imported_dylib.sh
build_targets:
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies(is_rules_go = True)
go_rules_dependencies()

go_register_toolchains(version = "1.17")

Expand Down
2 changes: 1 addition & 1 deletion go/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def get_versioned_shared_lib_extension(path):
# something like 1.2.3, or so.1.2, or dylib.1.2, or foo.1.2
return ""

MINIMUM_BAZEL_VERSION = "4.2.0"
MINIMUM_BAZEL_VERSION = "4.2.1"

def as_list(v):
"""Returns a list, tuple, or depset as a list."""
Expand Down
33 changes: 1 addition & 32 deletions go/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load("//go/private:nogo.bzl", "DEFAULT_NOGO", "go_register_nogo")
load("//proto:gogo.bzl", "gogo_special_proto")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def go_rules_dependencies(is_rules_go = False):
def go_rules_dependencies():
"""Declares workspaces the Go rules depend on. Workspaces that use
rules_go should call this.
Expand Down Expand Up @@ -285,37 +285,6 @@ def go_rules_dependencies(is_rules_go = False):
nogo = DEFAULT_NOGO,
)

go_name_hack(
name = "io_bazel_rules_go_name_hack",
is_rules_go = is_rules_go,
)

def _maybe(repo_rule, name, **kwargs):
if name not in native.existing_rules():
repo_rule(name = name, **kwargs)

def _go_name_hack_impl(ctx):
build_content = """\
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "def",
srcs = ["def.bzl"],
visibility = ["//visibility:public"],
)
"""
ctx.file("BUILD.bazel", build_content)
content = "IS_RULES_GO = {}".format(ctx.attr.is_rules_go)
ctx.file("def.bzl", content)

go_name_hack = repository_rule(
implementation = _go_name_hack_impl,
attrs = {
"is_rules_go": attr.bool(),
},
doc = """go_name_hack records whether the main workspace is rules_go.
See documentation for _filter_transition_label in
go/private/rules/transition.bzl.
""",
)
1 change: 0 additions & 1 deletion go/private/rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ bzl_library(
"@io_bazel_rules_go//go/private:mode",
"@io_bazel_rules_go//go/private:platforms",
"@io_bazel_rules_go//go/private:providers",
"@io_bazel_rules_go_name_hack//:def",
],
)

Expand Down
25 changes: 8 additions & 17 deletions go/private/rules/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ load(
"GoLibrary",
"GoSource",
)
load(
"@io_bazel_rules_go_name_hack//:def.bzl",
"IS_RULES_GO",
)
load(
"@io_bazel_rules_go//go/platform:crosstool.bzl",
"platform_from_crosstool",
Expand All @@ -44,21 +40,16 @@ load(
def filter_transition_label(label):
"""Transforms transition labels for the current workspace.
This is a workaround for bazelbuild/bazel#10499. If a transition refers to
a build setting in the same workspace, for example
@io_bazel_rules_go//go/config:goos, it must use a label without a workspace
name if and only if the workspace is the main workspace.
All Go build settings and transitions are in io_bazel_rules_go. So if
io_bazel_rules_go is the main workspace (for development and testing),
go_transition must use a label like //go/config:goos. If io_bazel_rules_go
is not the main workspace (almost always), go_transition must use a label
like @io_bazel_rules_go//go/config:goos.
This works around bazelbuild/bazel#10499 by automatically using the correct
way to refer to this repository (@io_bazel_rules_go from another workspace,
but only repo-relative labels if this repository is the main workspace).
"""
if IS_RULES_GO and label.startswith("@io_bazel_rules_go"):
return label[len("@io_bazel_rules_go"):]
else:
if label.startswith("//command_line_option:"):
# This is a special label used to refer to Bazel command-line options in
# transitions and should not be modified in any way.
return label
else:
return str(Label(label))

def go_transition_wrapper(kind, transition_kind, name, **kwargs):
"""Wrapper for rules that may use transitions.
Expand Down

0 comments on commit be988df

Please sign in to comment.