Repository navigation
fix(grep): accept GNU grep flags -E/-r/-R/-i/-w/-A/-B/-C (#2614), and stop -E/-r from silently eating the next arg (#2253) - #3119
Conversation
rtk grep rejected the most common real-world grep invocations at the clap layer, so the hook's rewrite guaranteed a parse failure and a silent raw fallback with 0 tokens saved. Measured over 5 weeks of local tracking (11,932 commands): 2,142 parse failures, 1,970 of them grep (92%), all with fallback_succeeded=1 — they read as success while saving nothing. Upstream: rtk-ai/rtk#2614. Root cause: the rejected flags arrive BEFORE the pattern/path positionals, so trailing_var_arg never collected them into extra_args. grep_cmd already had -r skip and format-flag passthrough logic, but clap failed first so that code was unreachable. Two classes of flag, handled differently: Dropped (accepted, never forwarded to rg) — rg's short forms collide AND take a value, so forwarding would silently swallow the next argument, which is the mechanism behind #2253: grep -E = extended regex | rg -E = --encoding=VALUE grep -r = recursive | rg -r = --replace=VALUE Dropping is correct: rg is already recursive + extended-regex by default. Forwarded (identical semantics in both): -i, -w, -A/-B/-C <NUM>. No breaking change: -l still means max_len. Aligning it with GNU's --files-with-matches would change existing CLI behavior and is left out deliberately. Verified against /usr/bin/grep: -E alternation, -i case-insensitivity and -w word boundaries all produce identical match sets. Tests: 4 regression tests, incl. a guard that dropping a flag does not consume the following token. Suite 2147 -> 2151 passing; the 3 failures in hooks::rewrite_cmd::tests::unattestable_passthrough are pre-existing on HEAD (verified by stashing this change) and unrelated.
|
|
|
Closing — superseded by upstream, and no longer mergeable. Since this PR was filed, upstream
This PR only added individual clap fields for Thanks to the maintainers for the more thorough fix. Closing in favor of it. (Refs #2614, #2253.) |
Fixes the clap-layer rejection described in #2614, and closes a silent-corruption path that is the likely mechanism behind #2253.
Problem
rtk greprejected the most common real-world grep invocations, so the hook's rewrite guaranteed a parse failure followed by a silent raw fallback at 0 tokens saved.From a 5-week local tracking DB (11,932 commands): 2,142 parse failures, 1,970 of them
grep(92%), every one withfallback_succeeded=1— they read as success while saving nothing. This independently reproduces @-reporter's 4,416/90-day figure in #2614.Root cause: the rejected flags arrive before the
pattern/pathpositionals, sotrailing_var_argnever collected them intoextra_args.grep_cmdalready had-r-skip and format-flag passthrough logic, but clap failed first, so that code was unreachable.Two classes of flag, handled differently
Dropped — accepted by clap, never forwarded to rg. Their rg short forms collide with grep's and take a value, so forwarding silently consumes the following argument:
-E--encoding=VALUE-r--replace=VALUEThis is very likely the mechanism behind #2253 ("matches silently rewritten
def→n"): a forwarded-E/-reats the pattern. Dropping them is semantically correct — rg is recursive and extended-regex by default, so grep's intent is already satisfied.Forwarded — identical semantics in both tools:
-i,-w,-A/-B/-C <NUM>.Not changed (deliberate)
-lstill meansmax_len. Aligning it with GNU's--files-with-matcheswould break existing CLI behavior for anyone scriptingrtk grep -l, so it is left alone and called out in a test. Happy to do it in a follow-up if maintainers want the breaking change.Verification
Against
/usr/bin/grep(not the rtk-rewritten shell alias):-E 'fn run|fn filter'-ci LAZY_STATIC-w runAll five previously-failing forms now parse:
-nE,-rn --include,-c,-n -A2,-i.Tests
4 regression tests, including a guard that dropping a flag does not consume the following token (the #2253 shape). Suite 2147 → 2151 passing.
The 3 failures in
hooks::rewrite_cmd::tests::unattestable_passthroughare pre-existing on HEAD — verified by stashing this change and re-running (identical 3 failures, identical 2147 pass count). Unrelated to this PR.cargo fmt --allclean,cargo clippy --all-targets0 warnings.