Skip to content

The agent that grows with you

fix(terminal): relative spawn PATH entries survive a resetting login profile

Teknium35c2783b6ab7
diff --git a/tests/tools/test_login_shell_path_preservation.py b/tests/tools/test_login_shell_path_preservation.py
index 397bbd4480c..f2451a2ae09 100644
--- a/tests/tools/test_login_shell_path_preservation.py
+++ b/tests/tools/test_login_shell_path_preservation.py
@@ -114,22 +114,25 @@ def test_dropped_spawn_path_dirs_return_after_the_login_path(tmp_path, monkeypat
"""Any spawn-env dir a resetting profile dropped (not only Hermes' own: the gateway's
pm runtime, a ``docker run -e PATH=`` addition) comes back in parent order AFTER the
login PATH, so a command the login PATH resolves keeps resolving to the same binary."""
- user_bin, extra_a, extra_b = (tmp_path / n for n in ("user-bin", "extra-a", "extra-b"))
- for d in (user_bin, extra_a, extra_b):
+ user_bin, extra_a, extra_b, rel_bin = (tmp_path / n for n in ("user-bin", "extra-a", "extra-b", "rel-bin"))
+ for d in (user_bin, extra_a, extra_b, rel_bin):
d.mkdir()
_exe(user_bin, "shared-tool")
_exe(extra_a, "shared-tool")
_exe(extra_b, "only-in-parent")
+ _exe(rel_bin, "only-relative")
_managed_setup(tmp_path, monkeypatch,
profile_body=f'PATH="/usr/local/bin:/usr/bin:/bin"\nPATH="{user_bin}:$PATH"\n')
- env = LocalEnvironment(cwd=str(tmp_path), env={"PATH": f"{extra_a}:{extra_b}:/usr/bin:/bin"})
+ # A relative entry resolves against the terminal's cwd, not this process's.
+ monkeypatch.chdir(user_bin)
+ env = LocalEnvironment(cwd=str(tmp_path), env={"PATH": f"{extra_a}:{extra_b}:rel-bin:/usr/bin:/bin"})
try:
entries = _snapshot_path_value(env)
assert entries.index(str(user_bin)) < entries.index("/usr/bin") < entries.index(str(extra_a)) \
- < entries.index(str(extra_b))
+ < entries.index(str(extra_b)) < entries.index("rel-bin")
assert entries.count("/usr/bin") == 1
- out = env.execute("only-in-parent; shared-tool")["output"].split()
- assert out == ["extra-b", "user-bin"]
+ out = env.execute("only-in-parent; shared-tool; only-relative")["output"].split()
+ assert out == ["extra-b", "user-bin", "rel-bin"]
finally:
env.cleanup()
diff --git a/tools/environments/local.py b/tools/environments/local.py
index 3e0b263aa88..1dc153c38bc 100644
--- a/tools/environments/local.py
+++ b/tools/environments/local.py
@@ -871,12 +871,14 @@ def _append_dropped_path_entries(cmd_string: str, parent_path: str) -> str:
``/opt/hermes/bin`` and bare ``hermes`` exits 127 (#56634). Dropped dirs go AFTER
the login PATH in parent order, so a command that resolved before still resolves
to the same binary (the user's nvm/pyenv keep precedence); only commands that had
- no match gain one. Present entries are never duplicated; missing dirs are skipped.
+ no match gain one. Present entries are never duplicated; missing absolute dirs are skipped.
Background commands (``ProcessRegistry.spawn_local``, ``bash -lic``, no snapshot) use
this same prelude so the two paths cannot diverge."""
if _IS_WINDOWS:
return cmd_string # native PATH stays ``;``-joined; bash sees a converted copy
- entries = [d for d in dict.fromkeys(parent_path.split(":")) if d and os.path.isdir(d)]
+ # Relative entries resolve against the shell's cwd, not ours, so only absolute ones are checked.
+ entries = [d for d in dict.fromkeys(parent_path.split(":"))
+ if d and (not os.path.isabs(d) or os.path.isdir(d))]
if not entries:
return cmd_string
restore = (f"for __hermes_p in {' '.join(shlex.quote(d) for d in entries)}; do "