Skip to content

grep: detect format flags bundled inside short-flag clusters - #2544

Closed
EtaCassiopeia wants to merge 1 commit into
rtk-ai:developfrom
EtaCassiopeia:fix/grep-clustered-format-flags
Closed

EtaCassiopeia wants to merge 1 commit into
rtk-ai:developfrom
EtaCassiopeia:fix/grep-clustered-format-flags

Conversation

@EtaCassiopeia

@EtaCassiopeia EtaCassiopeia commented Jun 22, 2026 •

Copy link
Copy Markdown

Follow-up to #2543 — this fixes the second part of it.

has_format_flag checks extra_args for things like -l/-c/-o so it knows to skip the usual match-line parsing and just pass rg's output straight through (since those flags change rg's output format entirely). That check only matches exact tokens though, and extract_pattern_path merges plain boolean short flags into one combined token before they get here — so -rln becomes -ln after r is stripped, and -ln never matches -l exactly.

End result: something like rtk grep -rln pattern path runs rg with -l correctly, but then tries to parse the (now filenames-only) output as if it were normal file:line:content matches, finds nothing that fits that shape, and reports "0 matches" even when there clearly are matches. That's worse than just erroring, since it looks identical to a real empty result.

Fix is small — has_format_flag now also scans the individual letters of a short cluster (long flags and exact single-flag matches are unaffected). Added a few tests for the clustered case plus one to make sure long flags like --ignore-case don't get walked character-by-character by mistake.

Tested locally:

  • rtk grep -rln pattern path now correctly falls through to passthrough and shows the real file list instead of "0 matches in 0 files"
  • rtk grep -rn pattern path unaffected
  • existing grep_cmd test suite passes (72 tests)

closes #2543

has_format_flag only matched exact tokens like "-l", but
extract_pattern_path merges boolean short flags into one cluster
(e.g. `-rln` becomes `-ln` after stripping `r`). A bundled -l/-c/-o/-L/-Z
never matched, so grep_cmd tried to NUL-parse rg's output as if it were
normal match lines, even though -l/-c/etc. change rg's output shape
entirely. The result was a false "0 matches" instead of the real answer.

Now has_format_flag also scans the individual letters of a short-flag
cluster, so a bundled format flag correctly falls through to the
existing passthrough path.
@CLAassistant

CLAassistant commented Jun 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@routesmith

Copy link
Copy Markdown

Confirmed on WSL2 (Ubuntu 24.04) / Linux x86_64, rtk 0.42.4, invoked via the Claude Code auto-rewrite hook — a third platform alongside the macOS report in #2647 and the Linux/0.42.1 report in #2563.

Traced independently to exactly the cause described here: extract_pattern_path → parse_cluster collapses -rli to -li (strips r), and has_format_flag (grep_cmd.rs) only exact-matches -l, so the -l inside the cluster is invisible. Passthrough is skipped, but -li is still forwarded to rg → rg emits filenames-only output → every line fails parse_match_line (file\0line:content shape) and is silently dropped → 0 matches in 0 files: on content that plainly exists.

Minimal repro:

grep -rli "foo\|bar" .        # -> "0 matches in 0 files:" (false)
grep -r -l -i "foo\|bar" .    # -> correct (l visible as its own token -> passthrough)
rtk proxy grep -rli "foo\|bar" .   # -> correct (bypasses filter)

Real-world impact matched #2563 almost exactly: the false zero read as an LLM hallucination and nearly triggered a destructive "correction" (concluding correct, existing content did not exist). The by_file.is_empty() + non-empty stdout case really should fall back to raw output per RTK's own documented fallback rule, independent of the cluster-detection fix here. This PR fixes the trigger; a fallback guard would catch the whole class. Thanks for the fix — would like to see it land.

@routesmith

Copy link
Copy Markdown

Follow-up: this is already fixed on develop / in stable v0.43.0 via the grep_cmd.rs → src/cmds/system/search.rs refactor (#2641), which lands both a cluster-aware has_format_flag (short flags scanned per-letter, so -rli → -l routes to passthrough) and an unparsed_signal() guard that re-runs raw when stdout is non-empty but nothing parses. Verified the repro returns correct results on v0.43.0. Since this PR targets the now-removed grep_cmd.rs, it looks superseded and can probably be closed in favor of the search refactor.

@KuSh

KuSh commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks for digging into the bundled-cluster case and writing the repro. This landed by way of #2550 (merged 2026-06-23), which rewrote grep into src/cmds/system/search.rs; its has_format_flag scans every letter of a short cluster for c/l/L/o/q/b/Z/z and matches long flags against an explicit list, so -ln and -rln now route to passthrough exactly as you described. Develop also carries unit tests for -rl/-rc/-rq/-rln/-cr and an end-to-end -rln test citing #2543, so the coverage your patch added is present too. Running the repro on develop lists the matching files instead of reporting 0 matches. Closing as covered, but please comment or reopen if you see a flag shape that still slips through.

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

grep: -r / -rn rejected on 0.42.4 (and -rln returns a false "0 matches" on latest develop)

4 participants