Skip to content

rtk ast-grep: the per-file cap cuts inside a structural match, so the kept output is a truncated code fragment #4016

Open

Description

@KuSh

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

ast-grep emits one path:line:content line per source line of a match, and a structural match routinely spans several — that is the stated reason the filter exists. DEFAULT_MAX_PER_FILE = 5 in src/cmds/system/ast_grep_cmd.rs counts lines, so take(max_per_file) can stop halfway through a single match:

$ ast-grep run -p 'LazyLock::new($$$)' -l rust src/cmds/system/ctest_cmd.rs | wc -l
24                       # 6 matches
$ rtk ast-grep run -p 'LazyLock::new($$$)' -l rust src/cmds/system/ctest_cmd.rs
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+):\s+(.+?)\s+\.{2,}...
src/cmds/system/ctest_cmd.rs:27:    )
src/cmds/system/ctest_cmd.rs:28:    .expect("invalid ctest result regex")
  … 19 more match line(s) in src/cmds/system/ctest_cmd.rs

The first match alone is six lines, so the reader gets five-sixths of one match and nothing else — an unbalanced fragment with its closing }); cut off. For a filter whose whole point is block-shaped hits, a file's budget is often less than one complete match.

The fix is a design call rather than a bug fix, which is why it is not part of #3857: cap by match instead of by line, grouping consecutive line runs per file (or read the match boundaries from --json and use them to segment the plain output). Either way the per-file hint should then count matches, and the wording can go back to "matches".

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 87%

    Summary

    ast-grep's per-file cap counts source lines instead of matches, so take(max_per_file) can truncate mid-match; switch to counting/grouping complete matches (e.g. via --json boundaries).

    📁 Relevant files
    • src/cmds/system/ast_grep_cmd.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