Repository navigation
Conversation
📊 Automated PR Analysis
SummaryThis PR replaces the Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
4eb3fda to
4e4a80a
Compare
e2470b6 to
dd09014
Compare
dd09014 to
e05f7fd
Compare
e05f7fd to
400a251
Compare
400a251 to
385b21e
Compare
385b21e to
ce89f2f
Compare
ce89f2f to
5a2afe1
Compare
ac507f1 to
a3bcacb
Compare
863d5ce to
ddcf77b
Compare
|
@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.
Eleven other Git Bash shapes ( Two things before it can go in:
I'm leaving #4260 and #4985 open for now with a note pointing here. |
pszymkowiak
left a comment
There was a problem hiding this comment.
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 bypassinto Git'sprintf. 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:\xgot better). None regressed. - Native side: the same 30 arguments go through
git rev-parse --sq-quoteand a.cmdshim 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' -deleteno longer skips subdirectories,grep "it's"no longer hangs, andC:\\tempand 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.
ddcf77b to
de78669
Compare
|
Thanks @pszymkowiak, for the side-by-side runs and the review. Rebased on develop: the conflicts in On the PowerShell row: rather than landing this with the gap documented, I'd wait until #4544 covers |
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.
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.
de78669 to
577c220
Compare
`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]>
577c220 to
e36369a
Compare
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.
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.
Summary
ChildArgExt::child_arg(#3728) asks every call site to remember a second name forarg, and a call site can forget it, astree.rsdid (#4101). This makes the encoding a property of the type:resolved_command()returns aChildCommand, whose ownarg/argsencode and whoseCommandis private, so std's unencodedargis out of reach.An argument to a program cmd.exe does not parse is quoted whenever it holds something Cygwin's
build_argv/globifywould 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)
rtk grep "it's" freaches MSYS grep asit's, no longerits;rtk grep '^\[TAG\]' fkeeps its backslashes instead of becoming the class^[TAG](Windows: MSYS/Cygwin children also lose apostrophes and glob metacharacters, not just double quotes (sequel to #3727) #4102).rtk grep x "\\srv\my share\f.txt"reaches MSYS grep with its leading\\intact: a UNC path whose host starts with a letter goes out as\\s"rv\my share\f.txt", which both parsers read as written.rtk run -cthrough cmd runs ascmd /S /C "<script>", so a quoted program path holding a space starts (Windows:rtk run -c/--shell cmdlose a script's own quotes to cmd.exe (quoted program path never starts) #4288).rtk ls *.rs,rtk cat *.rs,rtk du -s *.txtandrtk bypass ls *.txtreport a missing*.rsor*.txt. Without a tool's grammar rtk cannot tell a file operand from a pattern, and a pattern the child expands (find . -name '*.tmp' -delete,grep -E '.{0,20}foo') silently runs a different command, while a literal file operand fails loudly. From Git Bash nothing changes, since bash expands first. fix(windows): expand glob operands a non-globbing caller left as written #4544 expands the file operands ofls,wc,findandgrep/rgbefore the child is built..bat/.cmdshims andcmditself stay on std's encoders. A batch file is told apart on the path Windows resolves, as std does, sox.bat.,x.bat\.andx.batwith a trailing space count as batch files, and a verbatim\\?\path is tested as written. This takes over fix(core): decide batch files the way std does #4498.Enforcement
clippy.tomldisallowsstd::process::Command::new, andsrc/lib.rsdenies that lint outside test builds, so a rawCommandin production code failscargo clippy;ChildCommand::newis the one allowed site. The workspace lints allow it, so tests and the other workspace crates are not bound by it..semgrep.yml'sdynamic-command-executionandinterpreter-executionalso matchChildCommand::new.Test plan
raw_argcall and Windows' path resolution behind the batch test are Windows-only. The expectations were checked against msys2-runtime's ownbuild_argv/quoted/globifyandglob.cccompiled on Linux, next to an MSVCRT parser.rtk run -cthrough cmd withecho "a b" & echo cand with a quoted program path holding a space, and runsecho "a & b">> out.txt & exit 7throughrun -c,run --shell cmd -canderr/test/summary --shell cmd, which must write"a & b"once and exit 7; Linux runs the same five surfaces throughsh.cargo fmt --all -- --check,cargo clippy --all-targets(also with--no-default-features, and forproxy/) andcargo test --allpass on Linux; a Windowscargo check --all-targetspasses.Fixes #4102
Fixes #4116
Fixes #4288
Refs #4256
🤖 Generated with Claude Code