Skip to content

rtk ast-grep: -C/-A/-B context lines are counted as matches, eating the per-file cap and inflating the overflow hint #4015

Open

Description

@KuSh

Follow-up from #3857 (not a regression it introduced — the command is new).

ast-grep run prefixes context lines exactly like match lines, with ::

$ ast-grep run -p 'LazyLock::new($$$)' -l rust -C 2 src/cmds/system/ctest_cmd.rs
src/cmds/system/ctest_cmd.rs:22:const MAX_FAILED_BLOCK_ENTRIES: usize = truncate::reduced(CAP_LIST, 5);
src/cmds/system/ctest_cmd.rs:23:
src/cmds/system/ctest_cmd.rs:24:static TEST_RE: LazyLock<Regex> = LazyLock::new(|| {
...

MATCH_LINE_RE in src/cmds/system/ast_grep_cmd.rs has no way to tell the two apart, so with -C 2 the five-line per-file budget is spent almost entirely on leading context before any match is reached, and the overflow count is inflated by context lines on top of the line-vs-match gap:

$ rtk ast-grep run -p 'LazyLock::new($$$)' -l rust -C 2 src/cmds/system/ctest_cmd.rs
src/cmds/system/ctest_cmd.rs:22:const MAX_FAILED_BLOCK_ENTRIES: usize = ...
src/cmds/system/ctest_cmd.rs:23:
src/cmds/system/ctest_cmd.rs:24:static TEST_RE: LazyLock<Regex> = LazyLock::new(|| {
src/cmds/system/ctest_cmd.rs:25:    Regex::new(
src/cmds/system/ctest_cmd.rs:26:        r"(?s)^\s*(?:\d+/(\d+)\s+)?Test\s+#(\d+):...
  … 27 more match line(s) in src/cmds/system/ctest_cmd.rs

The file holds 6 matches. Asking for context makes the output strictly less useful than not asking for it.

src/cmds/system/search.rs solves this for grep/rg by forcing --null and capturing the separator: ^([^\x00]+)\x00(\d+)([:-]) (search.rs:287, :739), where : means match and - means context. ast-grep has no equivalent separator in plain mode, so the fix probably means either honouring context as a unit attached to its match, or declining to filter when a context flag is present (the way has_format_flag() declines other shapes at search.rs:751).

Related: #1161, #1313.

Verified with ast-grep 0.45.3 in Docker (node:22-trixie-slim, LC_ALL=C).

Activity

  1. rtk-wshm-sync-bot commented on Sep 13, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟡 Priority medium
    🎯 Confidence 90%

    Summary

    ast-grep's -C/-A/-B context lines are indistinguishable from match lines in the filter's regex, so they consume the per-file cap and inflate the overflow count; needs a separator-based fix like search.rs's grep/rg handling or disabling filtering when context flags are present.

    📁 Relevant files
    • src/cmds/system/ast_grep_cmd.rs
    • src/cmds/system/search.rs

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibugpriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions