| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 1 | commit 7c26118cffcb3d01bf5f668c4ca563c33d20814a |
| 2 | Author: Fabian Meumertzheim <fabian@meumertzhe.im> |
| 3 | Date: Sun Feb 16 23:26:59 2025 +0100 |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 4 | |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 5 | Introduce `["all"]` scope for nogo |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 6 | |
| 7 | diff --git a/go/private/extensions.bzl b/go/private/extensions.bzl |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 8 | index a53dc6e3..8636b385 100644 |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 9 | --- a/go/private/extensions.bzl |
| 10 | +++ b/go/private/extensions.bzl |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 11 | @@ -79,16 +79,15 @@ _nogo_tag = tag_class( |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 12 | ), |
| 13 | "includes": attr.label_list( |
| 14 | default = NOGO_DEFAULT_INCLUDES, |
| 15 | - # The special include "all" is undocumented on purpose: With it, adding a new transitive |
| 16 | - # dependency to a Go module can cause a build failure if the new dependency has lint |
| 17 | - # issues. |
| 18 | doc = """ |
| 19 | A Go target is checked with nogo if its package matches at least one of the entries in 'includes' |
| 20 | and none of the entries in 'excludes'. By default, nogo is applied to all targets in the main |
| 21 | repository. |
| 22 | |
| 23 | Uses the same format as 'visibility', i.e., every entry must be a label that ends with ':__pkg__' or |
| 24 | -':__subpackages__'. |
| 25 | +':__subpackages__'. As an exception to this rule, the special value ["all"] is allowed for 'includes' |
| 26 | +and means that nogo should be applied to all Go targets, including those in all external |
| 27 | +repositories. |
| 28 | """, |
| 29 | ), |
| 30 | "excludes": attr.label_list( |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 31 | @@ -144,11 +143,9 @@ _MAX_NUM_TOOLCHAINS = 9999 |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 32 | _TOOLCHAIN_INDEX_PAD_LENGTH = len(str(_MAX_NUM_TOOLCHAINS)) |
| 33 | |
| 34 | def _go_sdk_impl(ctx): |
| 35 | - nogo_tag = struct( |
| 36 | - nogo = DEFAULT_NOGO, |
| 37 | - includes = NOGO_DEFAULT_INCLUDES, |
| 38 | - excludes = NOGO_DEFAULT_EXCLUDES, |
| 39 | - ) |
| 40 | + nogo = DEFAULT_NOGO |
| 41 | + nogo_includes = NOGO_DEFAULT_INCLUDES |
| 42 | + nogo_excludes = NOGO_DEFAULT_EXCLUDES |
| 43 | for module in ctx.modules: |
| 44 | if not module.is_root or not module.tags.nogo: |
| 45 | continue |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 46 | @@ -159,22 +156,26 @@ def _go_sdk_impl(ctx): |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 47 | *[t for p in zip(module.tags.nogo, len(module.tags.nogo) * ["\n"]) for t in p] |
| 48 | ) |
| 49 | nogo_tag = module.tags.nogo[0] |
| 50 | - for scope in nogo_tag.includes + nogo_tag.excludes: |
| 51 | - # Validate that the scope references a valid, visible repository. |
| 52 | - # buildifier: disable=no-effect |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 53 | - scope.repo_name |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 54 | - if scope.name != "__pkg__" and scope.name != "__subpackages__": |
| 55 | - fail( |
| 56 | - "go_sdk.nogo: all entries in includes and excludes must end with ':__pkg__' or ':__subpackages__', got '{}' in".format(scope.name), |
| 57 | - nogo_tag, |
| 58 | - ) |
| 59 | + nogo = nogo_tag.nogo |
| 60 | + nogo_includes = nogo_tag.includes |
| 61 | + nogo_excludes = nogo_tag.excludes |
| 62 | + |
| 63 | + # "all" is still processed into a Label instance, so we just check its name. |
| 64 | + if len(nogo_includes) == 1 and nogo_includes[0].name == "all": |
| 65 | + nogo_includes = ["all"] |
| 66 | + else: |
| 67 | + for scope in nogo_includes: |
| 68 | + _check_nogo_scope(scope, nogo_tag) |
| 69 | + for scope in nogo_excludes: |
| 70 | + _check_nogo_scope(scope, nogo_tag) |
| 71 | + |
| 72 | go_register_nogo( |
| 73 | name = "io_bazel_rules_nogo", |
| 74 | - nogo = str(nogo_tag.nogo), |
| 75 | + nogo = str(nogo), |
| 76 | # Go through canonical label literals to avoid a dependency edge on the packages in the |
| 77 | # scope. |
| 78 | - includes = [str(l) for l in nogo_tag.includes], |
| 79 | - excludes = [str(l) for l in nogo_tag.excludes], |
| 80 | + includes = [str(l) for l in nogo_includes], |
| 81 | + excludes = [str(l) for l in nogo_excludes], |
| 82 | ) |
| 83 | |
| 84 | multi_version_module = {} |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 85 | @@ -374,6 +375,16 @@ def _go_sdk_impl(ctx): |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 86 | else: |
| 87 | return None |
| 88 | |
| 89 | +def _check_nogo_scope(scope, nogo_tag): |
| 90 | + # Validate that the scope references a valid, visible repository. |
| 91 | + # buildifier: disable=no-effect |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 92 | + scope.repo_name |
| Tim Windelschmidt | a068440 | 2025-02-25 11:32:40 +0100 | [diff] [blame] | 93 | + if scope.name != "__pkg__" and scope.name != "__subpackages__": |
| 94 | + fail( |
| 95 | + "go_sdk.nogo: all entries in includes and excludes must end with ':__pkg__' or ':__subpackages__', got '{}' in".format(scope.name), |
| 96 | + nogo_tag, |
| 97 | + ) |
| 98 | + |
| 99 | def _default_go_sdk_name(*, module, multi_version, tag_type, index, suffix = ""): |
| Jan Schär | 1e9f560 | 2025-05-21 07:40:56 +0000 | [diff] [blame] | 100 | # Keep the version and name of the root module out of the repository name if possible to |
| 101 | # prevent unnecessary rebuilds when it changes. |