Skip to content

fix(grep): strip grep -r/-R/-E before ripgrep to prevent silent match mangling - #2254

Closed
PLNech wants to merge 1 commit into
rtk-ai:developfrom
algolia:fix/grep-recursive-flag-mangling
Closed

PLNech wants to merge 1 commit into
rtk-ai:developfrom
algolia:fix/grep-recursive-flag-mangling

Conversation

@PLNech

@PLNech PLNech commented Jun 4, 2026

Copy link
Copy Markdown

Problem

grep_cmd 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 take a value:

  • grep -r/-R (recursive) → rg -r is --replace → grep -rn PATTERN becomes --replace=n, rewriting every match to n (e.g. def foo() → n foo()).
  • grep -E (ERE) → rg -E is --encoding → grep -nE PATTERN feeds 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, updated test_recursive_flag_stripped). cargo clippy clean. One file changed.

Closes #2253.

… 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.
@CLAassistant

CLAassistant commented Jun 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@PLNech

PLNech commented Jun 8, 2026

Copy link
Copy Markdown
Author

CLA signed! Thanks for the great tool 🤖 🩶

@KuSh

KuSh commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

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.

@KuSh KuSh closed this Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep/rg: matches silently rewritten ("def" → "n") — grep -rn/-E collide with ripgrep's value-taking -r/--replace and -E/--encoding

3 participants