Skip to content

fix(grep): use PCRE-converted pattern with -E flag in grep fallback - #2348

Closed
shiloong wants to merge 1 commit into
rtk-ai:developfrom
shiloong:fix/grep-fallback-pattern
Closed

shiloong wants to merge 1 commit into
rtk-ai:developfrom
shiloong:fix/grep-fallback-pattern

Conversation

@shiloong

@shiloong shiloong commented Jun 9, 2026

Copy link
Copy Markdown

Problem

When rtk grep falls back from rg to grep (when rg is unavailable), the fallback uses the original BRE pattern and lacks the -E (extended regex) flag. This causes patterns with BRE-to-PCRE conversion to fail silently:

  1. User passes a BRE pattern like fn foo\|pub.*bar to rtk grep
  2. rtk converts it to PCRE fn foo|pub.*bar for rg
  3. When rg is unavailable, grep receives fn foo|pub.*bar (PCRE) but without -E, grep treats | as a literal pipe character
  4. Result: zero matches returned, misleading AI agents into thinking the pattern was not found

Fix

  • Use the PCRE-converted pattern (rg_pattern) instead of the original BRE pattern in the grep fallback
  • Add -E (extended regex) flag to the grep invocation so | alternation is interpreted correctly
-            grep_cmd.args(["-rnHZ", pattern, path]).args(extra_args);
+            grep_cmd.args(["-rnHEZ", &rg_pattern, path]).args(extra_args);

Before

$ rtk grep "fn foo\|pub.*bar" .
0 matches for 'fn foo\|pub.*bar'

After

$ rtk grep "fn foo\|pub.*bar" .
X matches in YF:

[file] src/main.rs (3):
   10: fn foo() { ... }
   25: pub struct Bar { ... }

@shiloong
shiloong force-pushed the fix/grep-fallback-pattern branch from 514ea7d to 09157e9 Compare June 9, 2026 08:19
@CLAassistant

CLAassistant commented Jun 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

When rtk grep falls back from rg to grep (when rg is unavailable), the
fallback uses the original BRE pattern and lacks -E (extended regex)
support. This causes patterns with alternation (e.g. 'fn foo\|pub.*bar')
to fail silently:

- User passes BRE pattern 'fn foo\|pub.*bar' to rtk grep
- rtk converts to PCRE 'fn foo|pub.*bar' for rg
- When rg is unavailable, grep receives original BRE pattern without -E
- grep interprets | as literal pipe, returning zero matches
- Result: misleading AI agents into thinking the pattern was not found

Fix:
- Use PCRE-converted pattern (rg_pattern) instead of original BRE pattern
- Add -E (extended regex) flag to grep invocation

Signed-off-by: Shile Zhang <[email protected]>
@KuSh

KuSh commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks for digging into this. The BRE to PCRE conversion this fixes no longer exists on develop: PR #2641 (merged 2026-06-26) stopped substituting one engine for the other, so rtk grep now runs real grep with your own pattern and a separate rtk rg runs ripgrep. grep_cmd.rs was renamed to search.rs in that change and the fallback path you patched is gone, which removes the whole class of pattern mismatch you described. If you still hit a case where a grep pattern is mangled on the current develop, please comment here or reopen with the command and we will take another look.

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

3 participants