fix(bzlmod): silence duplicate version registrations (#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 #1818.
diff --git a/CHANGELOG.md b/CHANGELOG.md index a2e60f7..5f11f7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md
@@ -55,6 +55,10 @@ [#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
diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index 3b97a02..146e934 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel
@@ -132,6 +132,7 @@ srcs = ["python.bzl"], deps = [ ":pythons_hub_bzl", + ":repo_utils_bzl", ":toolchains_repo_bzl", ":util_bzl", "//python:repositories_bzl",
diff --git a/python/private/python.bzl b/python/private/python.bzl index ce00a7b..6a265d1 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl
@@ -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") @@ -27,12 +28,6 @@ _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. """ @@ -71,6 +66,8 @@ 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: @@ -131,11 +128,14 @@ # 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: @@ -231,11 +231,11 @@ 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,