Skip to content

Commit 2852efd

Browse files
fix(logger): do not output WARN level logs for non-root modules (#3760)
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 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> (cherry picked from commit 5511aaf)
1 parent d876cfe commit 2852efd

4 files changed

Lines changed: 25 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ END_UNRELEASED_TEMPLATE
5858
* (pypi) Fix the versions of packages that we are recording to a `MODULE.bazel.lock` file
5959
facts by passing all of the versions to the `get_index` function.
6060
Fixes [#3756](https://github.com/bazel-contrib/rules_python/issues/3756).
61+
* (bzlmod) Reduce default verbosity of our loggers for non-root modules
62+
([#3749](https://github.com/bazel-contrib/rules_python/issues/3749)).
6163

6264
{#v2-0-0}
6365
## [2.0.0] - 2026-04-09

python/private/pypi/extension.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ You cannot use both the additive_build_content and additive_build_content_file a
244244
minor_mapping = kwargs.get("minor_mapping", MINOR_MAPPING),
245245
evaluate_markers_fn = kwargs.get("evaluate_markers", None),
246246
available_interpreters = kwargs.get("available_interpreters", INTERPRETER_LABELS),
247-
logger = repo_utils.logger(module_ctx, "pypi:hub:" + hub_name),
247+
logger = repo_utils.logger(module_ctx, "pypi:hub:" + hub_name, mod = mod),
248248
)
249249
pip_hub_map[pip_attr.hub_name] = builder
250250
elif pip_hub_map[hub_name].module_name != mod.name:

python/private/python.bzl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ load(
3131
)
3232
load(":version.bzl", "version")
3333

34-
def parse_modules(*, module_ctx, logger, _fail = fail):
34+
def parse_modules(*, module_ctx, logger = None, _fail = fail):
3535
"""Parse the modules and return a struct for registrations.
3636
3737
Args:
@@ -137,7 +137,7 @@ def parse_modules(*, module_ctx, logger, _fail = fail):
137137
first = first,
138138
second_toolchain_name = toolchain_name,
139139
second_module_name = mod.name,
140-
logger = logger,
140+
logger = logger or repo_utils.logger(module_ctx, "python", mod = mod),
141141
)
142142
toolchain_info = None
143143
else:
@@ -213,8 +213,10 @@ def parse_modules(*, module_ctx, logger, _fail = fail):
213213
)
214214

215215
def _python_impl(module_ctx):
216-
logger = repo_utils.logger(module_ctx, "python")
217-
py = parse_modules(module_ctx = module_ctx, logger = logger)
216+
py = parse_modules(module_ctx = module_ctx)
217+
218+
# For all other processing (after parsing the modules) let's use a single logger.
219+
logger = repo_utils.logger(module_ctx, "python", mod = module_ctx.modules[0])
218220

219221
# Host compatible runtime repos
220222
# dict[str version, struct] where struct has:

python/private/repo_utils.bzl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _is_repo_debug_enabled(mrctx):
3131
"""
3232
return mrctx.getenv(REPO_DEBUG_ENV_VAR) == "1"
3333

34-
def _logger(mrctx = None, name = None, verbosity_level = None, printer = None):
34+
def _logger(mrctx = None, name = None, verbosity_level = None, printer = None, mod = None):
3535
"""Creates a logger instance for printing messages.
3636
3737
Args:
@@ -42,6 +42,7 @@ def _logger(mrctx = None, name = None, verbosity_level = None, printer = None):
4242
taken from `mrctx`.
4343
printer: a function to use for printing. Defaults to `print` or `fail` depending
4444
on the logging method.
45+
mod: {type}`module_ctx.module`. The module for which the logger is created.
4546
4647
Returns:
4748
A struct with attributes logging: trace, debug, info, warn, fail.
@@ -50,20 +51,30 @@ def _logger(mrctx = None, name = None, verbosity_level = None, printer = None):
5051
the logger injected into the function work as expected by terminating
5152
on the given line.
5253
"""
54+
default_verbosity_level = "WARN"
55+
if mod:
56+
if name:
57+
name = "{}:{}".format(mod.name, name)
58+
else:
59+
name = mod.name
60+
61+
if not mod.is_root:
62+
default_verbosity_level = "ERROR" # the warnings are non actionable anyway, but we should keep them.
63+
5364
if verbosity_level == None:
5465
if _is_repo_debug_enabled(mrctx):
55-
verbosity_level = "DEBUG"
56-
else:
57-
verbosity_level = "WARN"
66+
default_verbosity_level = "DEBUG"
5867

5968
env_var_verbosity = mrctx.getenv(REPO_VERBOSITY_ENV_VAR)
60-
verbosity_level = env_var_verbosity or verbosity_level
69+
verbosity_level = env_var_verbosity or default_verbosity_level
6170

6271
verbosity = {
6372
"DEBUG": 2,
73+
"ERROR": -1,
6474
"FAIL": -1,
6575
"INFO": 1,
6676
"TRACE": 3,
77+
"WARN": 0,
6778
}.get(verbosity_level, 0)
6879

6980
if hasattr(mrctx, "attr"):

0 commit comments

Comments
 (0)