Skip to content

rtk ast-grep: a directory named after a subcommand disables filtering for the whole implicit-run invocation #4162

Open

Description

@KuSh

Pre-existing on develop (verified at 727ee6e6 with the binary built from it, rtk 0.48.0). Arrived in 1bd299b5 (#4121, merged). Surfaced while reviewing #4150, which is unrelated to it.

Summary

In the implicit-run form, filters_this_invocation disqualifies the whole invocation when any pre--- positional matches a subcommand name:

let identified_as_run = positionals.first() == Some(&"run");
if !identified_as_run && positionals.iter().any(|n| OTHER_SUBCOMMANDS.contains(n)) {
    return false;
}

A path argument spelled like a subcommand — test/, scan/, new/ — therefore turns off filtering for a plain pattern search. rtk ast-grep -p '…' src test passes 60 raw lines through where rtk ast-grep run -p '…' src test returns 13.

The trade-off is deliberate and the doc comment states it: reading lsp as a run would hand an editor a closed stdin, which is a correctness failure, while losing compression on a path spelled like a subcommand is not. This issue is about recovering the compression, not about reverting that order.

Reproduction

Needs ast-grep on PATH (verified with 0.45.3).

R=$(mktemp -d); cd "$R"
mkdir -p src test
for d in src test; do
  for i in $(seq 1 30); do printf 'fn f%02d() { println!("%s %d"); }\n' "$i" "$d" "$i"; done > $d/a.rs
done

ast-grep     -p 'println!($$$)' src test | wc -l   # 60  native
rtk ast-grep run -p 'println!($$$)' src test | wc -l   # 13  filtered
rtk ast-grep     -p 'println!($$$)' src test | wc -l   # 60  unfiltered
rtk ast-grep     -p 'println!($$$)' src      | wc -l   #  7  filtered again

Observed

The explicit-run form compresses:

test/a.rs:1:fn f01() { println!("test 1"); }
...
  … 25 more match line(s) in test/a.rs
src/a.rs:1:fn f01() { println!("src 1"); }
...
  … 25 more match line(s) in src/a.rs
[full output: ~/.cache/rtk/tee/…_ast-grep.log]

The implicit form, same search, emits all 60 lines. Dropping the test argument restores filtering (7 lines), which isolates the directory name as the trigger.

Expected

An invocation that is unambiguously run — the first positional is not a subcommand name — stays filtered regardless of what the remaining positionals are called.

Suggested direction

ast-grep's dispatch only ever reads the first positional as a subcommand; everything after it is a pattern or a path. Once the flag grammar is known well enough to locate the first positional reliably, the check can look at positionals.first() alone instead of .any(), which keeps the lsp/scan correctness guarantee and stops a directory name from costing the whole filter.

Source: src/cmds/system/ast_grep_cmd.rs — filters_this_invocation, OTHER_SUBCOMMANDS.

Scope note

The current OTHER_SUBCOMMANDS is ["scan", "test", "new", "lsp", "completions", "docs"]. Against ast-grep 0.45.3 the real list is run, scan, test, new, lsp, outline, completions, help — so docs is not a subcommand, and outline and help are missing. #4150 re-derives the table from the binary and corrects that. This issue is only about the .any() scope, which #4150 leaves as is.

Activity

  1. added
    bug
    filter-quality
    on Sep 20, 2026
  2. rtk-wshm-sync-bot commented on Sep 20, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟡 Priority medium
    🎯 Confidence 93%

    Summary

    In implicit run form, any path positional matching a subcommand name (e.g. test/, scan/) disables ast-grep output filtering entirely; fix by checking only positionals.first() once flag grammar reliably locates it.

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

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

    Suggested Actions

    1. Change filters_this_invocation to check positionals.first() instead of .any() over all positionals
    2. Add regression test using test/, scan/, new/ as path args to implicit-run invocations
    3. Coordinate with fix(ast-grep): route subcommands by the grammar ast-grep actually has #4150, which re-derives OTHER_SUBCOMMANDS but leaves the .any() scope untouched
    4. Verify lsp/scan first-positional correctness guarantee still holds after the change

    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:clibugfilter-qualitygood first issuepriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions