Skip to content
Merged
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
4 changes: 4 additions & 0 deletions news/4441.fixed.md
Original file line number Diff line number Diff line change
@@ -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)).
27 changes: 27 additions & 0 deletions python/private/stage2_bootstrap_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import runpy
import types
import uuid
import warnings
from functools import cache

# ===== Template substitutions start =====
Expand Down Expand Up @@ -585,4 +586,30 @@ 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):
# 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()
15 changes: 15 additions & 0 deletions tests/bootstrap_impls/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
54 changes: 54 additions & 0 deletions tests/bootstrap_impls/relative_formatwarning_test.py
Original file line number Diff line number Diff line change
@@ -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()
Loading