Skip to content

fix: wrap formatwarning() to use relative paths - #4441

Merged
aignas merged 3 commits into
bazel-contrib:mainfrom
dws:relative-formatwarning
Nov 15, 2026
Merged

aignas merged 3 commits into
bazel-contrib:mainfrom
dws:relative-formatwarning

Conversation

@dws

@dws dws commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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.

@dws
dws marked this pull request as ready for review November 8, 2026 18:01
@dws
dws requested review from aignas and rickeylev as code owners 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.
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
aignas enabled auto-merge November 15, 2026 13:22
@aignas

aignas commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

I personally think it belongs in py_test runners, we don't have unittest runner, but we do have pytest-bazel. I'd say that we could merge this since this is for format warning.

Thinking out loud, maybe we should modify it:

  • When we are running under bazel test or bazel build (genrules or py_binary as tool) then we should print those warnings like this.
  • When we are running standalone or bazel run, we should not alter the warnings for easier debugging?

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
aignas added this pull request to the merge queue Nov 15, 2026
Merged via the queue into bazel-contrib:main with commit 07acc9a Nov 15, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants