Repository navigation
Conversation
…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.
|
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. |
Summary
-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.\|to|translation when--fixed-stringsis active (pattern should be literal).--pcre2,--glob), letting the error propagate so the LLM knows rg is missing rather than silently getting wrong results.Problem
When AI coding assistants invoke
rg -w "class" ., the hook rewrites it tortk grep -w "class" .. Clap did not recognize-w/-i/-F/--pcre2/-gand treated them as parse errors, triggeringrun_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
rtk grepfalls back to native grep when--fixed-stringsis placed before pattern/pathrtk grepcalls system grep instead of ripgrep, breaks-g/--globflagsTest plan
-wi, default path, flags after pattern)--fixed-strings/-Frtk grep -w "class" .,rtk grep --pcre2 "(?<=fn )\w+" .,rtk grep -g "*.rs" "fn run" .,rtk grep --fixed-strings "fn run(" .