Skip to content

fix(err,test): preserve argument quoting and exit codes in rtk err/test - #3002

Closed
albatrossflyon-coder wants to merge 1 commit into
rtk-ai:developfrom
albatrossflyon-coder:fix/err-test-quote-preservation
Closed

albatrossflyon-coder wants to merge 1 commit into
rtk-ai:developfrom
albatrossflyon-coder:fix/err-test-quote-preservation

Conversation

@albatrossflyon-coder

Copy link
Copy Markdown
Contributor

Summary

Fixes #2985.

rtk err <cmd> and rtk test <cmd> build their sh -c/cmd /C command string with argv.join(" "). The OS already splits argv into correct elements by the time this process sees them, but the naive join discards that boundary information. An argument containing shell metacharacters — node -e 'console.log("a")', python3 -c "print(...)" — gets flattened into the join, and the shell re-splits it on those same characters when re-parsing the string.

On the exit-code half: I don't think exit-code propagation itself is actually broken — run_streaming correctly reads and returns the child's real exit status (verified this is the same code path I already fixed for #2994 in #2997). The exit-0 symptom in the issue's repro is what happens when there's no child failure to propagate: the shell's own syntax-error diagnostic looks like a command failure, but the target command was never actually invoked in a form the shell could execute — there's nothing non-zero to report because nothing that ran actually failed. Fixing the quoting fixes this too, verified below.

Fix

Added quote_shell_arg/shell_quote_join to re-quote each argument before joining, so the shell reconstructs the original argument boundaries instead of re-splitting a flattened string.

Windows needed one extra step beyond quoting. std::process::Command's own argument-escaping was re-escaping the already-quoted command string a second time when spawning cmd.exe — corrupting it before cmd.exe ever saw it. Using raw_arg (passes the string through untouched, no extra escaping layer) for the Windows /C invocation fixes that. Without it I could reproduce a different broken-quoting failure than the one reported — same root cause, different platform-specific escaping bug.

Verified live

$ rtk err node -e 'console.log("a")'
[ok] Command completed successfully (no errors)
$ echo $?
0

A genuinely failing command with quotes, semicolons, and parens all in one argument:

$ rtk err node -e 'console.log("before"); process.exit(3)'
[FAIL] Command failed (exit code: 3)
  before
$ echo $?
3

Also confirmed python -c "print('hello world')" and that legitimate shell features (a plain rtk err invocation without special characters) are unaffected.

Testing

  • New unit tests: quote_shell_arg leaves safe args unquoted / quotes args with metacharacters / quotes args with spaces; shell_quote_join preserves argument boundaries (the exact regression scenario) / handles empty argv
  • cargo test: 2449 passed, 8 ignored, 0 failed
  • cargo clippy --all-targets -- -D warnings: clean

Note

I tested this on Windows (not the macOS environment in the original report), so I can't directly confirm the POSIX sh -c branch against a real POSIX shell — quote_shell_arg's non-Windows path uses the standard single-quote-wrap-with-'\''-escape technique, which is the textbook-correct approach for this, but flagging that a maintainer with a Unix box handy might want to double check it directly.

`rtk err <cmd>` and `rtk test <cmd>` built their sh -c/cmd /C command
string with `argv.join(" ")`. The OS already split argv into correct
elements, but the naive join throws that boundary information away --
an argument containing shell metacharacters (`node -e 'console.log("a")'`,
`python3 -c "print(...)"`) gets flattened, then the shell re-splits it
on those same characters and fails, most commonly with a syntax error.

On top of the quoting loss, a failure like this was being reported as
exit 0 -- not because exit-code propagation itself was broken (it
correctly reads the child's real status), but because there was no
child failure to correctly propagate: the shell's own diagnostic
about the malformed command was easy to mistake for the target
command failing, when actually the target command was never run
at all in a form the shell could execute successfully.

Add quote_shell_arg/shell_quote_join to re-quote each argument before
joining, so the shell reconstructs the exact original boundaries.
Windows needed one extra step beyond quoting: std::process::Command's
own arg-escaping was re-escaping the already-quoted command string a
second time when spawning cmd.exe, corrupting it. Using raw_arg (which
passes the string through untouched) for the Windows /C invocation
fixes that -- verified live with quotes, spaces, semicolons, and
parens in one argument, both for a passing command and one that exits
non-zero after printing partial output.

Fixes rtk-ai#2985
@ecuthiell

Copy link
Copy Markdown

Thanks for working on #2985. I independently reproduced the same failure on native Windows through rtk test: one already-tokenized regex argument containing | was flattened, reparsed by cmd.exe, and treated as a pipeline.

I think direct execution is the stronger contract here than reconstructing and shell-quoting a command string. err, test, and summary already receive tokenized argv from Clap; passing those tokens to Command::new(program).args(args):

  • preserves OsString argument boundaries without a second parser;
  • avoids platform-specific shell quoting and double-escaping rules;
  • keeps literal metacharacters as data rather than implicit shell syntax; and
  • leaves intentional shell behavior explicit through rtk run -c or an explicitly supplied shell child.

I prepared a complete focused patch against current develop at 53323c8512999ae20033c4ff8d62db7a3b4d2b02. It:

  • changes err, test, and summary to retain Vec<OsString> and execute it directly;
  • centralizes child construction while keeping the lossy display string diagnostic-only;
  • adds native-Windows and Unix real-process regressions that exercise all three public subcommands, require exactly one literal pipe-containing argument, and verify exact exit-code propagation; and
  • documents the direct-argv contract and explicit shell escape hatches.

Validation on Windows with Rust 1.97.1:

  • cargo fmt --all --check
  • cargo clippy --all-targets -- -D warnings
  • focused process regression: all three subcommands passed
  • non-baseline suite: 2,468 passed, 8 ignored
  • full suite: the same 11 core::stream Unix-command-assumption failures as the unmodified Windows base; no additional failures

Before opening another overlapping PR, would you prefer to incorporate the direct-execution patch into this PR, or would a maintainer prefer a fresh focused PR against current develop? I am happy to follow either route. The separate hook/rewrite behavior in #3185 is intentionally out of scope.

codex-gpt-5.6-sol-ultra on behalf of Ewen Cuthiell

@KuSh

KuSh commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Closing this in favour of #4125, which takes the same diagnosis to a different conclusion. Thank you for the write-up — the analysis here is right, and the part that took care to establish is the part that ended up mattering.

You identified that argv.join(" ") throws away boundaries the OS had already resolved, and that the exit-code symptom in #2985 was downstream of the quoting rather than a separate bug in exit-code propagation. Both hold. #4125 reaches the same place by removing the reconstruction instead of correcting it: rtk run|err|test|summary execute the program directly, and a script becomes explicit via rtk run -c '<script>' or --shell <shell> '<script>'. That covers summary and run as well as err/test, and it means there is no re-quote/re-split round trip left to get right — including on Windows, where your raw_arg note correctly identifies that cmd.exe's parsing is a second layer on top of CommandLineToArgvW.

One piece of this PR is better than what #4125 currently has, and I have said so on that PR: your quote_shell_arg decides with an allowlist (is_ascii_alphanumeric plus _-./:=@), where #4125's display-quoting uses a denylist of whitespace and quote characters and so leaves *, $VAR, ;, &&, | and backticks bare in the labels it records. An allowlist is the right shape for that, and yours is the one I have pointed them at.

If you disagree that removing the shell is the better answer here, say so and I will reopen — the argument is worth having on its merits, and #4125 has not merged.

@KuSh KuSh closed this Sep 22, 2026
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.

rtk err loses argument quoting (breaks node -e / python3 -c) and exits 0 on failure

3 participants