1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//! CLI dispatch for `ast-bro run`.
use std::path::{Path, PathBuf};
use std::str::FromStr;
use ast_grep_language::SupportLang;
use super::{detect_lang, search_with_pattern};
pub fn run(
pattern: &str,
rewrite_template: Option<&str>,
lang_override: Option<&str>,
paths: &[PathBuf],
glob: Option<&str>,
write_changes: bool,
json: bool,
pretty: bool,
) -> i32 {
let search_paths = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths.to_vec()
};
let files = crate::walk_paths(&search_paths, glob);
let mut match_count: usize = 0;
let mut rewrite_count: usize = 0;
let mut error_count: usize = 0;
let mut attempted_files: usize = 0;
// Collect search matches when emitting JSON so the whole run produces
// one valid JSON array (consistent with `map` / `show` / etc.) rather
// than newline-delimited objects.
let mut json_matches: Vec<super::RunMatch> = Vec::new();
#[derive(serde::Serialize)]
struct RewriteRecord {
file: String,
status: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
diff: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
}
let mut json_rewrites: Vec<RewriteRecord> = Vec::new();
// Pre-resolve language and compile pattern once when --lang is specified,
// so we avoid redundant parsing on every file in the loop.
let (fixed_lang, compiled_pattern) = if let Some(l) = lang_override {
let lang = match parse_lang(l) {
Some(l) => l,
None => {
eprintln!("error: unsupported language '{}'", l);
return 2;
}
};
let pat = match ast_grep_core::Pattern::try_new(pattern, lang) {
Ok(p) => p,
Err(e) => {
eprintln!("error: invalid pattern: {}", e);
return 2;
}
};
(Some(lang), Some(pat))
} else {
(None, None)
};
// Cache compiled patterns per language when lang is auto-detected.
let mut pattern_cache: std::collections::HashMap<ast_grep_language::SupportLang, Result<ast_grep_core::Pattern, String>> = std::collections::HashMap::new();
for path in &files {
// Detect language first to avoid reading non-source files.
let lang = if let Some(l) = fixed_lang {
l
} else {
match detect_lang(path) {
Some(l) => l,
None => continue,
}
};
attempted_files += 1;
// Mirror the MCP cap so both run paths refuse to slurp pathological
// files (minified bundles, generated data) under source extensions.
if let Ok(meta) = std::fs::metadata(path) {
if meta.len() > super::RUN_MAX_FILE_BYTES {
eprintln!(
"{}: skipped (size {} > cap {})",
path.display(),
meta.len(),
super::RUN_MAX_FILE_BYTES
);
error_count += 1;
continue;
}
}
let source = match std::fs::read_to_string(path) {
Ok(s) => s,
Err(e) => {
eprintln!("{}: read failed: {}", path.display(), e);
error_count += 1;
continue;
},
};
if let Some(replacement) = rewrite_template {
let result = if let Some(ref compiled) = compiled_pattern {
super::rewrite_with_pattern(&source, lang, compiled, replacement)
} else {
let compiled = pattern_cache.entry(lang).or_insert_with(|| {
ast_grep_core::Pattern::try_new(pattern, lang)
.map_err(|e| format!("invalid pattern for {}: {}", lang, e))
});
match compiled {
Ok(p) => super::rewrite_with_pattern(&source, lang, p, replacement),
Err(e) => {
eprintln!("{}: {}", path.display(), e);
error_count += 1;
continue;
}
}
};
match result {
Ok(Some(new_source)) => {
let file_str = path.display().to_string();
if write_changes {
match super::atomic_write(path, new_source.as_bytes()) {
Err(e) => {
if json {
json_rewrites.push(RewriteRecord {
file: file_str,
status: "write_failed",
diff: None,
error: Some(e.to_string()),
});
} else {
eprintln!("{}: write failed: {}", path.display(), e);
}
error_count += 1;
}
Ok(()) => {
if json {
json_rewrites.push(RewriteRecord {
file: file_str,
status: "rewritten",
diff: None,
error: None,
});
} else {
println!("{}: rewritten", path.display());
}
rewrite_count += 1;
}
}
} else if json {
let diff = line_change_report(path, &source, &new_source);
json_rewrites.push(RewriteRecord {
file: file_str,
status: "diff",
diff: Some(diff),
error: None,
});
rewrite_count += 1;
} else {
show_diff(path, &source, &new_source);
rewrite_count += 1;
}
}
Ok(None) => {}
Err(e) => {
if json {
json_rewrites.push(RewriteRecord {
file: path.display().to_string(),
status: "rewrite_error",
diff: None,
error: Some(e.clone()),
});
} else {
eprintln!("{}: {}", path.display(), e);
}
error_count += 1;
},
}
} else {
let result = if let Some(ref compiled) = compiled_pattern {
search_with_pattern(&source, lang, compiled)
} else {
let compiled = pattern_cache.entry(lang).or_insert_with(|| {
ast_grep_core::Pattern::try_new(pattern, lang)
.map_err(|e| format!("invalid pattern for {}: {}", lang, e))
});
match compiled {
Ok(p) => search_with_pattern(&source, lang, p),
Err(e) => {
eprintln!("{}: {}", path.display(), e);
error_count += 1;
continue;
}
}
};
match result {
Ok(matches) => {
match_count += matches.len();
for mut m in matches {
m.file = path.display().to_string();
if json {
json_matches.push(m);
} else {
let first_line = m
.matched_text
.lines()
.next()
.unwrap_or("");
println!(
"{}:{}:{}-{}:{}: {}",
m.file, m.start_line, m.start_col, m.end_line, m.end_col, first_line
);
}
}
}
Err(e) => {
eprintln!("{}: {}", path.display(), e);
error_count += 1;
},
}
}
}
// Flush collected results as a single JSON document so machine
// consumers can parse one valid object per invocation.
if json {
let serialized = if rewrite_template.is_some() {
#[derive(serde::Serialize)]
struct RewriteDoc<'a> {
schema: &'static str,
mode: &'static str,
dry_run: bool,
rewrite_count: usize,
error_count: usize,
files: &'a [RewriteRecord],
}
let doc = RewriteDoc {
schema: crate::core::JSON_SCHEMA_RUN,
mode: "rewrite",
dry_run: !write_changes,
rewrite_count,
error_count,
files: &json_rewrites,
};
if pretty {
serde_json::to_string_pretty(&doc)
} else {
serde_json::to_string(&doc)
}
} else {
#[derive(serde::Serialize)]
struct SearchDoc<'a> {
schema: &'static str,
matches: &'a [super::RunMatch],
error_count: usize,
}
let doc = SearchDoc {
schema: crate::core::JSON_SCHEMA_RUN,
matches: &json_matches,
error_count,
};
if pretty {
serde_json::to_string_pretty(&doc)
} else {
serde_json::to_string(&doc)
}
};
match serialized {
Ok(s) => println!("{}", s),
Err(e) => {
eprintln!("error: failed to serialize JSON output: {}", e);
return 2;
}
}
}
// Distinguish "walked nothing" from "walked some, matched nothing" for
// the user — exit code stays at 1 either way (matches ripgrep/grep
// convention), but the hint surfaces a likely path/glob/--lang
// misconfiguration. Suppressed in JSON mode so machine consumers get a
// clean stream.
if attempted_files == 0 && !json {
eprintln!(
"ast-bro run: no source files processed (check paths, --glob, or --lang)"
);
}
// Exit code semantics:
// 0 = success (matches found, or rewrites applied)
// 1 = no matches found (search mode) or no rewrites possible (rewrite mode)
// 2 = all attempted files errored (and at least one file was attempted)
if attempted_files > 0 && error_count == attempted_files {
2
} else if rewrite_template.is_some() && rewrite_count == 0 {
1
} else if rewrite_template.is_none() && match_count == 0 {
1
} else {
0
}
}
pub fn parse_lang(s: &str) -> Option<SupportLang> {
match s.to_lowercase().as_str() {
"rs" | "rust" => Some(SupportLang::Rust),
"py" | "python" => Some(SupportLang::Python),
"ts" | "typescript" => Some(SupportLang::TypeScript),
"tsx" => Some(SupportLang::Tsx),
"js" | "javascript" => Some(SupportLang::JavaScript),
"cs" | "csharp" => Some(SupportLang::CSharp),
"go" => Some(SupportLang::Go),
"java" => Some(SupportLang::Java),
"kt" | "kotlin" => Some(SupportLang::Kotlin),
"scala" => Some(SupportLang::Scala),
"cpp" | "c++" => Some(SupportLang::Cpp),
"rb" | "ruby" => Some(SupportLang::Ruby),
"php" => Some(SupportLang::Php),
other => SupportLang::from_str(other).ok(),
}
}
/// Produce a path-prefixed, line-by-line change report between `old` and
/// `new`. Each changed line is emitted as `path:line: -old` / `path:line: +new`
/// (a custom format — *not* a standard `--- / +++ / @@` unified diff). Used
/// by both CLI (dry-run rewrite) and MCP `run` tool.
pub fn line_change_report(path: &Path, old: &str, new: &str) -> String {
use similar::TextDiff;
let mut out = String::new();
let diff = TextDiff::from_lines(old, new);
for op in diff.ops() {
for change in diff.iter_changes(op) {
if change.tag() == similar::ChangeTag::Equal {
continue;
}
let sign = match change.tag() {
similar::ChangeTag::Delete => "-",
similar::ChangeTag::Insert => "+",
similar::ChangeTag::Equal => unreachable!(),
};
let display_idx = change
.old_index()
.or_else(|| change.new_index())
.unwrap_or(0)
+ 1;
let line_content = change.to_string();
out.push_str(&format!(
"{}:{}: {}{}",
path.display(),
display_idx,
sign,
line_content
));
if !line_content.ends_with('\n') {
out.push('\n');
}
}
}
out
}
/// Print diff to stdout (CLI dry-run mode).
fn show_diff(path: &Path, old: &str, new: &str) {
print!("{}", line_change_report(path, old, new));
}