Repository navigation
Conversation
… mangling
`rtk grep` forwards extra args to ripgrep, but only skipped a standalone `-r`.
grep and rg disagree on several flag letters, and the clash is destructive
because rg's variants are value-taking:
grep -r / -R (recursive) -> rg -r is --replace (takes a value)
grep -E (ERE) -> rg -E is --encoding (takes a value)
So a muscle-memory `grep -rn PATTERN` reaches rg as `--replace=n` and silently
rewrites every match to "n" (e.g. `def foo()` is reported as `n foo()`), and
`grep -nE PATTERN` feeds PATTERN to `--encoding` ("unknown encoding: PATTERN").
Both are silent corruptions that make AI agents draw wrong conclusions about code.
Generalize the extra-args sanitizing into `strip_grep_only_flags`, which drops
grep's `-r`/`-R`/`-E` (and the `--recursive`/`--dereference-recursive`/
`--extended-regexp` long forms) before forwarding to rg — including from combined
short bundles (`-rn` -> `-n`, `-nE` -> `-n`) — while preserving value-taking flags
and their values (`-A3`, `-tpy`, `-erare`). rg recurses and is ERE by default, so
dropping these is safe.
Adds unit coverage for bundles, long forms, and value-flag preservation.
|
CLA signed! Thanks for the great tool 🤖 🩶 |
|
Thanks for the clear diagnosis, the -r/--replace and -E/--encoding collision was real. It was resolved a different way in #2641 (merged 2026-06-26), which stopped substituting ripgrep for grep: rtk grep now runs real grep with your flags untouched and rtk rg runs real ripgrep, so grep-only flags never reach rg and there is nothing left to strip. On current develop rtk grep -rn foo . and rtk grep -nE 'fo+' file return correct matches with no rewriting. Note that #2253 stays open for a separate residual on the ripgrep side, rtk rg -r REPL world file still misreads the short-form value flag, which is a different code path from the one this patch touched, so that one is worth tracking there. If you think part of your fix is still needed on develop, please comment here or reopen. |
Problem
grep_cmdforwards extra args to ripgrep but only skipped a standalone-r.grepandrgdisagree on several flag letters, and the clash is destructive because rg's variants take a value:grep -r/-R(recursive) → rg-ris--replace→grep -rn PATTERNbecomes--replace=n, rewriting every match ton(e.g.def foo()→n foo()).grep -E(ERE) → rg-Eis--encoding→grep -nE PATTERNfeeds PATTERN to--encoding("unknown encoding: PATTERN").Both are silent/confusing corruptions of read-only code inspection. Details and repro in #2253.
Fix
Generalize the extra-args sanitizing into
strip_grep_only_flags, which drops grep's-r/-R/-E(and the matching long forms) before forwarding to rg — including from combined short bundles (-rn→-n,-nE→-n) — while preserving value-taking flags and their values (-A3,-tpy,-erare). rg recurses and is ERE by default, so dropping these is safe.Tests
Unit coverage for bundles, long forms, and value-flag preservation (
test_strip_grep_only_flags_bundles, updatedtest_recursive_flag_stripped).cargo clippyclean. One file changed.Closes #2253.