From f426e991857fe63dfdef8cc5d5f8603529e1f65e Mon Sep 17 00:00:00 2001 From: David Sanderson <32687193+dws@users.noreply.github.com> Date: Sun, 8 Nov 2026 11:41:10 -0500 Subject: [PATCH 1/3] fix: wrap formatwarning() to use relative paths 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. --- python/private/stage2_bootstrap_template.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/python/private/stage2_bootstrap_template.py b/python/private/stage2_bootstrap_template.py index f445ad2b6a..ef754483b8 100644 --- a/python/private/stage2_bootstrap_template.py +++ b/python/private/stage2_bootstrap_template.py @@ -27,6 +27,7 @@ import runpy import types import uuid +import warnings from functools import cache # ===== Template substitutions start ===== @@ -585,4 +586,24 @@ def main(): sys.exit(0) +# 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. + +original_formatwarning = warnings.formatwarning +original_cwd = os.getcwd() + + +def relpath_formatwarning(message, category, filename, lineno, line=None): + relative_filename = ( + os.path.relpath(filename, original_cwd) if os.path.isabs(filename) else filename + ) + return original_formatwarning( + message, category, relative_filename, lineno, line=line + ) + + +warnings.formatwarning = relpath_formatwarning + main() From 1d7228e20d6b36405d4b985b718be41e0e1a9ed7 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:09:56 +0900 Subject: [PATCH 2/3] add news entry --- news/4441.fixed.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 news/4441.fixed.md diff --git a/news/4441.fixed.md b/news/4441.fixed.md new file mode 100644 index 0000000000..8180fa4ece --- /dev/null +++ b/news/4441.fixed.md @@ -0,0 +1,4 @@ +(binaries/tests) Warnings printed by `py_binary` and `py_test` now show file +paths relative to the working directory at launch instead of absolute paths, +which keeps the output deterministic, e.g. under remote execution +([#4441](https://github.com/bazel-contrib/rules_python/pull/4441)). From c7020f4cf22e4c0911184f67287a6869b4e6be8f Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:19:32 +0900 Subject: [PATCH 3/3] fix: only make paths within the cwd relative in warnings 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. --- python/private/stage2_bootstrap_template.py | 18 ++++--- tests/bootstrap_impls/BUILD.bazel | 15 ++++++ .../relative_formatwarning_test.py | 54 +++++++++++++++++++ 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 tests/bootstrap_impls/relative_formatwarning_test.py diff --git a/python/private/stage2_bootstrap_template.py b/python/private/stage2_bootstrap_template.py index ef754483b8..1094b36704 100644 --- a/python/private/stage2_bootstrap_template.py +++ b/python/private/stage2_bootstrap_template.py @@ -596,12 +596,18 @@ def main(): def relpath_formatwarning(message, category, filename, lineno, line=None): - relative_filename = ( - os.path.relpath(filename, original_cwd) if os.path.isabs(filename) else filename - ) - return original_formatwarning( - message, category, relative_filename, lineno, line=line - ) + # Only paths within the directory are made relative, because the + # number of `..` in the path to e.g. the stdlib would depend on the + # depth of the directory. + try: + if os.path.isabs(filename) and os.path.normcase( + os.path.commonpath([filename, original_cwd]) + ) == os.path.normcase(original_cwd): + filename = os.path.relpath(filename, original_cwd) + except ValueError: + # On Windows, paths on different drives don't have a common path. + pass + return original_formatwarning(message, category, filename, lineno, line=line) warnings.formatwarning = relpath_formatwarning diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel index f5c8c1fe82..eda0e73161 100644 --- a/tests/bootstrap_impls/BUILD.bazel +++ b/tests/bootstrap_impls/BUILD.bazel @@ -148,6 +148,21 @@ py_test( deps = ["//python/runfiles"], ) +py_reconfig_test( + name = "relative_formatwarning_bootstrap_script_test", + srcs = ["relative_formatwarning_test.py"], + bootstrap_impl = "script", + main = "relative_formatwarning_test.py", + target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT, +) + +py_reconfig_test( + name = "relative_formatwarning_bootstrap_system_python_test", + srcs = ["relative_formatwarning_test.py"], + bootstrap_impl = "system_python", + main = "relative_formatwarning_test.py", +) + py_reconfig_test( name = "sys_path_order_bootstrap_system_python_test", srcs = ["sys_path_order_test.py"], diff --git a/tests/bootstrap_impls/relative_formatwarning_test.py b/tests/bootstrap_impls/relative_formatwarning_test.py new file mode 100644 index 0000000000..0ae1876161 --- /dev/null +++ b/tests/bootstrap_impls/relative_formatwarning_test.py @@ -0,0 +1,54 @@ +import os +import string +import sys +import unittest +import warnings + + +class RelativeFormatwarningTest(unittest.TestCase): + def format(self, filename): + return warnings.formatwarning("msg", UserWarning, filename, 1) + + def test_formatwarning_is_wrapped(self): + self.assertEqual(warnings.formatwarning.__name__, "relpath_formatwarning") + + def test_path_within_cwd_is_relative(self): + filename = os.path.join(os.getcwd(), "pkg", "mod.py") + expected = os.path.join("pkg", "mod.py") + self.assertTrue(self.format(filename).startswith(expected + ":1: ")) + + def test_path_outside_cwd_is_absolute(self): + # e.g. a warning from the stdlib + filename = os.path.join(os.path.dirname(os.getcwd()), "other", "mod.py") + self.assertTrue(self.format(filename).startswith(filename + ":1: ")) + + def test_relative_path_is_unchanged(self): + filename = os.path.join("pkg", "mod.py") + self.assertTrue(self.format(filename).startswith(filename + ":1: ")) + + def test_warn_from_file_within_cwd(self): + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + warnings.warn("msg") + warning = caught[0] + formatted = warnings.formatwarning( + warning.message, warning.category, warning.filename, warning.lineno + ) + # The test runs from the runfiles directory that contains this file. + expected = os.path.relpath(warning.filename, os.getcwd()) + self.assertFalse(expected.startswith(".."), expected) + self.assertTrue(formatted.startswith(expected + ":"), formatted) + + @unittest.skipUnless(sys.platform == "win32", "drive letters are Windows-only") + def test_path_on_other_drive_is_absolute(self): + cwd_drive = os.path.splitdrive(os.getcwd())[0].upper() + drive = next( + d + ":" for d in reversed(string.ascii_uppercase) if d + ":" != cwd_drive + ) + filename = drive + "\\other\\mod.py" + # This raised ValueError, because relpath() can't cross drives. + self.assertTrue(self.format(filename).startswith(filename + ":1: ")) + + +if __name__ == "__main__": + unittest.main()