From 6c461904863d056a14f1b1d8b95f85040d2b7e12 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Fri, 8 May 2026 12:48:50 +0900 Subject: [PATCH] fix(logger): do not output WARN level logs for non-root modules A user has reported issues which actually resulted from a particular `bazel` version being used and once he updated, the issues disappeared because of changes in implicit non-root module dependencies. In order to reduce the warning spam of non-root modules, set the default level to `ERROR` for non-root module parsing context and `WARN` for root module context. This means that the users will not get console output that they have no way to fix without patching upstream dependencies. At the same time ensure that the name of the module is printed together with the logs to better understand where something is coming from. Fixes #3749 --- CHANGELOG.md | 2 ++ python/private/pypi/extension.bzl | 2 +- python/private/python.bzl | 8 +++++--- python/private/repo_utils.bzl | 19 ++++++++++++++----- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c102d3b04e..d45eca1322 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,8 @@ END_UNRELEASED_TEMPLATE * (pypi) Fix `importlib.metadata.files` by ensuring `RECORD` is included in installed wheel targets, except when built from sdist ([#3024](https://github.com/bazel-contrib/rules_python/issues/3024)). +* (bzlmod) Reduce default verbosity of our loggers for non-root modules + ([#3749](https://github.com/bazel-contrib/rules_python/issues/3749)). {#v0-0-0-added} diff --git a/python/private/pypi/extension.bzl b/python/private/pypi/extension.bzl index e6052782fa..2a8ace28f2 100644 --- a/python/private/pypi/extension.bzl +++ b/python/private/pypi/extension.bzl @@ -358,7 +358,7 @@ You cannot use both the additive_build_content and additive_build_content_file a minor_mapping = kwargs.get("minor_mapping", MINOR_MAPPING), evaluate_markers_fn = kwargs.get("evaluate_markers", None), available_interpreters = kwargs.get("available_interpreters", INTERPRETER_LABELS), - logger = repo_utils.logger(module_ctx, "pypi:hub:" + hub_name), + logger = repo_utils.logger(module_ctx, "pypi:hub:" + hub_name, mod = mod), ) pip_hub_map[pip_attr.hub_name] = builder elif pip_hub_map[hub_name].module_name != mod.name: diff --git a/python/private/python.bzl b/python/private/python.bzl index 2ea757892e..7d38652e19 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -31,7 +31,7 @@ load( ) load(":version.bzl", "version") -def parse_modules(*, module_ctx, logger, _fail = fail): +def parse_modules(*, module_ctx, logger = None, _fail = fail): """Parse the modules and return a struct for registrations. Args: @@ -137,7 +137,7 @@ def parse_modules(*, module_ctx, logger, _fail = fail): first = first, second_toolchain_name = toolchain_name, second_module_name = mod.name, - logger = logger, + logger = logger or repo_utils.logger(module_ctx, "python", mod = mod), ) toolchain_info = None else: @@ -213,8 +213,10 @@ def parse_modules(*, module_ctx, logger, _fail = fail): ) def _python_impl(module_ctx): + py = parse_modules(module_ctx = module_ctx) + + # For all other processing(after parsing the modules) let's use a single logger. logger = repo_utils.logger(module_ctx, "python") - py = parse_modules(module_ctx = module_ctx, logger = logger) # Host compatible runtime repos # dict[str version, struct] where struct has: diff --git a/python/private/repo_utils.bzl b/python/private/repo_utils.bzl index ae2fc2e5d0..33783f8efc 100644 --- a/python/private/repo_utils.bzl +++ b/python/private/repo_utils.bzl @@ -31,7 +31,7 @@ def _is_repo_debug_enabled(mrctx): """ return mrctx.getenv(REPO_DEBUG_ENV_VAR) == "1" -def _logger(mrctx = None, name = None, verbosity_level = None, printer = None): +def _logger(mrctx = None, name = None, verbosity_level = None, printer = None, mod = None): """Creates a logger instance for printing messages. Args: @@ -42,6 +42,7 @@ def _logger(mrctx = None, name = None, verbosity_level = None, printer = None): taken from `mrctx`. printer: a function to use for printing. Defaults to `print` or `fail` depending on the logging method. + mod: {type}`module_ctx.module`. The module for which the logger is created. Returns: A struct with attributes logging: trace, debug, info, warn, fail. @@ -50,14 +51,22 @@ def _logger(mrctx = None, name = None, verbosity_level = None, printer = None): the logger injected into the function work as expected by terminating on the given line. """ + default_verbosity_level = "WARN" + if mod: + if name: + name = "{}:{}".format(mod.name, name) + else: + name = mod.name + + if not mod.is_root: + default_verbosity_level = "ERROR" # the warnings are non actionable anyway, but we should keep them. + if verbosity_level == None: if _is_repo_debug_enabled(mrctx): - verbosity_level = "DEBUG" - else: - verbosity_level = "WARN" + default_verbosity_level = "DEBUG" env_var_verbosity = mrctx.getenv(REPO_VERBOSITY_ENV_VAR) - verbosity_level = env_var_verbosity or verbosity_level + verbosity_level = env_var_verbosity or default_verbosity_level verbosity = { "DEBUG": 2,