Skip to content

Commit f5a8014

Browse files
committed
Introduce ["all"] scope for nogo
1 parent 54ba841 commit f5a8014

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

go/private/extensions.bzl

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,15 @@ _nogo_tag = tag_class(
7474
),
7575
"includes": attr.label_list(
7676
default = NOGO_DEFAULT_INCLUDES,
77-
# The special include "all" is undocumented on purpose: With it, adding a new transitive
78-
# dependency to a Go module can cause a build failure if the new dependency has lint
79-
# issues.
8077
doc = """
8178
A Go target is checked with nogo if its package matches at least one of the entries in 'includes'
8279
and none of the entries in 'excludes'. By default, nogo is applied to all targets in the main
8380
repository.
8481
8582
Uses the same format as 'visibility', i.e., every entry must be a label that ends with ':__pkg__' or
86-
':__subpackages__'.
83+
':__subpackages__'. As an exception to this rule, the special value ["all"] is allowed for 'includes'
84+
and means that nogo should be applied to all Go targets, including those in all external
85+
repositories.
8786
""",
8887
),
8988
"excludes": attr.label_list(
@@ -111,11 +110,9 @@ _MAX_NUM_TOOLCHAINS = 9999
111110
_TOOLCHAIN_INDEX_PAD_LENGTH = len(str(_MAX_NUM_TOOLCHAINS))
112111

113112
def _go_sdk_impl(ctx):
114-
nogo_tag = struct(
115-
nogo = DEFAULT_NOGO,
116-
includes = NOGO_DEFAULT_INCLUDES,
117-
excludes = NOGO_DEFAULT_EXCLUDES,
118-
)
113+
nogo = DEFAULT_NOGO
114+
nogo_includes = NOGO_DEFAULT_INCLUDES
115+
nogo_excludes = NOGO_DEFAULT_EXCLUDES
119116
for module in ctx.modules:
120117
if not module.is_root or not module.tags.nogo:
121118
continue
@@ -126,22 +123,26 @@ def _go_sdk_impl(ctx):
126123
*[t for p in zip(module.tags.nogo, len(module.tags.nogo) * ["\n"]) for t in p]
127124
)
128125
nogo_tag = module.tags.nogo[0]
129-
for scope in nogo_tag.includes + nogo_tag.excludes:
130-
# Validate that the scope references a valid, visible repository.
131-
# buildifier: disable=no-effect
132-
scope.workspace_name
133-
if scope.name != "__pkg__" and scope.name != "__subpackages__":
134-
fail(
135-
"go_sdk.nogo: all entries in includes and excludes must end with ':__pkg__' or ':__subpackages__', got '{}' in".format(scope.name),
136-
nogo_tag,
137-
)
126+
nogo = nogo_tag.nogo
127+
nogo_includes = nogo_tag.includes
128+
nogo_excludes = nogo_tag.excludes
129+
130+
# "all" is still processed into a Label instance, so we just check its name.
131+
if len(nogo_includes) == 1 and nogo_includes[0].name == "all":
132+
nogo_includes = ["all"]
133+
else:
134+
for scope in nogo_includes:
135+
_check_nogo_scope(scope, nogo_tag)
136+
for scope in nogo_excludes:
137+
_check_nogo_scope(scope, nogo_tag)
138+
138139
go_register_nogo(
139140
name = "io_bazel_rules_nogo",
140-
nogo = str(nogo_tag.nogo),
141+
nogo = str(nogo),
141142
# Go through canonical label literals to avoid a dependency edge on the packages in the
142143
# scope.
143-
includes = [str(l) for l in nogo_tag.includes],
144-
excludes = [str(l) for l in nogo_tag.excludes],
144+
includes = [str(l) for l in nogo_includes],
145+
excludes = [str(l) for l in nogo_excludes],
145146
)
146147

147148
multi_version_module = {}
@@ -300,6 +301,16 @@ def _go_sdk_impl(ctx):
300301
else:
301302
return None
302303

304+
def _check_nogo_scope(scope, nogo_tag):
305+
# Validate that the scope references a valid, visible repository.
306+
# buildifier: disable=no-effect
307+
scope.workspace_name
308+
if scope.name != "__pkg__" and scope.name != "__subpackages__":
309+
fail(
310+
"go_sdk.nogo: all entries in includes and excludes must end with ':__pkg__' or ':__subpackages__', got '{}' in".format(scope.name),
311+
nogo_tag,
312+
)
313+
303314
def _default_go_sdk_name(*, module, multi_version, tag_type, index, suffix = ""):
304315
# Keep the version out of the repository name if possible to prevent unnecessary rebuilds when
305316
# it changes.

0 commit comments

Comments
 (0)