Skip to content

Commit 6d30e40

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

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

go/private/extensions.bzl

Lines changed: 25 additions & 14 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(
@@ -126,21 +125,23 @@ def _go_sdk_impl(ctx):
126125
*[t for p in zip(module.tags.nogo, len(module.tags.nogo) * ["\n"]) for t in p]
127126
)
128127
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-
)
128+
129+
# "all" is still processed into a Label instance.
130+
if len(nogo_tag.includes) == 1 and nogo_tag.includes[0].name == "all":
131+
includes = ["all"]
132+
else:
133+
for scope in nogo_tag.includes:
134+
_check_nogo_scope(scope, nogo_tag)
135+
includes = nogo_tag.includes
136+
for scope in nogo_tag.excludes:
137+
_check_nogo_scope(scope, nogo_tag)
138+
138139
go_register_nogo(
139140
name = "io_bazel_rules_nogo",
140141
nogo = str(nogo_tag.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+
includes = [str(l) for l in includes],
144145
excludes = [str(l) for l in nogo_tag.excludes],
145146
)
146147

@@ -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)