Repository navigation
Python: keep Windows backslashes in LocalShellTool shell paths #9858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
341268c
1920d50
ccd0285
caa9d3d
e796d2b
6c99d30
4e30a65
d814feb
68f0e4f
6328335
378b57f
6bf44e4
eff30e9
251551e
e3c7e90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,56 @@ | |
| _ENV_OVERRIDE = "AGENT_FRAMEWORK_SHELL" | ||
|
|
||
|
|
||
| def _split_shell_override(value: str) -> list[str]: | ||
| r"""Split a shell override string into argv. | ||
|
|
||
| On POSIX, ``shlex.split`` is correct. On Windows, ``shlex.split`` treats | ||
| ``\`` as an escape and drops backslashes from unquoted paths such as | ||
| ``C:\Windows\...\powershell.exe``, which then fails with | ||
| ``FileNotFoundError``. Parse with CommandLineToArgvW-like rules: keep | ||
| backslashes literal, honour double quotes, and concatenate adjacent | ||
| quoted and unquoted fragments (so ``"C:/Program Files"/PowerShell/7/pwsh.exe`` | ||
| stays one argv element). | ||
| """ | ||
| import shlex | ||
|
|
||
| if sys.platform != "win32": | ||
| return shlex.split(value) | ||
|
|
||
| args: list[str] = [] | ||
| buf: list[str] = [] | ||
| in_quotes = False | ||
| i = 0 | ||
| n = len(value) | ||
| while i < n: | ||
| c = value[i] | ||
| if in_quotes: | ||
| if c == '"': | ||
| in_quotes = False | ||
| else: | ||
| buf.append(c) | ||
| else: | ||
| if c == '"': | ||
| in_quotes = True | ||
| elif c in " ": | ||
| if buf: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This drops an explicitly quoted empty argument because |
||
| args.append("".join(buf)) | ||
| buf = [] | ||
| else: | ||
| buf.append(c) | ||
| i += 1 | ||
| if buf: | ||
| args.append("".join(buf)) | ||
| return args | ||
|
|
||
|
|
||
| def resolve_shell(shell: str | Sequence[str] | None, *, interactive: bool) -> list[str]: | ||
| """Resolve the shell invocation argv. | ||
|
|
||
| Priority: | ||
|
|
||
| 1. Explicit ``shell`` argument (string is split via shlex rules; sequence | ||
| is used verbatim). | ||
| 1. Explicit ``shell`` argument (string is split with platform-aware rules; | ||
| sequence is used verbatim). | ||
| 2. ``AGENT_FRAMEWORK_SHELL`` environment variable. | ||
| 3. Platform default. | ||
|
|
||
|
|
@@ -39,9 +82,7 @@ def resolve_shell(shell: str | Sequence[str] | None, *, interactive: bool) -> li | |
| """ | ||
| if shell is not None: | ||
| if isinstance(shell, str): | ||
| import shlex | ||
|
|
||
| parts = shlex.split(shell) | ||
| parts = _split_shell_override(shell) | ||
| if not parts: | ||
| raise ShellExecutionError("shell override must not be empty") | ||
| else: | ||
|
|
@@ -52,9 +93,7 @@ def resolve_shell(shell: str | Sequence[str] | None, *, interactive: bool) -> li | |
|
|
||
| env_override = os.environ.get(_ENV_OVERRIDE) | ||
| if env_override: | ||
| import shlex | ||
|
|
||
| parts = shlex.split(env_override) | ||
| parts = _split_shell_override(env_override) | ||
| if parts: | ||
| return parts if interactive else _ensure_command_flag(parts) | ||
|
|
||
|
|
@@ -82,13 +121,17 @@ def resolve_shell(shell: str | Sequence[str] | None, *, interactive: bool) -> li | |
|
|
||
|
|
||
| def is_powershell(argv: Sequence[str]) -> bool: | ||
| """Return True when ``argv[0]`` appears to be PowerShell.""" | ||
| """Return True when ``argv[0]`` appears to be PowerShell. | ||
|
|
||
| Normalize backslashes to ``/`` before ``basename`` so win32-style | ||
| paths still detect correctly when this helper runs on POSIX (unit | ||
| tests monkeypatch ``sys.platform`` to ``win32`` on Linux CI). | ||
| """ | ||
| if not argv: | ||
| return False | ||
| name = os.path.basename(argv[0]).lower() | ||
| name = os.path.basename(argv[0].replace("\\", "/")).lower() | ||
| return name in {"pwsh", "pwsh.exe", "powershell", "powershell.exe"} | ||
|
|
||
|
|
||
| def _ensure_command_flag(argv: list[str]) -> list[str]: | ||
| """Append the right ``-c`` / ``-Command`` flag for stateless argv. | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Escaped quotes still close the quoted segment here. A valid Windows command line can contain
\"inside a quoted startup script; treating that quote as a delimiter drops the literal quote and can split one argument into several. Please handle Windows backslash-before-quote rules, including odd/even backslash counts, or use a compatible parser, and add a regression with an escaped quote inside a quoted argument.