Skip to content

Commit a068d1b

Browse files
authored
feat(bzlmod): Use a common constant for detecting bzlmod being enabled (#1302)
Various parts of the codebase detect whether bzlmod is enabled or not. Most of them copy/paste the same if @ in Label(..) trick and use a comment to explain what they're doing. Rather than copy/paste that everywhere, this commit uses a constant defined that does this once and reuses the constant value to determine if bzlmod is enabled. Closes: #1295
1 parent 3ffdf01 commit a068d1b

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

python/cc/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Package for C/C++ specific functionality of the Python rules.
22

33
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
4+
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
45
load("//python/private:current_py_cc_headers.bzl", "current_py_cc_headers")
5-
load("//python/private:util.bzl", "BZLMOD_ENABLED")
66

77
package(
88
default_visibility = ["//:__subpackages__"],

python/pip.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
load("//python/pip_install:pip_repository.bzl", "pip_repository", _package_annotation = "package_annotation")
1717
load("//python/pip_install:repositories.bzl", "pip_install_dependencies")
1818
load("//python/pip_install:requirements.bzl", _compile_pip_requirements = "compile_pip_requirements")
19+
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
1920
load(":versions.bzl", "MINOR_MAPPING")
2021

2122
compile_pip_requirements = _compile_pip_requirements
@@ -286,7 +287,7 @@ def _whl_library_render_alias_target(
286287
wheel_name):
287288
# The template below adds one @, but under bzlmod, the name
288289
# is canonical, so we have to add a second @.
289-
if str(Label("//:unused")).startswith("@@"):
290+
if BZLMOD_ENABLED:
290291
rules_python = "@" + rules_python
291292
alias = ["""\
292293
alias(

python/pip_install/pip_repository.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ load("//python:versions.bzl", "WINDOWS_NAME")
1919
load("//python/pip_install:repositories.bzl", "all_requirements")
2020
load("//python/pip_install:requirements_parser.bzl", parse_requirements = "parse")
2121
load("//python/pip_install/private:srcs.bzl", "PIP_INSTALL_PY_SRCS")
22+
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
2223
load("//python/private:toolchains_repo.bzl", "get_host_os_arch")
2324

2425
CPPFLAGS = "CPPFLAGS"
@@ -76,8 +77,7 @@ def _resolve_python_interpreter(rctx):
7677
if rctx.attr.python_interpreter_target != None:
7778
python_interpreter = rctx.path(rctx.attr.python_interpreter_target)
7879

79-
# If we have @@ we have bzlmod so we need to hand Windows differently.
80-
if str(Label("//:unused")).startswith("@@"):
80+
if BZLMOD_ENABLED:
8181
(os, _) = get_host_os_arch(rctx)
8282

8383
# On Windows, the symlink doesn't work because Windows attempts to find

python/private/bzlmod_enabled.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
"""Variable to check if bzlmod is enabled"""
15+
16+
# When bzlmod is enabled, canonical repos names have @@ in them, while under
17+
# workspace builds, there is never a @@ in labels.
18+
BZLMOD_ENABLED = "@@" in str(Label("//:unused"))

python/private/util.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
load("@bazel_skylib//lib:types.bzl", "types")
1818

19-
# When bzlmod is enabled, canonical repos names have @@ in them, while under
20-
# workspace builds, there is never a @@ in labels.
21-
BZLMOD_ENABLED = "@@" in str(Label("//:unused"))
22-
2319
def copy_propagating_kwargs(from_kwargs, into_kwargs = None):
2420
"""Copies args that must be compatible between two targets with a dependency relationship.
2521

python/repositories.bzl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ For historic reasons, pip_repositories() is defined in //python:pip.bzl.
1919

2020
load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive")
2121
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
22+
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
2223
load("//python/private:coverage_deps.bzl", "coverage_dep")
2324
load(
2425
"//python/private:toolchains_repo.bzl",
@@ -498,9 +499,7 @@ def python_register_toolchains(
498499
**kwargs: passed to each python_repositories call.
499500
"""
500501

501-
# If we have @@ we have bzlmod
502-
bzlmod = str(Label("//:unused")).startswith("@@")
503-
if bzlmod:
502+
if BZLMOD_ENABLED:
504503
# you cannot used native.register_toolchains when using bzlmod.
505504
register_toolchains = False
506505

@@ -580,7 +579,7 @@ def python_register_toolchains(
580579
)
581580

582581
# in bzlmod we write out our own toolchain repos
583-
if bzlmod:
582+
if BZLMOD_ENABLED:
584583
return
585584

586585
toolchains_repo(

0 commit comments

Comments
 (0)