Skip to content

The agent that grows with you

fix(files): scope backslash compensation to local Windows

ethernet948c9dbfc0b0
diff --git a/tests/tools/test_shell_arg_transport.py b/tests/tools/test_shell_arg_transport.py
new file mode 100644
index 00000000000..56a9d0c95fb
--- /dev/null
+++ b/tests/tools/test_shell_arg_transport.py
@@ -0,0 +1,49 @@
+"""Non-path values survive local argv and serialized POSIX-script transport."""
+import json
+import os
+import subprocess
+
+import pytest
+
+from tools.environments.local import LocalEnvironment, _find_bash
+from tools.file_operations import ShellFileOperations
+
+
+class _SerializedShell:
+ is_local = False
+
+ def __init__(self, bash, cwd, env):
+ self.bash, self.cwd, self.env = bash, str(cwd), env
+
+ def execute(self, command, *, cwd=None, timeout=30, **kwargs):
+ # Deliver command text on stdin, without Windows command-line parsing.
+ script = json.loads(json.dumps({'command': command}))['command']
+ result = subprocess.run([self.bash, '--noprofile', '--norc', '-s'],
+ input=script, cwd=cwd or self.cwd, env=self.env,
+ capture_output=True, text=True, encoding='utf-8', timeout=timeout)
+ return {'output': result.stdout, 'returncode': result.returncode}
+
+
[email protected]('serialized', [False, True])
+def test_non_path_bytes_survive_real_shell_transport(tmp_path, monkeypatch, serialized):
+ home = tmp_path / 'home'
+ home.mkdir()
+ monkeypatch.setenv('HERMES_HOME', str(home))
+ monkeypatch.setenv('HERMES_RUNTIME_DIR', str(tmp_path / 'store'))
+ monkeypatch.setenv('HOME', str(home))
+ env = {**os.environ, 'BASH_ENV': '', 'ENV': ''}
+ bash = _find_bash()
+ backend = (_SerializedShell(bash, tmp_path, env) if serialized
+ else LocalEnvironment(cwd=str(tmp_path), timeout=30, env=env))
+ ops = ShellFileOperations(backend)
+ try:
+ for value in ('', 'two words', "quote'and\"quote", '$() ` : ;',
+ 'line\nbreak', *('a' + '\\' * length + '.b' for length in (1, 2, 3, 4, 8)),
+ "a\\'b"):
+ quoted = ops._escape_shell_arg(value, translate_path=False)
+ result = ops._exec(f"printf '%s' {quoted} | od -An -v -tx1")
+ assert result.exit_code == 0, (value, result.stdout)
+ assert bytes.fromhex(result.stdout) == value.encode('utf-8'), (serialized, value, result.stdout)
+ finally:
+ if isinstance(backend, LocalEnvironment):
+ backend.cleanup()
diff --git a/tools/file_operations.py b/tools/file_operations.py
index b9c45851f56..5b6571f1cf8 100644
--- a/tools/file_operations.py
+++ b/tools/file_operations.py
@@ -351,14 +351,14 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):
agree on the path form. No-op off Windows and for plain POSIX paths.
``translate_path=False`` skips that translation for non-path values
- (regex patterns) whose backslashes are meaningful. Bash single
- quotes preserve those bytes; only path arguments are translated.
+ such as regex patterns. Backslash compensation applies only to the
+ local Windows argv transport. Serialized shell text stays literal.
"""
from tools.environments.local import _IS_WINDOWS, _bash_safe_path
if translate_path:
arg = _bash_safe_path(arg)
- elif _IS_WINDOWS:
+ elif _IS_WINDOWS and getattr(self.env, "is_local", False):
arg = arg.replace("\\", "\\\\")
# Use single quotes and escape any single quotes in the string
return "'" + arg.replace("'", "'\"'\"'") + "'"