Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect whether Gazelle itself is a Bazel module #1624

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
],
)

Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"],
Expand Down
8 changes: 8 additions & 0 deletions internal/bzlmod/non_module_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions internal/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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("@@")
7 changes: 4 additions & 3 deletions internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(""))
Expand Down
27 changes: 27 additions & 0 deletions internal/is_bazel_module.bzl
Original file line number Diff line number Diff line change
@@ -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),
},
)
Loading