Skip to content

refactor(core): make MSYS-safe child argument encoding unforgettable - #4103

Open
KuSh wants to merge 1 commit into
rtk-ai:developfrom
KuSh:proto/child-command
Open

KuSh wants to merge 1 commit into
rtk-ai:developfrom
KuSh:proto/child-command

Conversation

@KuSh

@KuSh KuSh commented Sep 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

ChildArgExt::child_arg (#3728) asks every call site to remember a second name for arg, and a call site can forget it, as tree.rs did (#4101). This makes the encoding a property of the type: resolved_command() returns a ChildCommand, whose own arg/args encode and whose Command is private, so std's unencoded arg is out of reach.

An argument to a program cmd.exe does not parse is quoted whenever it holds something Cygwin's build_argv/globify would reinterpret in an MSYS/Cygwin child (Git for Windows' grep, find, ls, …): " or ', one of ()*?[{}, a leading ~ or @, a line break. The quoting is libuv's, exact for native children. Arguments are handled as UTF-16 code units, so an unpaired surrogate is protected too. A backslash pair inside the quotes still loses one backslash in an MSYS child, which #4326 tracks.

Behaviour changes (Windows)

Enforcement

  • clippy.toml disallows std::process::Command::new, and src/lib.rs denies that lint outside test builds, so a raw Command in production code fails cargo clippy; ChildCommand::new is the one allowed site. The workspace lints allow it, so tests and the other workspace crates are not bound by it.
  • .semgrep.yml's dynamic-command-execution and interpreter-execution also match ChildCommand::new.

Test plan

  • Every encoding decision is asserted on every platform, down to the exact bytes each kind of child receives; only the raw_arg call and Windows' path resolution behind the batch test are Windows-only. The expectations were checked against msys2-runtime's own build_argv/quoted/globify and glob.cc compiled on Linux, next to an MSVCRT parser.
  • Windows CI runs rtk run -c through cmd with echo "a b" & echo c and with a quoted program path holding a space, and runs echo "a & b">> out.txt & exit 7 through run -c, run --shell cmd -c and err/test/summary --shell cmd, which must write "a & b" once and exit 7; Linux runs the same five surfaces through sh.
  • cargo fmt --all -- --check, cargo clippy --all-targets (also with --no-default-features, and for proxy/) and cargo test --all pass on Linux; a Windows cargo check --all-targets passes.

Fixes #4102
Fixes #4116
Fixes #4288
Refs #4256

🤖 Generated with Claude Code

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

♻️ Type refactor
🟡 Risk medium

Summary

This PR replaces the ChildArgExt trait-based convention for MSYS/Cygwin-safe argument encoding with a ChildCommand newtype wrapping std::process::Command, so the safe encoding is enforced by the type system rather than by developer memory. It also widens the quoting trigger beyond just " to a superset of Cygwin-reinterpreted characters, adds a clippy lint banning raw Command::new, and fixes an argument-mangling bug in tree.rs with a zero-line diff to that file.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4101, #4102


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

@KuSh
KuSh force-pushed the proto/child-command branch from 4eb3fda to 4e4a80a Compare September 18, 2026 23:18
@KuSh
KuSh marked this pull request as ready for review September 18, 2026 23:18
@KuSh
KuSh force-pushed the proto/child-command branch 9 times, most recently from e2470b6 to dd09014 Compare September 19, 2026 00:22
@KuSh
KuSh force-pushed the proto/child-command branch from dd09014 to e05f7fd Compare September 26, 2026 01:28
@KuSh
KuSh force-pushed the proto/child-command branch from e05f7fd to 400a251 Compare September 28, 2026 01:31
@KuSh
KuSh force-pushed the proto/child-command branch from 400a251 to 385b21e Compare September 28, 2026 04:37
@KuSh
KuSh force-pushed the proto/child-command branch from 385b21e to ce89f2f Compare September 30, 2026 22:28
@KuSh
KuSh force-pushed the proto/child-command branch from ce89f2f to 5a2afe1 Compare September 30, 2026 22:57
@pszymkowiak

pszymkowiak commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

@KuSh I ran this natively on Windows today (MSVC build, Git for Windows 2.56, throwaway dirs, both from a real Git Bash and from PowerShell), side by side with develop and the two smaller PRs that quote MSYS arguments, #4260 and #4985. Short version: this is the one to land, it fixes everything they fix without their regressions.

case develop #4260 #4985 this PR #4544 (on top)
Git Bash: find . -name '*.tmp' -delete, one top-level match + sub/c.tmp sub/c.tmp left behind, exit 0 ok ok ok ok
Git Bash: find . -name '[ab]*.tmp', grep -E '.{0,20}foo' errors ok ok ok ok
grep '^\[', grep 'x|\[a' (#4737) invalid regex ok ok ok ok
grep "it's" f no match, hangs while stdin is open same ok ok ok
grep 'C:\\temp' f ok ok no match ok ok
grep x \\localhost\C$\...\f ok ok file not found ok ok
PowerShell: rtk grep foo *.txt ok broken broken broken ok
PowerShell: rtk ls *.txt, rtk wc -l *.txt ok broken broken broken broken

Eleven other Git Bash shapes (grep --include='*.rs', git ls-files '*.rs', git log --format with braces and brackets, HEAD@{0}, find -name '?2.tmp', plain ls/wc on paths) match the native tools on every branch. Clippy is clean on Windows.

Two things before it can go in:

  1. It no longer merges cleanly on develop: conflicts in src/cmds/system/search.rs and src/core/runner.rs.
  2. The last row. With this PR alone, rtk grep/ls/wc *.txt from PowerShell or cmd stop working, and fix(windows): expand glob operands a non-globbing caller left as written #4544 brings back grep but not ls or wc yet. Do you want to land this with that gap documented, or wait until the ls/wc operand expansion is ready? Either way I'd like the decision written down here, since Windows users on PowerShell-based agents will hit it.

I'm leaving #4260 and #4985 open for now with a note pointing here.

@pszymkowiak pszymkowiak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. I went through it in depth, both by reading the encoder and the call sites and by running it natively on Windows (MSVC, Git for Windows 2.56) and on Linux, each against this PR's base 6c94fc51.

  • MSYS side: I pushed 30 awkward arguments through rtk bypass into Git's printf. Six that were mangled on the base now arrive intact (it's, a bare ~, a{b, \\srv\share\a b, a line break, and \\?\C:\x got better). None regressed.
  • Native side: the same 30 arguments go through git rev-parse --sq-quote and a .cmd shim byte for byte the same as on the base, so native programs and batch files see no change.
  • Real cases: everything from my earlier comment holds. find -name '*.tmp' -delete no longer skips subdirectories, grep "it's" no longer hangs, and C:\\temp and UNC paths stay intact.
  • Full suites: Windows 5229 passed against 5203 on the base, Linux 5600 against 5578. Both have the same pre-existing failures as the base, nothing new. fmt and clippy are clean.

It still needs a rebase on develop (conflicts in src/cmds/system/search.rs and src/core/runner.rs), and the PowerShell ls/wc *.txt question from my previous comment is still open. Happy for this to go in once those are settled.

@KuSh
KuSh force-pushed the proto/child-command branch from ddcf77b to de78669 Compare November 21, 2026 00:09
@KuSh

KuSh commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @pszymkowiak, for the side-by-side runs and the review. Rebased on develop: the conflicts in search.rs and runner.rs were imports only (develop's Flag/Grammar next to ChildCommand), nothing else changed.

On the PowerShell row: rather than landing this with the gap documented, I'd wait until #4544 covers ls and wc too, then merge this PR and #4544 back to back, so no release has the quoting without the operand expansion. I've asked for the rebase and the two commands on #4544.

bharvey42 added a commit to bharvey42/rtk that referenced this pull request Nov 24, 2026
From PowerShell or cmd, `rtk ls *.txt` reached the child as the literal
name `*.txt` (rtk-ai#4103 quotes it), so ls failed with `cannot access`.

ls now expands its free positionals, as read with the host flavour's
grammar from rtk-ai#5099, so a flag's value such as `-I '*.o'`, `--ignore
'*.o'` or `--hide='*~'` still reaches ls as a pattern, and BSD's boolean
`-I` leaves the next word an operand. The expansion runs once at the top
of `run`, so the filtered listing and `native_listing` name the same
files; the tracked command stays what the user typed.

operand_glob gains `expand_at`, which expands the arguments at the
indices a command's parser picked and keeps the rest; grep/rg now use it
too.
bharvey42 added a commit to bharvey42/rtk that referenced this pull request Nov 24, 2026
From PowerShell or cmd, `rtk wc -l *.txt` reached the child as the
literal name `*.txt` (rtk-ai#4103 quotes it). Bounding wc's operands needs a
grammar, so wc gets one per flavour, transcribed from each wc's help:
GNU (coreutils 8.32 and 9.4) `-c/-m/-l/-w/-L` with their long names,
`--files0-from F`, `--total WHEN`, `--help` and `--version`, with
getopt_long's unique-prefix abbreviation; BSD `-Lclmw` and `--libxo`.

Three decisions now read it instead of `starts_with('-')`:

- Operand expansion: the free positionals only, never `--files0-from`'s
  or `--total`'s value.
- Whether wc reads stdin: with no file operand, with `-` as one, or when
  the last `--files0-from` names `-`. Before, `--total always` counted
  `always` as a file.
- The output mode. A per-character scan of every dash-led argument read
  `--chars` as `-c` (bytes) and `--bytes` as no flag, so its output was
  passed through raw. Long names, abbreviations and `-L` now count as
  columns and `--`-protected operands don't. For BSD, options stop at
  the first operand (`wc f -l` names a file `-l` there) and `-c`/`-m`
  cancel, the last one winning, both checked against macOS's wc.

ls's `Flavor` and `HOST_FLAVOR` become `pub(super)` so wc reads the same
host flavour.
@KuSh
KuSh force-pushed the proto/child-command branch from de78669 to 577c220 Compare November 25, 2026 01:20
`ChildArgExt::child_arg` requires every call site to remember a second
name for `arg`, and a call site can forget it, as `tree.rs` did (rtk-ai#4101).
`resolved_command` now returns `ChildCommand`, whose own `arg`/`args` do
the encoding, and which keeps its `Command` private: an extension trait
cannot replace std's inherent `arg`.

Every argument to a program cmd.exe does not parse is encoded literally
(`cmd` and `.bat`/`.cmd` shims are below). It is quoted whenever it
holds what Cygwin's `build_argv` and `globify` reinterpret: `"` or `'`,
one of `()*?[{}`, a leading `~`, a leading `@` (read as a response
file) and a line break (an argument separator). That closes rtk-ai#4102:
without it `rtk grep "it's"` reaches MSYS grep as `its`, and
`rtk grep '^\[TAG\]'` as the class `^[TAG]`. Glob metacharacters and a
leading `~` are in that set, so an MSYS child never glob- or
tilde-expands an argument rtk hands it: what the calling shell left
unexpanded reaches the tool as written. From cmd or PowerShell, which
leave globs to the program, `rtk cat *.rs` therefore fails on a missing
`*.rs`: without the tool's grammar a file operand cannot be told from a
pattern, and a pattern the child expands (`find -name '*.tmp' -delete`)
silently runs a different command. Arguments are encoded as UTF-16
code units, so an unpaired surrogate is protected like any other text.
The quoting is libuv's, exact for native children; for an MSYS child a
run of two or more backslashes that is not in front of a quote loses
one backslash of each pair (see rtk-ai#4326). A UNC path whose host starts
with a letter and is followed by a `\`, when it must be wrapped (for one
of those characters, a space or a tab), goes out with its `\\` and the
host's first letter before the opening quote, `\\s"rv\share\x(1)"`:
`globify` still reads it as a DOS path and keeps the `\\` that quoting
it whole would halve, so both parsers read it as written.

On Windows `cmd` parses its own command line and gets its script
verbatim, as `cmd /S /C "<script>"`: `/S` makes cmd strip exactly the
wrapping pair of quotes instead of applying its
two-quotes-around-an-executable heuristic. Other arguments to a program
cmd.exe parses stay on std's encoders, the batch-aware one for a
`.bat`/`.cmd` shim and the general one for `cmd`. A batch file is told
apart as std tells it: on the path Windows resolves, so `x.bat.`,
`x.bat\.` and `x.bat` with a trailing space are batch files, and a
verbatim `\\?\` or `\??\` path as written. Windows integration tests run
`rtk run -c` through cmd, with `echo "a b" & echo c` and with a quoted
program path holding a space, and a script with a quoted `&` and an exit
code through `run -c` and `run`/`err`/`test`/`summary --shell cmd`.

Only the `raw_arg` calls and that path resolution are Windows-only:
every other encoding decision is asserted on every platform. In a test
build the git `resolved_command` returns is isolated through a test-only
`ChildCommand` method, which applies `isolate_git_config` to the private
`Command`. Code that reads the argv, such as the usage guard, reads the
arguments as written, since on Windows the command line carries their
encoding.

`clippy.toml` disallows `std::process::Command::new`, and `src/lib.rs`
denies that lint outside test builds, so production code builds every
child through `ChildCommand::new`, the one allowed site. The workspace
lints allow it, so tests, integration tests and the other workspace
crates are not bound by it. `.semgrep.yml`'s `dynamic-command-execution`
and `interpreter-execution` also match `ChildCommand::new`.
`.claude/rules/rust-patterns.md` states the rule for agents, and the
contributor docs, `SECURITY.md` and the agent and skill guides build
production children through `ChildCommand` (`resolved_command`,
`tool_exec`).

Fixes rtk-ai#4102
Fixes rtk-ai#4116
Fixes rtk-ai#4288
Refs rtk-ai#4256

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KuSh
KuSh force-pushed the proto/child-command branch from 577c220 to e36369a Compare November 25, 2026 02:16
bharvey42 added a commit to bharvey42/rtk that referenced this pull request Nov 25, 2026
From PowerShell or cmd, `rtk ls *.txt` reached the child as the literal
name `*.txt` (rtk-ai#4103 quotes it), so ls failed with `cannot access`.

ls now expands its free positionals, as read with the host flavour's
grammar from rtk-ai#5099, so a flag's value such as `-I '*.o'`, `--ignore
'*.o'` or `--hide='*~'` still reaches ls as a pattern, and BSD's boolean
`-I` leaves the next word an operand. The expansion runs once at the top
of `run`, so the filtered listing and `native_listing` name the same
files; the tracked command stays what the user typed.

operand_glob gains `expand_at`, which expands the arguments at the
indices a command's parser picked and keeps the rest; grep/rg now use it
too.
bharvey42 added a commit to bharvey42/rtk that referenced this pull request Nov 25, 2026
From PowerShell or cmd, `rtk wc -l *.txt` reached the child as the
literal name `*.txt` (rtk-ai#4103 quotes it). Bounding wc's operands needs a
grammar, so wc gets one per flavour, transcribed from each wc's help:
GNU (coreutils 8.32 and 9.4) `-c/-m/-l/-w/-L` with their long names,
`--files0-from F`, `--total WHEN`, `--help` and `--version`, with
getopt_long's unique-prefix abbreviation; BSD `-Lclmw` and `--libxo`.

Three decisions now read it instead of `starts_with('-')`:

- Operand expansion: the free positionals only, never `--files0-from`'s
  or `--total`'s value.
- Whether wc reads stdin: with no file operand, with `-` as one, or when
  the last `--files0-from` names `-`. Before, `--total always` counted
  `always` as a file.
- The output mode. A per-character scan of every dash-led argument read
  `--chars` as `-c` (bytes) and `--bytes` as no flag, so its output was
  passed through raw. Long names, abbreviations and `-L` now count as
  columns and `--`-protected operands don't. For BSD, options stop at
  the first operand (`wc f -l` names a file `-l` there) and `-c`/`-m`
  cancel, the last one winning, both checked against macOS's wc.

ls's `Flavor` and `HOST_FLAVOR` become `pub(super)` so wc reads the same
host flavour.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

4 participants