Skip to content

fix(windows): preserve quotes in cmd commands (start_process)#495

Merged
wonderwhy-er merged 1 commit into
mainfrom
fix/win-cmd-quote-args
Jun 5, 2026
Merged

fix(windows): preserve quotes in cmd commands (start_process)#495
wonderwhy-er merged 1 commit into
mainfrom
fix/win-cmd-quote-args

Conversation

@wonderwhy-er

@wonderwhy-er wonderwhy-er commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Problem

On Windows, start_process corrupts any command containing double quotes when the shell is cmd. Examples that fail today:

  • dir "C:\Program Files\Mozilla Firefox\firefox.exe" -> The filename, directory name, or volume label syntax is incorrect.
  • tasklist /fi "imagename eq firefox.exe" -> ERROR: Invalid argument/option - 'eq'.
  • start "" "C:\Program Files\Mozilla Firefox\firefox.exe" -> silently fails to launch

Minimal repro:

echo "hello world" a"b"c
# got:  \"hello world\" a\"b\"c

Root cause

In terminal-manager.ts, the cmd branch of getShellSpawnArgs returns { executable: 'cmd', args: ['/c', command], useShellOption: false }, and executeCommand calls spawn(executable, args, opts) without shell: true. Node/libuv then builds the Windows command line using MSVCRT-style quoting, escaping embedded " as \". cmd.exe does not treat \" as an escape, so the quotes reach cmd mangled and the command breaks. (Node only disables this escaping automatically when shell: true is used, which DC deliberately avoids so it can control login flags.)

Fix

Set windowsVerbatimArguments = true for the cmd branch, so cmd parses its own quoting -- exactly what Node does internally for shell: true.

Scoped to cmd only via a new ShellSpawnConfig.windowsVerbatim flag:

  • cmd -> verbatim on (fixed)
  • PowerShell / pwsh -> unchanged. Verbatim is wrong for them: it splits a quoted argument like "hello world" into separate tokens. They keep their existing behaviour.
  • bash / zsh / fish (incl. WSL) -> unaffected; windowsVerbatimArguments is a no-op outside win32, and these already pass the command as a clean -c argument.
  • macOS / Linux -> no behaviour change at all (guarded by process.platform === 'win32').

Testing

Verified on a live local build (Windows 11, Node 24, cmd default shell):

Command (cmd) Before After
echo "hello world" a"b"c \"hello world\" a\"b\"c "hello world" a"b"c
dir "C:\Program Files\Mozilla Firefox\firefox.exe" syntax error lists the file
tasklist /fi "imagename eq explorer.exe" Invalid argument 'eq' works
start "" "C:\Program Files\Mozilla Firefox\firefox.exe" nothing Firefox launches

PowerShell re-checked after scoping: echo "hello world" returns hello world on a single line (correct), confirming no regression.

Summary by CodeRabbit

Bug Fixes

  • Enhanced command execution handling on Windows systems to improve reliability and compatibility with the Windows command environment.

On Windows, start_process spawns the shell executable directly
(`cmd /c <command>`) with useShellOption=false. Node/libuv then applies
its default MSVCRT-style argument quoting, which escapes embedded double
quotes as \". cmd.exe does not treat \" as an escape, so every quoted
command was corrupted before cmd parsed it -- e.g. a quoted path with
spaces ("C:\Program Files\app.exe") or `tasklist /fi "imagename eq x"`
would fail with "syntax incorrect" / "Invalid argument".

Set windowsVerbatimArguments=true for the cmd branch so cmd parses its
own quoting (the same thing Node does internally for `shell: true`).

Scoped to cmd only via a new ShellSpawnConfig.windowsVerbatim flag:
PowerShell/pwsh have different quote rules and must NOT use verbatim
(it splits quoted arguments into separate tokens), and POSIX shells
(bash/zsh/fish, incl. WSL) are unaffected -- windowsVerbatimArguments
is a no-op outside win32. No behaviour change on macOS/Linux.

Before: echo "hello world" a"b"c  ->  \"hello world\" a\"b\"c
After:  echo "hello world" a"b"c  ->  "hello world" a"b"c
@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f0bfb151-90c8-449d-838f-5790e1a11d5d

📥 Commits

Reviewing files that changed from the base of the PR and between 7126346 and 4835185.

📒 Files selected for processing (1)
  • src/terminal-manager.ts

📝 Walkthrough

Walkthrough

This PR adds Windows-specific argument handling for cmd.exe. A new windowsVerbatim configuration flag is defined, automatically set for cmd/cmd.exe shells, and applied as the windowsVerbatimArguments spawn option during command execution on Windows to improve argument quoting compatibility.

Changes

Windows cmd.exe Verbatim Argument Handling

Layer / File(s) Summary
Config, shell mapping, and runtime application
src/terminal-manager.ts
ShellSpawnConfig adds optional windowsVerbatim field; getShellSpawnArgs enables it for cmd/cmd.exe; executeCommand applies windowsVerbatimArguments spawn option on Windows when flag is set.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested labels

size:M

Suggested reviewers

  • edgarsskore
  • serg33v

Poem

🐰 A rabbit hops through Windows paths,
Setting flags for cmd.exe's wrath,
Verbatim arguments, quotes so neat,
Making cmd's behavior complete! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly summarizes the main change: fixing Windows cmd command quote preservation in start_process.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/win-cmd-quote-args

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@wonderwhy-er wonderwhy-er merged commit f5ba5a4 into main Jun 5, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant