Skip to content

rtk grep fails on patterns containing \[ (backslash appears to be dropped) #4737

Open

Description

@Jianyuanxi

Environment

  • rtk 1.0.1 (rtk-x86_64-pc-windows-msvc), Windows 11, Git Bash (MINGW64)
  • Invoked via the Claude Code hook rewrite (grep → rtk grep)

Repro
File rx.txt:

[a]
x
$ rtk grep -n '^\[' rx.txt
/usr/bin/grep: Invalid regular expression        # exit 2

$ rtk grep -n 'x\|\[a' rx.txt
/usr/bin/grep: Unmatched [, [^, [:, [., or [=    # exit 2

Expected (same patterns, called directly):

$ /usr/bin/grep -n '^\[' rx.txt
1:[a]
$ /usr/bin/grep -n 'x\|\[a' rx.txt
1:[a]
2:x

rtk proxy rg -n '^\[' rx.txt also works correctly.

Guess
The pattern seems to lose its backslashes when rtk forwards it to GNU grep (\[ arrives as [), which matches both error messages. Possibly related to #2817 (rtk grep shells out to GNU grep).

Activity

  1. rtk-wshm-sync-bot commented on Oct 22, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 90%

    Summary

    rtk grep drops backslashes from regex patterns before invoking GNU grep, causing valid patterns like \[ to fail with exit 2.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/commands/grep.rs

    Suggested Actions

    1. Reproduce with rtk grep -n '^\[' rx.txt on Linux/macOS to confirm it's not Windows-specific escaping
    2. Inspect how rtk grep builds the argv/command line passed to GNU grep — likely a shell-quoting or arg-escaping step stri…
    3. Compare with Hook rewrite produces unparseable args for native grep flags and non-existent subcommands #1604 (unparseable args for native grep flags) — may share the same arg-forwarding code path
    4. Add a regression test using patterns with literal brackets/alternation (\[, x\|\[a)

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. TechWizard9999 commented on Oct 22, 2026

    @TechWizard9999
    Contributor

    Hi @KuSh , Could you please assign it to me
    Thanks!!

  3. TechWizard9999 commented on Oct 22, 2026

    @TechWizard9999
    Contributor

    @KuSh Please review this as I have raised the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions