fix(grep): respect the invoked tool — grep runs grep, rg runs ripgrep#2183
Open
olsavmic wants to merge 1 commit into
Open
fix(grep): respect the invoked tool — grep runs grep, rg runs ripgrep#2183olsavmic wants to merge 1 commit into
olsavmic wants to merge 1 commit into
Conversation
|
|
a437345 to
bc216fc
Compare
`rg` and `grep` were both rewritten to `rtk grep`, which always ran ripgrep as its backend. That collapsed the two tools' identities and broke whichever one didn't match the backend: ripgrep-only flags (`--glob`, `-g`, `-P`, `--files`) failed because the command fell through to system grep, while grep commands silently misbehaved under rg (e.g. `-r` is recursive in grep but `--replace` in rg, so `grep -rn foo` rewrote every match). Preserve the invoked tool's identity from the rewrite onward, so each command runs its own backend with its own semantics and there is never any crossover: - rewrite: `grep ...` -> `rtk grep`, `rg ...` -> `rtk rg` (split the single rule). - `rtk rg` (new subcommand): faithful ripgrep passthrough — all args forwarded verbatim, so rg-only flags work in any order — with RTK's by-file filtering (and raw streaming for info/list flags like `--files`). - `rtk grep`: now runs system grep (grep semantics: BRE, grep flags, `-r`), parsing grep's `file:line:content` output; `-t/--file-type` maps to grep `--include`. - shared `emit_filtered` takes a per-backend line parser (rg NUL form vs grep colon form). Fixes rtk-ai#2167. Also addresses rtk-ai#2060, rtk-ai#1651. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
bc216fc to
20eedd1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rgandgrepwere both rewritten tortk grep, which always ran ripgrep as its backend. That collapsed the two tools into one and broke whichever didn't match the backend:--glob,-g,-P,--files) failed — the command fell through to system grep, which rejects them (rtk grep calls system grep instead of ripgrep — breaks -g/--glob flags #2167, rg --files is rewritten to rtk grep --files #2060, rtk fails to handle --pcre2 flag when agent tries to use rg #1651);-ris recursive in grep but--replacein rg, sogrep -rn foorewrote every match toninstead of searching.This PR preserves the invoked tool's identity from the rewrite onward, so each command runs its own backend with its own semantics — no crossover in either direction.
Design
grep …rtk grep-r)rg …rtk rg--glob/-g/-P/--files, any order)^(rg\|grep)rule intogrep → rtk grepandrg → rtk rg.rtk rg(new subcommand): faithful ripgrep passthrough — all args forwarded verbatim (flattrailing_var_arg, so leading flags never break clap) — with RTK's by-file filtering; info/list flags (--files,--json, …) stream raw.rtk grep: now runs system grep, parsing grep'sfile:line:contentoutput;-t/--file-typemaps to grep--include. Keeps its documented-l/-m/-t/--context-onlyinterface.emit_filteredtakes a per-backend line parser (rg NUL formfile\0line:contentvs grep colon form).Why not "route everything through one backend"
The first iterations of this PR tried that (route rg-style fallbacks to rg). Review found it corrupts the other tool because grep and rg share short flags with conflicting meanings — most dangerously
grep -rn foo→ rg--replace=n→ every match rewritten. There is no safe way to run one tool's syntax through the other; the only correct fix is to keep them separate.Test plan
cargo fmt --check && cargo clippy --all-targets && cargo test— 1985 passed, 7 ignored, clippy clean, fmt cleanrg→rtk rg,grep→rtk grep),parse_grep_line,grep_type_glob,is_raw_passthrough_flagNotes
rtk grepnow runs system grep rather than ripgrep — a deliberate behavior change so grep semantics are respected (loses rg's speed/gitignore-awareness forgrepcommands; that's grep being grep).rtk rgis the path for ripgrep.