Skip to content

fix(grep): prevent silent fallback to system grep when rg flags precede pattern - #2434

Closed
vlasky wants to merge 2 commits into
rtk-ai:developfrom
vlasky:fix/grep-flag-position-fallback
Closed

vlasky wants to merge 2 commits into
rtk-ai:developfrom
vlasky:fix/grep-flag-position-fallback

Conversation

@vlasky

@vlasky vlasky commented Jun 14, 2026

Copy link
Copy Markdown

Summary

  • Add explicit clap definitions for commonly-used ripgrep flags (-w, -i, -F/--fixed-strings, --pcre2, -g/--glob) so they parse correctly regardless of position relative to the pattern argument, preventing clap rejection and silent fallback to system grep.
  • Skip BRE \| to | translation when --fixed-strings is active (pattern should be literal).
  • Block grep fallback when rg-only flags are present (--pcre2, --glob), letting the error propagate so the LLM knows rg is missing rather than silently getting wrong results.
  • Translate long-form flags to grep-compatible short forms when fallback does activate.
  • Emit a stderr warning when the grep fallback fires.

Problem

When AI coding assistants invoke rg -w "class" ., the hook rewrites it to rtk grep -w "class" .. Clap did not recognize -w/-i/-F/--pcre2/-g and treated them as parse errors, triggering run_fallback() which silently executed system grep instead of ripgrep. The LLM believed it was calling ripgrep (with its output format, flag semantics, and default recursion) but received GNU grep output instead, causing confusion and incorrect conclusions.

Related Issues

Test plan

  • 9 new unit tests covering flag parsing (single flags, combined -wi, default path, flags after pattern)
  • 5 new unit tests covering grep fallback behavior (blocked by rg-only flags, allowed for compatible flags, long-form translation)
  • 2 new unit tests for BRE translation skip with --fixed-strings/-F
  • Full test suite passes (2166 tests)
  • Manual testing: rtk grep -w "class" ., rtk grep --pcre2 "(?<=fn )\w+" ., rtk grep -g "*.rs" "fn run" ., rtk grep --fixed-strings "fn run(" .

vlasky added 2 commits June 14, 2026 11:34
…ck to system grep

When AI coding assistants invoke `rg -w "class" .`, the hook rewrites it to
`rtk grep -w "class" .`. Previously, clap did not recognize -w/-i/-F/--pcre2/-g
and treated them as errors, triggering run_fallback() which silently executed
system grep instead of ripgrep. This caused confusing results for LLMs since
they believed they were calling ripgrep (with its output format, flag semantics,
and default recursion) but received GNU grep output instead.

Add explicit clap definitions for these commonly-used ripgrep flags so they
parse successfully regardless of position relative to the pattern argument.
The flags are re-injected into extra_args before calling grep_cmd::run().

Fixes rtk-ai#2120
…fallback

Two pre-existing bugs made more visible by the flag-position fix:

1. The BRE \| to | translation was applied unconditionally, corrupting
   patterns when --fixed-strings/-F was active. Now skipped when the
   extra_args contain a fixed-strings flag.

2. When rg is unavailable, the grep fallback now:
   - Refuses to fall back when rg-only flags are present (--pcre2,
     --glob/-g), letting the error propagate so the LLM knows rg is
     missing rather than silently getting wrong results from grep.
   - Translates long-form flags (--fixed-strings, --word-regexp,
     --ignore-case) to grep-compatible short forms.
   - Emits a stderr warning when the fallback activates.
@CLAassistant

CLAassistant commented Jun 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@KuSh

KuSh commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the detailed writeup, it described the failure mode exactly. This was fixed a different way in #2641 (merged 2026-06-26), which replaced grep_cmd.rs with a search module that runs whichever engine was actually invoked, passing its flags through untouched, so grep is never substituted for rg and flags before the pattern parse fine. On current develop rtk rg -w, rtk rg -g '*.txt' and rtk rg --pcre2 all run ripgrep as expected, and issues #2120, #2167 and #2064 were closed against that change. The fixed-strings and flag-position cases your patch handled are covered there, so the clap definitions and the fallback blocking are no longer needed. If something in this area still misbehaves on the latest 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

3 participants