Repository navigation
fix: wrap formatwarning() to use relative paths - #4441
Merged
Merged
Conversation
dws
marked this pull request as ready for review
November 8, 2026 18:01
To help eliminate nondeterministic output, particularly when remotely executing, we here wrap Python's formatwarning() to cause it to produce paths relative to the current directory as of the launch of the executable.
dws
force-pushed
the
relative-formatwarning
branch
from
November 8, 2026 18:55
0f67dc5 to
f426e99
Compare
aignas
approved these changes
Nov 15, 2026
os.path.relpath() raises ValueError on Windows for paths on different drives, which would make warnings.warn() fail, and paths outside of the cwd, e.g. to the stdlib, would get a number of .. that depends on the depth of the cwd.
aignas
enabled auto-merge
November 15, 2026 13:22
Collaborator
|
I personally think it belongs in Thinking out loud, maybe we should modify it:
WDYT? |
aignas
added a commit
to aignas/pytest-bazel
that referenced
this pull request
Nov 15, 2026
pytest's warnings summary and the TEST_WARNINGS_OUTPUT_FILE showed the absolute path of the file that issued a warning. Under bazel that path is deep in a sandbox that is gone after the test, so an editor cannot open it, while the tracebacks already use paths relative to the runfiles root. While pytest runs, wrap `warnings.formatwarning` so that the files in the runfiles tree are shown relative to the runfiles root, which for the main repository is the same as the workspace root. This is the same idea as bazel-contrib/rules_python#4441. Add a test that runs a failing sample through pytest_bazel and checks that every code location in the report is relative and exists.
aignas
added a commit
to aignas/pytest-bazel
that referenced
this pull request
Nov 15, 2026
pytest's warnings summary and the TEST_WARNINGS_OUTPUT_FILE showed the absolute path of the file that issued a warning. Under bazel that path is deep in a sandbox that is gone after the test, so an editor cannot open it, while the tracebacks already use paths relative to the runfiles root. While pytest runs, wrap `warnings.formatwarning` so that the files in the runfiles tree are shown relative to the runfiles root, which for the main repository is the same as the workspace root. This is the same idea as bazel-contrib/rules_python#4441. Add a test that runs a failing sample through pytest_bazel and checks that every code location in the report is relative and exists. The path is made relative with the lexical `PurePath.relative_to` rather than `os.path.relpath`, which raises for paths on different drives on Windows. Anything outside of the runfiles tree, e.g. another drive, a UNC path or `<string>`, is kept as it is, so that formatting a warning never fails because of its path. Only do this under `bazel test`, i.e. when BAZEL_TEST is set. With `bazel run` or without bazel, the absolute paths exist and work from any directory, while the relative ones only work from the workspace root. Document it in docs/features/warnings.md.
aignas
approved these changes
Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
To help eliminate nondeterministic output, particularly when remotely executing, we here wrap Python's formatwarning() to cause it to produce paths relative to the current directory as of the launch of the executable.
I'm proposing this here, since I'm not sure whether this change in behavior would be accepted in core Python, since it's intended to help produce deterministic output under remote execution under Bazel. We've been patching this into our rules_python for some time and have not observed any problems with it.