Skip to content

Commit 79478de

Browse files
authored
fix(bzlmod): silence duplicate version registrations (bazelbuild#2111)
Before this PR we would warn when non-root modules register interpreter versions and the owners of root modules would have no way to silence the warning except for patching rules_python. This PR reduces the default verbosity of the warning to INFO to avoid spamming users by default. Fixes bazelbuild#1818.
1 parent fa13b01 commit 79478de

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ A brief description of the categories of changes:
5555
[#2091](https://github.com/bazelbuild/rules_python/issues/2090).
5656
* (gazelle) Make `gazelle_python_manifest.update` manual to avoid unnecessary
5757
network behavior.
58+
* (bzlmod): The conflicting toolchains during `python` extension will no longer
59+
cause warnings by default. In order to see the warnings for diagnostic purposes
60+
set the env var `RULES_PYTHON_REPO_DEBUG_VERBOSITY` to one of `INFO`, `DEBUG` or `TRACE`.
61+
Fixes [#1818](https://github.com/bazelbuild/rules_python/issues/1818).
5862

5963
### Added
6064
* (rules) `PYTHONSAFEPATH` is inherited from the calling environment to allow

python/private/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ bzl_library(
132132
srcs = ["python.bzl"],
133133
deps = [
134134
":pythons_hub_bzl",
135+
":repo_utils_bzl",
135136
":toolchains_repo_bzl",
136137
":util_bzl",
137138
"//python:repositories_bzl",

python/private/python.bzl

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
load("@bazel_features//:features.bzl", "bazel_features")
1818
load("//python:repositories.bzl", "python_register_toolchains")
1919
load("//python:versions.bzl", "TOOL_VERSIONS")
20+
load("//python/private:repo_utils.bzl", "repo_utils")
2021
load(":pythons_hub.bzl", "hub_repo")
2122
load(":text_util.bzl", "render")
2223
load(":toolchains_repo.bzl", "multi_toolchain_aliases")
@@ -27,12 +28,6 @@ load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER")
2728
_MAX_NUM_TOOLCHAINS = 9999
2829
_TOOLCHAIN_INDEX_PAD_LENGTH = len(str(_MAX_NUM_TOOLCHAINS))
2930

30-
# Printing a warning msg not debugging, so we have to disable
31-
# the buildifier check.
32-
# buildifier: disable=print
33-
def _print_warn(msg):
34-
print("WARNING:", msg)
35-
3631
def _python_register_toolchains(name, toolchain_attr, module, ignore_root_user_error):
3732
"""Calls python_register_toolchains and returns a struct used to collect the toolchains.
3833
"""
@@ -71,6 +66,8 @@ def _python_impl(module_ctx):
7166

7267
ignore_root_user_error = None
7368

69+
logger = repo_utils.logger(module_ctx, "python")
70+
7471
# if the root module does not register any toolchain then the
7572
# ignore_root_user_error takes its default value: False
7673
if not module_ctx.modules[0].tags.toolchain:
@@ -131,11 +128,14 @@ def _python_impl(module_ctx):
131128
# version that rules_python provides as default.
132129
first = global_toolchain_versions[toolchain_version]
133130
if mod.name != "rules_python" or not first.module.is_root:
131+
# The warning can be enabled by setting the verbosity:
132+
# env RULES_PYTHON_REPO_DEBUG_VERBOSITY=INFO bazel build //...
134133
_warn_duplicate_global_toolchain_version(
135134
toolchain_version,
136135
first = first,
137136
second_toolchain_name = toolchain_name,
138137
second_module_name = mod.name,
138+
logger = logger,
139139
)
140140
toolchain_info = None
141141
else:
@@ -231,11 +231,11 @@ def _fail_duplicate_module_toolchain_version(version, module):
231231
module = module,
232232
))
233233

234-
def _warn_duplicate_global_toolchain_version(version, first, second_toolchain_name, second_module_name):
235-
_print_warn((
234+
def _warn_duplicate_global_toolchain_version(version, first, second_toolchain_name, second_module_name, logger):
235+
logger.info(lambda: (
236236
"Ignoring toolchain '{second_toolchain}' from module '{second_module}': " +
237237
"Toolchain '{first_toolchain}' from module '{first_module}' " +
238-
"already registered Python version {version} and has precedence"
238+
"already registered Python version {version} and has precedence."
239239
).format(
240240
first_toolchain = first.name,
241241
first_module = first.module.name,

0 commit comments

Comments
 (0)