Skip to content

Commit

Permalink
Tighten regex on Simplifier/strip_v2_chroot_path (#21241)
Browse files Browse the repository at this point in the history
The regex that cleans up stdout was a bit aggressive looking for pants-sandbox absolute URLs. The motivating example was `pytest` debug stdout accidentally removing `extra_env_vars` which included URLs.

This PR tightens that to well-known alphanumeric, hyphens, underscores, and forward slashes before `pants-sandbox` instead.
  • Loading branch information
sureshjoshi authored Jul 31, 2024
1 parent d89439e commit 0663929
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/python/pants/util/strutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def strip_v2_chroot_path(v: bytes | str) -> str:
"""
if isinstance(v, bytes):
v = v.decode()
return re.sub(r"/.*/pants-sandbox-[a-zA-Z0-9]+/", "", v)
return re.sub(r"/[a-zA-Z0-9-_\/]*/pants-sandbox-[a-zA-Z0-9]+/", "", v)


@dataclasses.dataclass(frozen=True)
Expand Down
12 changes: 12 additions & 0 deletions src/python/pants/util/strutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ def test_strip_chroot_path() -> None:
assert strip_v2_chroot_path("") == ""
assert strip_v2_chroot_path("hello world") == "hello world"

# Confirm other data (e.g. URLS) is unaffected
assert (
strip_v2_chroot_path("https://pantsbuild.org and /private/tmp/pants-sandbox-uPH7bl/sandbox")
== "https://pantsbuild.org and sandbox"
)
assert (
strip_v2_chroot_path(
"{'URL': 'https://pantsbuild.org', 'VENV': '/tmp/q_dt/pants-sandbox-K3/.cache/pex'}"
)
== "{'URL': 'https://pantsbuild.org', 'VENV': '.cache/pex'}"
)


@pytest.mark.parametrize(
("strip_chroot_path", "strip_formatting", "expected"),
Expand Down

0 comments on commit 0663929

Please sign in to comment.