Skip to content

Commit

Permalink
fix(bzlmod): silence duplicate version registrations (bazelbuild#2111)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aignas authored Aug 6, 2024
1 parent fa13b01 commit 79478de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ A brief description of the categories of changes:
[#2091](https://github.com/bazelbuild/rules_python/issues/2090).
* (gazelle) Make `gazelle_python_manifest.update` manual to avoid unnecessary
network behavior.
* (bzlmod): The conflicting toolchains during `python` extension will no longer
cause warnings by default. In order to see the warnings for diagnostic purposes
set the env var `RULES_PYTHON_REPO_DEBUG_VERBOSITY` to one of `INFO`, `DEBUG` or `TRACE`.
Fixes [#1818](https://github.com/bazelbuild/rules_python/issues/1818).

### Added
* (rules) `PYTHONSAFEPATH` is inherited from the calling environment to allow
Expand Down
1 change: 1 addition & 0 deletions python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ bzl_library(
srcs = ["python.bzl"],
deps = [
":pythons_hub_bzl",
":repo_utils_bzl",
":toolchains_repo_bzl",
":util_bzl",
"//python:repositories_bzl",
Expand Down
18 changes: 9 additions & 9 deletions python/private/python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
load("@bazel_features//:features.bzl", "bazel_features")
load("//python:repositories.bzl", "python_register_toolchains")
load("//python:versions.bzl", "TOOL_VERSIONS")
load("//python/private:repo_utils.bzl", "repo_utils")
load(":pythons_hub.bzl", "hub_repo")
load(":text_util.bzl", "render")
load(":toolchains_repo.bzl", "multi_toolchain_aliases")
Expand All @@ -27,12 +28,6 @@ load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER")
_MAX_NUM_TOOLCHAINS = 9999
_TOOLCHAIN_INDEX_PAD_LENGTH = len(str(_MAX_NUM_TOOLCHAINS))

# Printing a warning msg not debugging, so we have to disable
# the buildifier check.
# buildifier: disable=print
def _print_warn(msg):
print("WARNING:", msg)

def _python_register_toolchains(name, toolchain_attr, module, ignore_root_user_error):
"""Calls python_register_toolchains and returns a struct used to collect the toolchains.
"""
Expand Down Expand Up @@ -71,6 +66,8 @@ def _python_impl(module_ctx):

ignore_root_user_error = None

logger = repo_utils.logger(module_ctx, "python")

# if the root module does not register any toolchain then the
# ignore_root_user_error takes its default value: False
if not module_ctx.modules[0].tags.toolchain:
Expand Down Expand Up @@ -131,11 +128,14 @@ def _python_impl(module_ctx):
# version that rules_python provides as default.
first = global_toolchain_versions[toolchain_version]
if mod.name != "rules_python" or not first.module.is_root:
# The warning can be enabled by setting the verbosity:
# env RULES_PYTHON_REPO_DEBUG_VERBOSITY=INFO bazel build //...
_warn_duplicate_global_toolchain_version(
toolchain_version,
first = first,
second_toolchain_name = toolchain_name,
second_module_name = mod.name,
logger = logger,
)
toolchain_info = None
else:
Expand Down Expand Up @@ -231,11 +231,11 @@ def _fail_duplicate_module_toolchain_version(version, module):
module = module,
))

def _warn_duplicate_global_toolchain_version(version, first, second_toolchain_name, second_module_name):
_print_warn((
def _warn_duplicate_global_toolchain_version(version, first, second_toolchain_name, second_module_name, logger):
logger.info(lambda: (
"Ignoring toolchain '{second_toolchain}' from module '{second_module}': " +
"Toolchain '{first_toolchain}' from module '{first_module}' " +
"already registered Python version {version} and has precedence"
"already registered Python version {version} and has precedence."
).format(
first_toolchain = first.name,
first_module = first.module.name,
Expand Down

0 comments on commit 79478de

Please sign in to comment.