Skip to content

fix(grep): normalize rg-compatible rewrite flags#1899

Open
hhh2210 wants to merge 2 commits into
rtk-ai:developfrom
hhh2210:fix/grep-rg-flag-rewrite
Open

fix(grep): normalize rg-compatible rewrite flags#1899
hhh2210 wants to merge 2 commits into
rtk-ai:developfrom
hhh2210:fix/grep-rg-flag-rewrite

Conversation

@hhh2210
Copy link
Copy Markdown

@hhh2210 hhh2210 commented May 16, 2026

Summary

  • Normalize common grep/rg flags before routing hook rewrites to rtk grep, so agent-generated searches stop producing clap parse failures for supported shapes.
  • Preserve native search intent for -rn, -A/-B/-C, -E, -i, -o, -c, -l/-L, and -g/--glob by moving compatible flags after the rtk grep positionals.
  • Keep conservative passthrough behavior for version/help calls, shell expansion, and empty quoted patterns instead of freezing shell semantics during rewrite.

Fixes #1824.
Partially addresses #1833 by reducing hook-rewrite fallbacks for common grep/rg flag shapes.
Refs #1604, #236.

Test plan

  • cargo fmt --all --check
  • cargo clippy --all-targets -- -D warnings
  • cargo test
  • Manual: target/debug/rtk hook check 'grep -n -A 40 begin{abstract} /tmp/file.tex' -> rtk grep 'begin{abstract}' /tmp/file.tex -A 40
  • Manual: target/debug/rtk hook check 'grep -ic warning /tmp/bib.log' -> rtk grep warning /tmp/bib.log -i -c
  • Manual: target/debug/rtk hook check 'rg -n "Project Guidelines" --glob CLAUDE.md .' -> rtk grep "Project Guidelines" . --glob CLAUDE.md
  • Manual: target/debug/rtk hook check 'rg --version' -> no rewrite
  • Manual: target/debug/rtk hook check 'rg "$PATTERN" src' -> no rewrite
  • Manual: target/debug/rtk hook check "rg '' src" -> no rewrite

Local environment used for manual verification: macOS 26.5 (build 25F71), arm64, Apple M4. CI is expected to cover the repository's broader platform matrix.

Notes

This intentionally stays scoped to rewrite normalization. It does not attempt to redesign rtk grep, change find, or close the non-existent-subcommand portion of #1604.

For #1833 specifically, this PR reduces fallback/parse failures in the hook-generated command path, but it does not fully extend the rtk grep argument parser, implement the GNU-grep literal -F preservation rule when -E is absent, or resolve the direct rtk grep -l / --max-len collision.

Copilot AI review requested due to automatic review settings May 16, 2026 05:14
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 16, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the command-rewrite registry so hook rewrites to rtk grep normalize common grep/rg flag orderings (notably moving compatible flags after <pattern> <path>), reducing clap parse failures and preserving intended searches for common flag shapes.

Changes:

  • Add a grep/rg-specific rewrite path that shell-splits the command, recognizes a set of supported flags, and reorders them into an rtk grep <pattern> [path] <extra_args...> form.
  • Introduce shell-quoting for rewritten args to produce a safely re-runnable command string.
  • Add unit tests covering normalized flag order, --glob normalization, and conservative skipping for $PATTERN / empty quoted patterns.

Comment thread src/discover/registry.rs
Comment on lines +629 to +645
fn shell_quote_arg(arg: &str) -> String {
if !arg.is_empty()
&& arg.chars().all(|c| {
c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.' | '/' | ':' | '=' | '~')
})
{
arg.to_string()
} else if !arg.is_empty()
&& arg.chars().all(|c| {
c.is_ascii_alphanumeric() || c.is_ascii_whitespace() || matches!(c, '_' | '-' | '.')
})
{
format!("\"{}\"", arg)
} else {
format!("'{}'", arg.replace('\'', "'\\''"))
}
}
Comment thread src/discover/registry.rs Outdated
Comment on lines +652 to +655
'i' | 'o' | 'c' | 'F' | 'w' | 'H' | 'h' => {
flags.push(format!("-{}", flag));
true
}
@hhh2210
Copy link
Copy Markdown
Author

hhh2210 commented May 16, 2026

cc @pszymkowiak @aeppling — CLA is now signed, PR should be unblocked for review.

@pszymkowiak this PR sits in the same hook-rewrite area you recently merged in #1718; the change here is the grep/rg flag-normalization half discussed in #1604/#1833/#1824.

I kept it deliberately narrow: it normalizes common grep/rg flag placement before rtk grep, while leaving the broader non-existent-subcommand handling and a full rtk grep redesign out of scope. Test plan and local validation are in the PR body.

@hhh2210 hhh2210 force-pushed the fix/grep-rg-flag-rewrite branch from 59d89be to 41f3551 Compare May 16, 2026 05:31
@hhh2210
Copy link
Copy Markdown
Author

hhh2210 commented May 16, 2026

Addressed the Copilot review points in the latest push:

  • unquoted shell glob metacharacters (*, ?, [) now conservatively skip rewrite, so shell expansion semantics are not changed
  • -h/-H are normalized to --no-filename/--with-filename, avoiding clap -h/--help collisions when passed through rtk grep

I also added regression coverage for both cases.

Keep grep-specific short flag normalization separate from ripgrep so hook rewrites do not change rg semantics. In particular, skip rg -h/-L/-r/-R while preserving compatible rg flags and grep -h/-L behavior.
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.

rg auto-rewrite to rtk grep breaks normal ripgrep flag ordering (--glob, -g, --version)

3 participants