Skip to content

fix: parse leading grep search flags - #2126

Closed
iccccccccccccc wants to merge 2 commits into
rtk-ai:developfrom
iccccccccccccc:fix/grep-leading-flags
Closed

iccccccccccccc wants to merge 2 commits into
rtk-ai:developfrom
iccccccccccccc:fix/grep-leading-flags

Conversation

@iccccccccccccc

Copy link
Copy Markdown

Fixes #2120

Changes

  • let rtk grep accept search flags before the pattern/path, including --fixed-strings
  • normalize leading search flags before invoking rg, so the flag is passed through instead of being treated as the pattern
  • keep the existing trailing-extra-args form working

Verification

  • cargo fmt --check
  • cargo test grep -- --nocapture
  • cargo test grep_clap_accepts -- --nocapture
  • cargo run --quiet -- grep -n --fixed-strings 'fn main' src
  • cargo run --quiet -- grep -A 1 'fn main' src
  • cargo test with C:\Program Files\Git\usr\bin on PATH for the Unix-style commands used by the stream tests

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hollamad hollamad left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally on Linux against the bug's reproducer set. The PR's two tests pass (clap accepts the args), but the resulting search behavior is broken — the leading flags get consumed as the search pattern rather than passed through to ripgrep.

Build:

  • branch: iccccccccccccc:fix/grep-leading-flags @ HEAD
  • cargo test --release --bin rtk grep_clap -> 2 passed
  • cargo build --release -> clean

What works (parse + execute correctly)

Command Result
rtk grep -i pat <file> matches [tracking] ✓
rtk grep -A 2 pat <file> 3 matches with context ✓
rtk grep -c pat <file> bare count ✓

What breaks (parse OK, execution wrong)

Command Expected Actual
rtk grep -rn 'tracking' <dir>/ match line [tracking] match line [n] (i.e. pattern became -rn)
rtk grep -rnE 'tracking|tee' <dir>/ match [tracking], [tee] match [nE] (pattern became -rnE)
rtk grep -E 'tracking|tee' <file> 2 matches rg: missing value for flag -E, 0 matches

Root cause looks like #[arg(allow_hyphen_values = true)] on pattern: String accepts the flag-as-string but the downstream rg invocation then receives pattern="-rn" and tries to literal-match [n] from inside [tracking], instead of -r -n being applied as rg flags.

Suggestion: rather than relaxing clap, detect leading hyphenated args before clap parsing and shift them into the existing EXTRA_ARGS Vec, then pass them to rg as flags. This keeps rtk's strict typing for its own flags while letting standard rg/grep flags reach ripgrep transparently.

Happy to test a follow-up commit if you push one.

@iccccccccccccc

Copy link
Copy Markdown
Author

Thanks, good catch. I pushed a follow-up in e2de3e2.

The issue was the grep/rg flag mismatch you pointed at: clustered grep flags like -rn still let rg see -r, and rg -r means replacement rather than recursive search. The follow-up strips grep-only -r / -n / -E before invoking rg (including clusters like -rn, -rnE, and -rni) and passes the remaining rg args before the pattern/path.

Checked locally:

  • cargo fmt --all -- --check
  • cargo test --release --bin rtk grep_clap -- --nocapture
  • cargo test --release --bin rtk grep_cmd -- --nocapture
  • manual repros for rtk grep -rn tracking, rtk grep -rnE 'tracking\|tee', and rtk grep -E 'tracking\|tee'

@hollamad

Copy link
Copy Markdown

Re-tested locally at e2de3e2. The three failing cases from my last comment all
work now:

Command Result
rtk grep -rn 'tracking' <dir>/ matches [tracking] (2 files)
rtk grep -rnE 'tracking|tee' <dir>/ matches [tracking] + [tee] (4 hits)
rtk grep -E 'tracking|tee' <file> 2 matches, no missing value for flag error

Originally-working cases (-i, -A, -c) still pass, no regression. Cluster
declustering preserves non-stripped flags as expected (-rni keeps -i).
Longform --recursive works.

cargo test --release --all and cargo clippy --release --all-targets clean on
this branch (3 pre-existing curl_cmd::tests failures also reproduce on develop,
unrelated). The two new grep_clap tests and the new normalize tests pass.

Thanks for the quick turnaround.

@rtk-release-bot

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet.

@rtk-release-bot rtk-release-bot Bot added the stale label Sep 3, 2026
@KuSh

KuSh commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix and the clear reproduction. Flag ordering for rtk grep was solved on develop by #2333 (merged 2026-06-14), which replaced the pattern and path positionals with a trailing argument vector, and #2641 (merged 2026-06-26), which turned rtk grep into a faithful grep wrapper with rtk rg for the ripgrep path. On the current build, rtk grep -rn --fixed-strings 'fn main' src and the flag-last form give identical correct output, and rtk grep -A 1 works too. One detail to flag: the -l short for --max-len that this branch keeps was later removed on purpose in #2628 so native grep's -l reaches the engine. Closing as already covered, but please comment or reopen if you still hit an ordering case that breaks.

@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk grep falls back to native grep when --fixed-strings is placed before the pattern/path

4 participants