Skip to content

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

Open

Description

@KuSh

rtk run -c '<script>' (and --shell cmd '<script>') hands the script to cmd.exe as an ordinary argument, so a script that carries its own quotes loses them to cmd's command-string parsing. A script whose program is a quoted path with spaces never starts.

Reported and reproduced on native Windows by @ryanduguid and independently confirmed by @bharvey42 on a different toolchain, while reviewing #4125. Filing so the evidence and the fix are not lost in that PR's thread — it is not a regression from #4125 and should not hold it.

Symptom

rtk run -c '"C:\path with spaces\rtk.exe" --version'

The child never starts; cmd reports the quoted path as an unknown command ('C:\path' is not recognized). The same command line handed straight to cmd /d /s /c with its outer command quoting works.

@bharvey42, with a node child that writes the argv it receives:

node argv.js "a b" c   through run -c / err|test|summary --shell cmd
  expected  ["a b","c"]
  actual    ["\"a","b\"","c"]

Mechanism

src/core/shell.rs builds the explicit-shell command as resolved_command(program).arg(command_flag(program)).child_arg(script).

cmd.exe does not parse its command string by CommandLineToArgvW rules; it strips the first and last quote of the tail and performs no backslash processing. So neither std's escaping nor ChildArgExt's MSYS/libuv encoding is the right encoding for the /C payload — the script needs cmd's own quoting, which is /S plus one extra outer quote pair passed through raw_arg.

This is the same family as #3185 (quoting lost on re-execution, stray files from a > inside a quoted argument), #3691 (quoted cmd.exe commands not transparent) and #4102 (MSYS children and child_args coverage), but a distinct site: those are about argv handed to a child, this is about the command string handed to cmd.

Not a regression, with one caveat

The pre-#4125 code did Command::new("cmd").arg("/C").arg(&raw) — plain std escaping, also without /S — so the quoted-path case fails on stable too. @bharvey42's before-state on v0.50.0 (which already carries #3728) confirms it:

Command through all four runners v0.50.0
'a b' c ["a","b","c"]
"x > stray" ["x"], plus an empty file stray
sh -c 'exit 7' rc 0
nosuchcmd123 rc 1
"C:/Program Files/Git/cmd/git.exe" --version 'C:/Program' is not recognized

The caveat worth acting on: for a script with embedded quotes the two are differently wrong, and arguably worse now — stable dropped the quotes (["a","b","c"]), the current code injects backslashes (["\"a","b\"","c"]). The argument boundary is lost either way, but the text is now corrupted rather than only unquoted.

Fix

@ryanduguid has a patch and tests, verified on native Windows (Rust 1.91.0 MSVC): pass /S /C and the script in one extra pair of outer quotes through CommandExt::raw_arg, leaving direct argv and other shells on their existing encoding. @bharvey42 confirmed both halves matter — raw_arg alone fixes embedded quotes but a script starting with a quoted path still breaks, because cmd strips the first and last quote.

Their tests cover paths with spaces, & and parentheses, embedded quotes, trailing backslashes, redirection and exit code 7 across run, err, test and summary, including default cmd and upper-case CMD.EXE. The patch is in #4125 — worth lifting from there rather than rewriting.

Also reported to reproduce at #3505 and #4206.

Not verified

Linux and macOS are unaffected (the /C branch is Windows-only), but the patch has not been through upstream CI. Whoever picks this up should let the Windows job run it.

Activity

  1. rtk-wshm-sync-bot commented on Sep 26, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 94%

    Summary

    On Windows, rtk run -c/--shell cmd pass a script to cmd.exe with the wrong quoting, so quoted program paths never launch and embedded quotes get corrupted; fix (with tests) already exists in PR #4125.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/core/shell.rs

    Suggested Actions

    1. Cherry-pick @ryanduguid's /S /C + outer-quote fix for cmd.exe from PR fix(cli)!: preserve argv boundaries in generic runners #4125 rather than rewriting
    2. Verify the fix against @bharvey42's argv table (embedded quotes, quoted paths, &/parens, redirection, exit code 7)
    3. Run the Windows CI job on the patch before merging since it hasn't been through upstream CI yet
    4. Cross-reference fix(exec): stop re-parsing already-parsed argv through a second shell #3505 and fix(err): pass the child argv through verbatim #4206 as other reports of the same underlying cmd.exe quoting bug

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

  2. TechWizard9999 commented on Sep 27, 2026

    @TechWizard9999
    Contributor

    Hi @KuSh , Can I work on this
    Could you please assign this ?

  3. added 3 commits that reference this issue on Sep 28, 2026
    385b21e
    ce89f2f
    5a2afe1
  4. TechWizard9999 commented on Oct 6, 2026

    @TechWizard9999
    Contributor

    Hi @KuSh , As I dont have windows to test could you please take care of this issue
    Thanks!!

  5. bharvey42 commented on Oct 8, 2026

    @bharvey42

    I can take this, since I'm on Windows (Git Bash, x86_64-pc-windows-gnu). The bug site hasn't changed on develop: src/core/shell.rs:174 still does .arg(command_flag(program)).child_arg(script).

    My plan is to lift @ryanduguid's /S /C + raw_arg patch and tests from #4125, credit them as co-author, and rerun the argv probe from the issue body across run, err, test and summary. I'll open a PR against develop.

    @ryanduguid, if you'd rather open it yourself, say so and I'll review it instead.

  6. KuSh commented on Oct 8, 2026

    @KuSh
    CollaboratorAuthor

    @bharvey42 should already be fixed by #4103. I'll rebase it to make it mergeable again

  7. added a commit that references this issue on Oct 8, 2026
    d826331
  8. 4 remaining items

  9. added 4 commits that reference this issue on Oct 9, 2026
    aa57322
    a94dd92
    eaa3fda
    cba780a
  10. KuSh commented on Oct 10, 2026

    @KuSh
    CollaboratorAuthor

    Thanks @bharvey42, and for closing #4486. The rebased #4103 already copies the binary with std::fs::copy(rtk().get_program(), &program), so the isolation scan passes.

    For the two cases you hit on 0.51.0, #4103 now has cmd_scripts_keep_a_quoted_ampersand_and_their_exit_code: echo "a & b">> out.txt & exit 7 through run -c, run --shell cmd -c and err/test/summary --shell cmd must append exactly "a & b" to the file and exit 7. It passes on windows-latest.

    Your argv probe is still welcome on top of that: it runs a real argv-printing child on a Git Bash / windows-gnu setup, which the CI test doesn't.

  11. added 4 commits that reference this issue on Oct 16, 2026
    7a137fe
    e4917d2
    8034dd1
    ac507f1
  12. added a commit that references this issue on Oct 29, 2026
    a3bcacb
  13. added 5 commits that reference this issue on Nov 17, 2026
    863d5ce
    ddcf77b
    de78669
    577c220
    e36369a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions