Skip to content

fix(ast-grep): preserve explicitly requested context - #4105

Open
jacobshaw01-del wants to merge 1 commit into
rtk-ai:developfrom
jacobshaw01-del:fix/ast-grep-preserve-context
Open

jacobshaw01-del wants to merge 1 commit into
rtk-ai:developfrom
jacobshaw01-del:fix/ast-grep-preserve-context

Conversation

@jacobshaw01-del

Copy link
Copy Markdown

Summary

ast-grep run -C 2 can emit one contiguous block where context and match lines have the same path:line:content shape. RTK counted every line against its five-line cap, truncating even the matched expression. The captured reproduction has 10 lines; before this fix, RTK retained only the first five plus an overflow hint.

Preserve stdout when the caller explicitly requests context with -A/--after, -B/--before, or -C/--context. Use the shared argument tokenizer to recognize separate and attached values while excluding pattern/glob values and paths after --. Ordinary searches retain their existing filtering. Update the command summary in the README.

Fixes #4015.

Test plan

  • Captured a real ast-grep 0.45.3 result and added a CLI regression that failed before the fix and passes afterward.
  • Added unit coverage for context flag spellings, literal flag-like values, and the -- boundary; integration coverage verifies exact output preservation and that ordinary searches still filter.
  • Manually compared the built RTK binary against ast-grep 0.45.3 for seven context flag forms: stdout is byte-identical in every case.
  • cargo fmt --all --check, cargo clippy --locked --all-targets, and cargo test --locked --all pass on macOS arm64 with Rust 1.98.1: 3,814 tests passed, 0 failed, 8 ignored.

Explicit context requests intentionally pass through unchanged, following Correctness vs Token Savings. This branch is based directly on develop and is independent of #4104.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added ast-grep bug Something isn't working rust labels Sep 18, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes ast-grep output filtering in RTK so that when a caller explicitly requests context lines via -A/-B/-C (or their long-form equivalents), the raw stdout is preserved instead of being truncated by the generic line cap. Adds a dedicated argument tokenizer-based detection of context flags that correctly distinguishes them from pattern/glob values and paths after --.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4015


Analyzed 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

Labels

ast-grep bug Something isn't working rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant