Skip to content

fix(shell): quote cmd.exe scripts for cmd, not the C runtime - #4486

Closed
bharvey42 wants to merge 1 commit into
rtk-ai:developfrom
bharvey42:fix/4288-cmd-script-quoting
Closed

bharvey42 wants to merge 1 commit into
rtk-ai:developfrom
bharvey42:fix/4288-cmd-script-quoting

Conversation

@bharvey42

Copy link
Copy Markdown

Fixes #4288.

rtk run -c and err/test/summary --shell cmd handed the script to cmd.exe through child_arg, which escapes by C-runtime rules. cmd parses its command string differently: it strips the first and last quote and does no backslash processing. So a script's own quotes reached cmd as \".

On Windows, this passes /S /C and the script inside one extra pair of outer quotes through CommandExt::raw_arg, so cmd strips exactly that pair. This follows Rust's documented cmd example. Direct argv and every other shell keep their current encoding, and Linux and macOS are untouched (the branch is #[cfg(windows)]).

The fix and the two integration tests are @ryanduguid's, from their review of #4125, and they are credited as co-author. On top of that:

  • The tests use common::rtk_command() and live in run_shell_test.rs's windows module, because the fix(tests): keep the test suite and the developer's environment apart #3758 isolation scan rejects CARGO_BIN_EXE_rtk outside tests/common.
  • a_single_shell_phrase_falls_back_to_the_platform_shell assumed [flag, script] for the platform shell, so it failed on Windows with the fix. It now accounts for cmd.
  • A new Windows unit test pins the exact argument shape for cmd and CMD.EXE.

Verified on Windows 11, Git Bash, x86_64-pc-windows-gnu, base develop e4f0509

  • cargo fmt --all --check passes, and cargo clippy --all-targets reports no warnings.
  • cargo test --all gives 4075 passed / 2 failed. Both failures also fail on pristine develop on this machine (find_cmd::run_from_args_propagates_find_exit_status, stream::test_read_lines_lossy_preserves_lines_after_invalid_utf8).
  • Red/green: without the shell.rs change, both new integration tests and both affected unit tests fail. With it, they pass.

I also ran an argv probe: a node child that prints the argv it receives, run under official v0.51.0 and under this branch. Each script went through all five forms: run -c, run --shell CMD.EXE -c, and err/test/summary --shell cmd.

Script v0.51.0 This PR
node argv.js "a b" c ["\"a","b\"","c"] ["a b","c"]
"…\node dir\node.exe" argv.js "a b" c rc 1, is not recognized ["a b","c"]
node argv.js "x & y" "(p)" ["\"x","&","y\"","\"(p)\""]: the quoted & acted as a command separator ["x & y","(p)"]
node -e "process.exit(7)" rc 0: the script was mangled into a no-op, which masked the exit code rc 7
direct run node argv.js "a b" '"q"' c ["a b","\"q\"","c"] unchanged

The last two rows go beyond what the issue reported. On v0.51.0, a quoted & in a cmd script splits it into a second command, and a quoted argument can silently turn the script into something else that exits 0.

Not verified: MSVC toolchain (ryanduguid validated the same shell.rs change on Rust 1.91.0 MSVC), Linux and macOS (no code change there), and upstream CI. I have no merged PR here yet, so the workflow needs a maintainer to approve it before it runs.

`rtk run -c` and `err`/`test`/`summary --shell cmd` passed the script to
cmd.exe through `child_arg`, whose escaping follows the C runtime's rules.
cmd parses its command string its own way: it strips the first and last
quote and does no backslash processing. A script's own quotes therefore
arrived as `\"`, which split quoted arguments, exposed a quoted `&` as a
command separator, and kept a script that starts with a quoted program
path from ever starting.

On Windows, pass `/S /C` and the script inside one extra pair of outer
quotes through `CommandExt::raw_arg`, so cmd strips exactly that pair.
Direct argv and every other shell keep their existing encoding.

The fix and the two integration tests are ryanduguid's, from rtk-ai#4125's
review. The unit test whose expectation assumed `[flag, script]` for the
platform shell now accounts for cmd, and a Windows unit test pins the
argument shape for `cmd` and `CMD.EXE`.

Fixes rtk-ai#4288

Co-authored-by: Ryan Duguid <[email protected]>
@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working shell testing windows labels Oct 8, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes cmd.exe script quoting on Windows: scripts run via rtk run -c or err/test/summary --shell cmd were being escaped with C-runtime rules via child_arg, but cmd.exe parses quotes differently (strips only the first/last quote, no backslash processing), corrupting scripts with embedded quotes, &, or redirection. The fix wraps the script in /S /C plus a single outer quote pair via raw_arg on Windows only, leaving all other shells and platforms unchanged, and adds unit and integra…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4288


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@bharvey42

Copy link
Copy Markdown
Author

Closing in favour of #4103, which already carries this fix (see #4288 (comment)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working shell testing windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: rtk run -c / --shell cmd lose a script's own quotes to cmd.exe (quoted program path never starts)

1 participant