Skip to content

fix(windows): quote glob patterns passed to MSYS children - #4260

Open
mrhard9090 wants to merge 1 commit into
rtk-ai:developfrom
mrhard9090:fix/windows-msys-glob-args
Open

mrhard9090 wants to merge 1 commit into
rtk-ai:developfrom
mrhard9090:fix/windows-msys-glob-args

Conversation

@mrhard9090

Copy link
Copy Markdown
Contributor

Summary

Refs #4256 (the grep/find part; the head -N rewrite is a separate question and isn't touched here).

  • On Windows, an MSYS/Cygwin child such as Git Bash's grep expands *, ?, [ and { in every argument it receives unquoted, even one the user had quoted. std only wraps arguments containing a space or a tab, so rtk grep -E '.{0,20}foo' f.txt reached grep as .0foo and .20foo: grep: .20foo: No such file or directory.
  • push_child_arg now re-encodes an argument with quote_arg_for_child when it holds one of those characters, as it already does for arguments holding a " (Windows: arguments containing a double quote are corrupted when rtk spawns MSYS/Cygwin children (rtk grep, rtk proxy) #3727). Batch shims stay on std's encoding. Native Windows programs parse the quoted form the same way.

Test plan

  • cargo fmt --all && cargo clippy --all-targets -- -D warnings (clean) && cargo test --bin rtk: 3743 passed, 2 failed. find_cmd::run_from_args_propagates_find_exit_status and stream::test_read_lines_lossy_preserves_lines_after_invalid_utf8 fail the same way on develop on this Windows machine.
  • New unit tests: test_needs_child_quoting_flags_quotes_and_msys_glob_characters (all platforms) and test_child_arg_quotes_msys_glob_patterns (Windows).
  • Manual testing on Windows 11 with Git Bash's grep, MSVC build: before, rtk grep -E '.{0,20}foo' f.txt printed /usr/bin/grep: .20foo: No such file or directory; after, it prints both matching lines. rtk find . -not -path './x/*' gave the same correct result before and after on this machine, so I haven't claimed the find case.

Written with help from Claude Opus 5.5.

An MSYS/Cygwin child such as Git Bash's grep expands *, ?, [ and { in every argument it receives unquoted. std only quotes arguments with spaces or tabs, so a pattern like .{0,20}foo reached grep as .0foo and .20foo. Re-encode those arguments with quote_arg_for_child too, as is already done for arguments holding a quote.

Refs rtk-ai#4256
@CLAassistant

CLAassistant commented Sep 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working find windows grep labels Sep 26, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes a Windows bug where MSYS/Cygwin child processes (like Git Bash's grep) would glob-expand unquoted *, ?, [, and { characters in arguments even when the caller had quoted them. The needs_child_quoting helper now flags these characters alongside ", and push_child_arg re-encodes matching arguments via quote_arg_for_child, while batch shims remain on std's default encoding.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

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

@HetCreep

Copy link
Copy Markdown

The find case you flagged as unconfirmed: I have a repro that was broken before this PR and is fixed by it.

Provenance: AI-assisted, human-directed. Built both develop (6d4b77e) and this PR's head (0ef4744) from source and ran everything below on the machine; nothing here is a read-only check.

-not -path never went through the code path this PR changes on your machine — a predicate with no action dispatches to rtk find's own native matcher, so there was nothing for the fix to change. An action (-delete, -exec) does go through it: find_cmd.rs's VERBATIM_ACTIONS list routes those to a real find child via run_passthrough, which is exactly where push_child_arg/needs_child_quoting sit.

The same shape this issue's own original report used, at three match counts:

find . -name '*.tmp' -delete, cwd has… develop (6d4b77e) this PR (0ef4744)
0 matches correct (nothing to delete) correct
1 match at top level, 1 more in a subdirectory silent: top-level file deleted, subdirectory match untouched, exit 0, no error both deleted
2 matches, both top level paths must precede expression: b.tmp''`, exit 1, deletes nothing both deleted, exit 0

The 1-match row is the one worth naming: it isn't an error, it's a successful-looking run that quietly loses the recursive part of the pattern, because the MSYS child glob-expands *.tmp against its own top-level listing before find's own -name matcher ever sees it, and a single match makes that substitution invisible.

Checked against a real, non-corrupted find on the same machine, not just against develop. A raw argv spawn of find.exe (no shell) hits the identical Windows-only bug a plain program would — it isn't rtk-specific — so the actual reference has to go through a shell: bash -c "find . -name '*.tmp' -delete". Four files at three depths (root ×2, one subdirectory, one two levels down), two non-matching controls:

before: a.tmp  b.tmp  keep.txt  sub/c.tmp  sub/deeper/d.tmp  sub/keep2.txt
real find (via bash -c):  after: keep.txt  sub/keep2.txt
this PR's rtk find:       after: keep.txt  sub/keep2.txt

Byte-identical.

No regression found. A plain -name with no glob character, and the separate bug this PR doesn't touch — a bare filename instead of a -name pattern (find x.tmp y.tmp -delete, tracked on #3410, a dispatch-logic issue rather than a child-quoting one) — behave the same before and after.

Suite at 0ef4744: cargo test --all --no-fail-fast — 3,744 unit tests + every integration binary pass, 1 fails (find_cmd::run_from_args_propagates_find_exit_status), same as your own test plan already notes and same as it fails on unmodified develop here too — not this PR.

Not checked: rtk started from PowerShell or cmd (no MSYSTEM) rather than Git Bash — #4103's design doc notes that's a different case, since nothing upstream has already globbed for you there, and I only tested from Git Bash. Debug build only, and only this one shape (-delete); didn't try -exec.

@pszymkowiak

pszymkowiak commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for this one, and for the clear write-up. I tested it natively on Windows (MSVC build, real Git Bash, throwaway dirs) and the bug is real: on develop rtk grep -E '.{0,20}foo' f.txt fails, rtk find . -name '*.tmp' -delete errors out with two top-level matches, and with a single top-level match it reports success while leaving sub/c.tmp behind. Your change fixes all of those, plus find . -name '[ab]*.tmp', and nothing else I tried in Git Bash moved (grep --include, git ls-files '*.rs', git log --format with braces, HEAD@{0}).

The catch is callers that don't expand globs. From PowerShell or cmd, develop only worked because the MSYS child did the expansion itself, so with this change these break:

from PowerShell develop this PR
rtk ls *.txt lists the files cannot access '*.txt'
rtk grep foo *.txt matches *.txt: No such file or directory
rtk wc -l *.txt counts '*.txt': No such file or directory

That trade-off is the one #4103 documents. #4544, stacked on it, has rtk expand file operands itself when the calling shell didn't, which brings grep *.txt back; ls and wc aren't covered yet, and I've raised that on #4103. So I'd rather not land the quoting on its own and ship the PowerShell regression without that compensation. I'm leaving this open for now while #4103 and #4544 move, and will come back to it once they do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working find grep windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants