Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions python/private/python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Comment on lines +218 to 219
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logger is created without passing the mod argument, which means it will default to WARN verbosity even when the extension is being used in a non-root module context. To be consistent with the goal of reducing log spam for non-root modules, this logger should also respect the root vs. non-root status.

Since module_ctx.modules[0] is guaranteed to be the root module if it uses the extension (and a non-root module otherwise), passing it as the mod argument will correctly set the default verbosity for the global extension processing. Also, there is a minor typo in the comment (missing space).

Suggested change
# For all other processing(after parsing the modules) let's use a single logger.
logger = repo_utils.logger(module_ctx, "python")
# For all other processing (after parsing the modules) let's use a single logger.
logger = repo_utils.logger(module_ctx, "python", mod = module_ctx.modules[0])

py = parse_modules(module_ctx = module_ctx, logger = logger)

# Host compatible runtime repos
# dict[str version, struct] where struct has:
Expand Down
19 changes: 14 additions & 5 deletions python/private/repo_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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,
Comment on lines 71 to 72
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The verbosity mapping is missing entries for "ERROR" and "WARN". Because of the .get(verbosity_level, 0) call on line 76, both "ERROR" and "WARN" currently resolve to a verbosity of 0. Since the warn method (line 108) is enabled when the verbosity is >= 0, setting the default level to "ERROR" for non-root modules (line 62) does not actually silence the warnings.

To fix this, "ERROR" should be mapped to -1 (to match "FAIL"), which will correctly silence warnings (level 0) while still allowing failures to be reported. Additionally, "WARN" should be explicitly mapped to 0 for clarity.

Suggested change
verbosity = {
"DEBUG": 2,
verbosity = {
"DEBUG": 2,
"ERROR": -1,
"WARN": 0,

Expand Down