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

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.

---------

Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
diff --git a/news/4441.fixed.md b/news/4441.fixed.md
new file mode 100644
index 0000000..8180fa4
--- /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)).
diff --git a/python/private/stage2_bootstrap_template.py b/python/private/stage2_bootstrap_template.py
index f445ad2..1094b36 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,30 @@
         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):
+    # 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
+
 main()
diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel
index f5c8c1f..eda0e73 100644
--- a/tests/bootstrap_impls/BUILD.bazel
+++ b/tests/bootstrap_impls/BUILD.bazel
@@ -149,6 +149,21 @@
 )
 
 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"],
     bootstrap_impl = "system_python",
diff --git a/tests/bootstrap_impls/relative_formatwarning_test.py b/tests/bootstrap_impls/relative_formatwarning_test.py
new file mode 100644
index 0000000..0ae1876
--- /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()