diff --git a/BUILD.bazel b/BUILD.bazel index 0bef73312..b49855bac 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,7 +1,7 @@ +load("@bazel_gazelle_is_bazel_module//:defs.bzl", "GAZELLE_IS_BAZEL_MODULE") load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("@io_bazel_rules_go//go:def.bzl", "nogo") load("//:def.bzl", "gazelle", "gazelle_binary") -load("//internal:common.bzl", "IS_BZLMOD_ENABLED") # gazelle:prefix github.com/bazelbuild/bazel-gazelle # gazelle:exclude vendor @@ -32,7 +32,7 @@ gazelle_binary( "//language/proto", "//language/go", "//internal/language/test_filegroup", - "@bazel_skylib_gazelle_plugin//bzl" if IS_BZLMOD_ENABLED else "@bazel_skylib//gazelle/bzl", + "@bazel_skylib_gazelle_plugin//bzl" if GAZELLE_IS_BAZEL_MODULE else "@bazel_skylib//gazelle/bzl", ], ) diff --git a/MODULE.bazel b/MODULE.bazel index d4a8c92e5..58cd44d32 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -22,6 +22,7 @@ use_repo( non_module_deps, "bazel_gazelle_go_repository_cache", "bazel_gazelle_go_repository_tools", + "bazel_gazelle_is_bazel_module", ) go_deps = use_extension("//:extensions.bzl", "go_deps") diff --git a/def.bzl b/def.bzl index d07ae6c70..020cb45d3 100644 --- a/def.bzl +++ b/def.bzl @@ -17,8 +17,8 @@ load( "shell", ) load( - "//internal:common.bzl", - "IS_BZLMOD_ENABLED", + "@bazel_gazelle_is_bazel_module//:defs.bzl", + "GAZELLE_IS_BAZEL_MODULE", ) load( "//internal:go_repository.bzl", @@ -79,7 +79,7 @@ def _gazelle_runner_impl(ctx): args.extend(["-go_prefix", ctx.attr.prefix]) if ctx.attr.build_tags: args.extend(["-build_tags", ",".join(ctx.attr.build_tags)]) - if IS_BZLMOD_ENABLED: + if GAZELLE_IS_BAZEL_MODULE: args.append("-bzlmod") args.extend([ctx.expand_location(arg, ctx.attr.data) for arg in ctx.attr.extra_args]) @@ -153,7 +153,7 @@ _gazelle_runner = rule( "data": attr.label_list(allow_files = True), "env": attr.string_dict(), "_repo_config": attr.label( - default = "@bazel_gazelle_go_repository_config//:WORKSPACE" if IS_BZLMOD_ENABLED else None, + default = "@bazel_gazelle_go_repository_config//:WORKSPACE" if GAZELLE_IS_BAZEL_MODULE else None, allow_single_file = True, ), "_template": attr.label( diff --git a/deps.bzl b/deps.bzl index 8af8eef69..5a8413518 100644 --- a/deps.bzl +++ b/deps.bzl @@ -32,6 +32,10 @@ load( "//internal:go_repository_config.bzl", "go_repository_config", ) +load( + "//internal:is_bazel_module.bzl", + "is_bazel_module", +) # Re-export go_repository . Users should get it from this file. go_repository = _go_repository @@ -84,6 +88,12 @@ def gazelle_dependencies( name = "bazel_gazelle_go_repository_config", config = go_repository_default_config, ) + + is_bazel_module( + name = "bazel_gazelle_is_bazel_module", + is_bazel_module = False, + ) + _maybe( go_repository, name = "co_honnef_go_tools", diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index adcd0420e..4d02ec4e4 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel @@ -46,6 +46,7 @@ filegroup( "go_repository_config.bzl", "go_repository_tools.bzl", "go_repository_tools_srcs.bzl", + "is_bazel_module.bzl", "list_repository_tools_srcs.go", "overlay_repository.bzl", "repository_docs.bzl", @@ -139,6 +140,12 @@ bzl_library( visibility = ["//:__subpackages__"], ) +bzl_library( + name = "is_bazel_module", + srcs = ["is_bazel_module.bzl"], + visibility = ["//:__subpackages__"], +) + bzl_library( name = "overlay_repository", srcs = ["overlay_repository.bzl"], diff --git a/internal/bzlmod/non_module_deps.bzl b/internal/bzlmod/non_module_deps.bzl index 65c95ae7d..ccbc3548a 100644 --- a/internal/bzlmod/non_module_deps.bzl +++ b/internal/bzlmod/non_module_deps.bzl @@ -6,6 +6,10 @@ load( "//internal:go_repository_tools.bzl", "go_repository_tools", ) +load( + "//internal:is_bazel_module.bzl", + "is_bazel_module", +) load( "@go_host_compatible_sdk_label//:defs.bzl", "HOST_COMPATIBLE_SDK", @@ -24,6 +28,10 @@ def _non_module_deps_impl(_): name = "bazel_gazelle_go_repository_tools", go_cache = Label("@bazel_gazelle_go_repository_cache//:go.env"), ) + is_bazel_module( + name = "bazel_gazelle_is_bazel_module", + is_bazel_module = True, + ) non_module_deps = module_extension( _non_module_deps_impl, diff --git a/internal/common.bzl b/internal/common.bzl index cb631931f..912940ef4 100644 --- a/internal/common.bzl +++ b/internal/common.bzl @@ -36,7 +36,3 @@ def executable_extension(ctx): if ctx.os.name.startswith("windows"): extension = ".exe" return extension - -# Label instances stringify to a canonical label if and only if Bzlmod is -# enabled. -IS_BZLMOD_ENABLED = str(Label("//:bogus")).startswith("@@") diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl index cb0ac7405..8dac6eb9b 100644 --- a/internal/go_repository.bzl +++ b/internal/go_repository.bzl @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//internal:common.bzl", "IS_BZLMOD_ENABLED", "env_execute", "executable_extension") +load("//internal:common.bzl", "env_execute", "executable_extension") load("//internal:go_repository_cache.bzl", "read_cache_env") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch", "read_user_netrc", "use_netrc") @@ -284,7 +284,8 @@ def _go_repository_impl(ctx): # ctx.attr.name is the canonical name of this repository, which contains a '~' if and only # if this repository is generated by a module extension rather than an invocation in # WORKSPACE. - if "~" in ctx.attr.name: + is_module_extension_repo = "~" in ctx.attr.name + if is_module_extension_repo: # TODO: In Bazel 6.3.0 and earlier, there is no way to obtain a label referencing a repo # generated by an extension from within that extension. We thus have to manually # construct such a label pointing to the sibling `_go_repository_config` repo created by @@ -318,7 +319,7 @@ def _go_repository_impl(ctx): cmd.extend(["-proto", ctx.attr.build_file_proto_mode]) if ctx.attr.build_naming_convention: cmd.extend(["-go_naming_convention", ctx.attr.build_naming_convention]) - if IS_BZLMOD_ENABLED: + if is_module_extension_repo: cmd.append("-bzlmod") cmd.extend(ctx.attr.build_extra_args) cmd.append(ctx.path("")) diff --git a/internal/is_bazel_module.bzl b/internal/is_bazel_module.bzl new file mode 100644 index 000000000..4fe29aaf0 --- /dev/null +++ b/internal/is_bazel_module.bzl @@ -0,0 +1,27 @@ +# Copyright 2023 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def _is_bazel_module_impl(repository_ctx): + repository_ctx.file("WORKSPACE") + repository_ctx.file("BUILD") + repository_ctx.file("defs.bzl", "GAZELLE_IS_BAZEL_MODULE = {}".format( + repr(repository_ctx.attr.is_bazel_module), + )) + +is_bazel_module = repository_rule( + implementation = _is_bazel_module_impl, + attrs = { + "is_bazel_module": attr.bool(mandatory = True), + }, +) diff --git a/tests/bcr/MODULE.bazel.lock b/tests/bcr/MODULE.bazel.lock index ea31e14ed..f29e22c29 100644 --- a/tests/bcr/MODULE.bazel.lock +++ b/tests/bcr/MODULE.bazel.lock @@ -15,7 +15,7 @@ "localOverrideHashes": { "test_dep": "6d4c308ab31c6cde49afa32b1b21e45cc779d342bfae4aa933d3aec14bd0e08a", "bazel_tools": "11c49407fdc54b48d69dcd4478440118124b9cd51b2dca5947a6414a585964a1", - "gazelle": "189cf3ff7d22c799d914198958c863c1097bc1b010929c9593df7188e760350d" + "gazelle": "6ed847b9ccfcbebb794d788d9c8aacbb74415e93b6a97286dc688022255e1d0e" }, "moduleDepGraph": { "": { @@ -334,7 +334,8 @@ }, "imports": { "bazel_gazelle_go_repository_cache": "bazel_gazelle_go_repository_cache", - "bazel_gazelle_go_repository_tools": "bazel_gazelle_go_repository_tools" + "bazel_gazelle_go_repository_tools": "bazel_gazelle_go_repository_tools", + "bazel_gazelle_is_bazel_module": "bazel_gazelle_is_bazel_module" }, "devImports": [], "tags": [], @@ -347,7 +348,7 @@ "usingModule": "gazelle@_", "location": { "file": "@@gazelle~override//:MODULE.bazel", - "line": 27, + "line": 28, "column": 24 }, "imports": { @@ -370,7 +371,7 @@ "devDependency": false, "location": { "file": "@@gazelle~override//:MODULE.bazel", - "line": 28, + "line": 29, "column": 18 } }, @@ -380,7 +381,7 @@ "devDependency": false, "location": { "file": "@@gazelle~override//:MODULE.bazel", - "line": 32, + "line": 33, "column": 15 } } @@ -1228,7 +1229,7 @@ } }, "@gazelle~override//:extensions.bzl%go_deps%test_dep@_~go_deps": { - "bzlTransitiveDigest": "ti6/wrYI3K/ecfLzekOUxKJVqza2UYx79wFadZG0AMQ=", + "bzlTransitiveDigest": "bSY+hUmga4vR05FClkVW1o1QpSWRZrUhdX0L1DUMudU=", "accumulatedFileDigests": {}, "envVariables": {}, "generatedRepoSpecs": { @@ -1255,7 +1256,7 @@ } }, "@gazelle~override//:extensions.bzl%go_deps": { - "bzlTransitiveDigest": "ti6/wrYI3K/ecfLzekOUxKJVqza2UYx79wFadZG0AMQ=", + "bzlTransitiveDigest": "bSY+hUmga4vR05FClkVW1o1QpSWRZrUhdX0L1DUMudU=", "accumulatedFileDigests": { "@@//:go.mod": "8e62c686b94b37593b38766d425ceccbf86a9b07c0121feaaf42293050a42ae3", "@@circl~1.3.3//:go.mod": "68b12e4662bb0639728490153ffc52d8bdd63be558bdd41bcb0d8f1eeeb03e41", @@ -1416,10 +1417,15 @@ } }, "@gazelle~override//internal/bzlmod:non_module_deps.bzl%non_module_deps": { - "bzlTransitiveDigest": "XZ67Cmf710UpuC/N/VbLMLZgN5otvjvWQGt30qJ15y8=", + "bzlTransitiveDigest": "ISQc84o0GNdJg4L98yZAVsk/zgDkdR6FNf5cLjBcfp0=", "accumulatedFileDigests": {}, "envVariables": {}, "generatedRepoSpecs": { + "bazel_gazelle_is_bazel_module": { + "bzlFile": "@@gazelle~override//internal:is_bazel_module.bzl", + "ruleClassName": "is_bazel_module", + "attributes": {"name":"--gazelle~override~non_module_deps~bazel_gazelle_is_bazel_module","is_bazel_module":true} + }, "bazel_gazelle_go_repository_tools": { "bzlFile": "@@gazelle~override//internal:go_repository_tools.bzl", "ruleClassName": "go_repository_tools",