Windows: arguments to cmd.exe itself get backslash-escaped quotes cmd does not decode (`rtk cmd /c` with a quoted path)
What happens
When the program rtk starts is cmd.exe itself (rtk cmd /c …, rtk proxy cmd /c …, or a passthrough whose program resolves to cmd), each argument gets std's general-purpose Windows encoding, the MSVCRT convention. That convention escapes an embedded " as \". cmd.exe does not decode that escape: /C strips one outer pair of quotes from its command string and runs the rest as written, backslashes included.
An argument that holds both quotes and a space therefore reaches cmd with \" in it. With the PowerShell form #3691 recommends:
rtk cmd.exe /c 'if exist "C:\Program Files\Git\bin\git.exe" (echo FOUND) else (echo MISSING)'the argument after /c contains spaces and quotes, so it is sent as
cmd.exe /c "if exist \"C:\Program Files\Git\bin\git.exe\" (echo FOUND) else (echo MISSING)"cmd strips the outer pair and sees if exist \"C:\Program Files\Git\bin\git.exe\" …. The \" sequences break the quoted path: cmd parses \"C:\Program as the path and Files\Git\bin\git.exe\" as the command, so FOUND is never printed. The same applies to echo "a b", which prints \"a b\".
The encoded command line above follows from std's MSVCRT encoding. What cmd then does with it is derived from cmd's documented /C quote handling; it has not been reproduced on a Windows machine.
.bat/.cmd shims are not affected: std encodes their arguments with its batch-aware encoder. rtk run -c / --shell cmd is not affected either, since the script is handed to cmd /S /C "<script>" verbatim (#4288, fixed by #4103).
Expected
An argument for a cmd.exe child should be encoded the way cmd parses its command line:
- the argument's own quotes are kept as written;
- an argument containing whitespace and no quotes is wrapped in plain quotes;
- nothing is backslash-escaped;
- when the arguments form a
/cor/kcommand string, they are joined and passed verbatim under/S /C "<…>", asrtk run -cdoes.
Where
src/core/child_command.rs after #4103: parses_like_cmd routes cmd itself to std's general-purpose encoder. The encoder decision is made per program there, so a cmd-aware branch would sit next to it.
Related
- #3691: its "Quoting Consideration" section hits this with
rtk cmd.exe /c '…"…"…'from PowerShell. - #4288: the same cmd parsing rule for
rtk run -c, fixed by #4103'scmd /S /C "<script>". - #4326: the other half of "encode for the kind of child", MSYS/Cygwin children and backslash pairs.
Source: rtk-ai/rtk