Skip to content

fix(grep): translate BRE \| alternation and strip -r flag for rg#206

Merged
pszymkowiak merged 1 commit intortk-ai:masterfrom
heAdz0r:fix/grep-bre-alternation-and-recursive-flag
Feb 18, 2026
Merged

fix(grep): translate BRE \| alternation and strip -r flag for rg#206
pszymkowiak merged 1 commit intortk-ai:masterfrom
heAdz0r:fix/grep-bre-alternation-and-recursive-flag

Conversation

@heAdz0r
Copy link
Contributor

@heAdz0r heAdz0r commented Feb 18, 2026

Summary

  • rtk grep "fn foo\|fn bar" src/ returns 0 results silently — users coming from grep/awk muscle memory use BRE \| for alternation, but rg uses PCRE-style |; rg receives the literal \| and finds nothing.
  • rtk grep "pattern" src/ -r returns 0 results silently — in rg, -r means --replace (requires an argument); passing it alone causes rg to exit with an error. The .or_else(|_| ...) IO-fallback never triggers on a non-zero exit, so stdout is empty.

Reproduction

# Bug 1: BRE alternation
rtk grep "fn run\|fn new" src/
# 🔍 0 for 'fn run\|fn new'   ← should find many matches

# Bug 2: -r flag
rtk grep "pattern" src/ -r
# 🔍 0 for 'pattern'          ← should find matches (rg is recursive by default)

Fix

Bug Root cause Fix
| alternation rg doesn't understand BRE alternation pattern.replace(r"|", "|") before passing to rg (grep_cmd.rs:23)
-r flag rg -r = --replace (needs arg), not recursive Filter out -r/--recursive from extra_args; rg is recursive by default (grep_cmd.rs:33-36)

Files Changed

File Changes Purpose
src/grep_cmd.rs +28 / -1 Both fixes + 2 regression tests

Test plan

  • cargo fmt --all --check — clean
  • cargo clippy --all-targets — 0 errors (pre-existing warnings only)
  • cargo test grep_cmd:: — 8 passed
  • Manual: rtk grep "fn run\|fn new" src/grep_cmd.rs — finds matches
  • Manual: rtk grep "pattern" src/ -r — no longer breaks

🤖 Generated with Claude Code

Two bugs when passing grep-idiom arguments to rtk grep:

1. Pattern alternation `\|` (BRE syntax) returns 0 results because rg
   uses PCRE-style `|`, not BRE `\|`. Users coming from grep/awk muscle
   memory naturally write `rtk grep "fn foo\|fn bar" src/` and get
   empty output with no error.
   Fix: translate `\|` → `|` in pattern before passing to rg.

2. `-r`/`--recursive` flag (grep idiom) silently breaks the search.
   In rg, `-r` means `--replace` and requires an argument — passing it
   alone causes rg to exit with an error. The `.or_else(|_| ...)` fallback
   only catches IO errors (command not found), so the grep fallback never
   triggers; stdout is empty and rtk reports "0 for '<pattern>'".
   Fix: filter out `-r`/`--recursive` from extra_args before building
   the rg command (rg is recursive by default, flag is a no-op).

Adds two regression tests documenting both behaviours.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@pszymkowiak
Copy link
Collaborator

Thank you! Good catch on both issues — silent 0 results is a real pain for users coming from grep.

@pszymkowiak pszymkowiak merged commit 70d1b04 into rtk-ai:master Feb 18, 2026
2 of 3 checks passed
heAdz0r added a commit to heAdz0r/rtk that referenced this pull request Feb 28, 2026
Upstream 0.22.2 sync (all previously missing fixes verified applied):
- fix(lint): propagate linter exit code (rtk-ai#207) — CI false-green fix
- feat: add rtk wc command for compact word/line/byte counts (rtk-ai#175)
- fix(playwright): JSON parser (specs layer) + binary resolution (rtk-ai#215)
- fix(grep): propagate rg exit codes 1/2 (rtk-ai#227)
- fix(git): branch creation not swallowed by list mode (rtk-ai#194)
- fix(git): support multiple -m flags in git commit (rtk-ai#202)
- fix(grep): BRE \| translation + strip -r flag (rtk-ai#206)
- fix(gh): smart markdown body filter for issue/pr view (rtk-ai#214)
- fix(gh): gh run view --log-failed flag passthrough (rtk-ai#159)
- feat(docker): docker compose support (rtk-ai#110)
- feat: hook audit mode (rtk-ai#151)
- feat: tee raw output to file (rtk-ai#134)

Version bump: 0.21.1-fork.19 → 0.22.2-fork.1

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

2 participants