Repository navigation
fix: parse leading grep search flags - #2126
iccccccccccccc wants to merge 2 commits into
Conversation
|
|
hollamad
left a comment
There was a problem hiding this comment.
Tested locally on Linux against the bug's reproducer set. The PR's two tests pass (clap accepts the args), but the resulting search behavior is broken — the leading flags get consumed as the search pattern rather than passed through to ripgrep.
Build:
- branch:
iccccccccccccc:fix/grep-leading-flags@ HEAD cargo test --release --bin rtk grep_clap-> 2 passedcargo build --release-> clean
What works (parse + execute correctly)
| Command | Result |
|---|---|
rtk grep -i pat <file> |
matches [tracking] ✓ |
rtk grep -A 2 pat <file> |
3 matches with context ✓ |
rtk grep -c pat <file> |
bare count ✓ |
What breaks (parse OK, execution wrong)
| Command | Expected | Actual |
|---|---|---|
rtk grep -rn 'tracking' <dir>/ |
match line [tracking] |
match line [n] (i.e. pattern became -rn) |
rtk grep -rnE 'tracking|tee' <dir>/ |
match [tracking], [tee] |
match [nE] (pattern became -rnE) |
rtk grep -E 'tracking|tee' <file> |
2 matches | rg: missing value for flag -E, 0 matches |
Root cause looks like #[arg(allow_hyphen_values = true)] on pattern: String accepts the flag-as-string but the downstream rg invocation then receives pattern="-rn" and tries to literal-match [n] from inside [tracking], instead of -r -n being applied as rg flags.
Suggestion: rather than relaxing clap, detect leading hyphenated args before clap parsing and shift them into the existing EXTRA_ARGS Vec, then pass them to rg as flags. This keeps rtk's strict typing for its own flags while letting standard rg/grep flags reach ripgrep transparently.
Happy to test a follow-up commit if you push one.
|
Thanks, good catch. I pushed a follow-up in The issue was the grep/rg flag mismatch you pointed at: clustered grep flags like Checked locally:
|
|
Re-tested locally at
Originally-working cases (
Thanks for the quick turnaround. |
|
This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet. |
|
Thanks for the fix and the clear reproduction. Flag ordering for rtk grep was solved on develop by #2333 (merged 2026-06-14), which replaced the pattern and path positionals with a trailing argument vector, and #2641 (merged 2026-06-26), which turned rtk grep into a faithful grep wrapper with rtk rg for the ripgrep path. On the current build, rtk grep -rn --fixed-strings 'fn main' src and the flag-last form give identical correct output, and rtk grep -A 1 works too. One detail to flag: the -l short for --max-len that this branch keeps was later removed on purpose in #2628 so native grep's -l reaches the engine. Closing as already covered, but please comment or reopen if you still hit an ordering case that breaks. |
Fixes #2120
Changes
rtk grepaccept search flags before the pattern/path, including--fixed-stringsrg, so the flag is passed through instead of being treated as the patternVerification
cargo fmt --checkcargo test grep -- --nocapturecargo test grep_clap_accepts -- --nocapturecargo run --quiet -- grep -n --fixed-strings 'fn main' srccargo run --quiet -- grep -A 1 'fn main' srccargo testwithC:\Program Files\Git\usr\binonPATHfor the Unix-style commands used by the stream tests