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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Parallel file content search powered by the ripgrep engine.
//!
//! Workload: I/O-bound (file reading + regex matching via ripgrep engine).
//! Parallelism: content search uses `ignore::WalkParallel` + bounded
//! `crossbeam-channel` (`concurrency::EVENT_CHANNEL_CAP`). `--target files`
//! collects via `collect_files_parallel` (honors `--threads`), sorts, then
//! emits stable NDJSON. Bound via `--threads`/`--max-concurrency`.
use std::io::Write;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
use anyhow::Result;
use grep_matcher::Matcher;
use grep_regex::RegexMatcherBuilder;
use grep_searcher::{SearcherBuilder, Sink, SinkContext, SinkMatch};
use crate::cli::{GlobalArgs, SearchArgs};
use crate::cli_args::SortBy;
use crate::ndjson_types::{
FileStats, SearchBegin, SearchContext, SearchCount, SearchEnd, SearchFile, SearchMatch,
Submatch, Summary,
};
use crate::output::NdjsonWriter;
use crate::signal::ShutdownSignal;
/// Search file contents in parallel using the ripgrep engine.
///
/// # Errors
///
/// Returns `AtomwriteError::WorkspaceJail` if the path escapes the workspace.
/// Returns `AtomwriteError::Io` if reading files fails.
#[tracing::instrument(skip_all, fields(command = "search"))]
pub fn cmd_search(
args: &SearchArgs,
global: &GlobalArgs,
writer: &mut NdjsonWriter<impl Write>,
shutdown: &ShutdownSignal,
) -> Result<()> {
let start = Instant::now();
if args.pcre2 {
return Err(crate::error::AtomwriteError::InvalidInput {
reason: "PCRE2 not available; rebuild with --features pcre2".into(),
}
.into());
}
let workspace = global.resolve_workspace()?;
let canonical_paths =
crate::commands::path_resolution::resolve_paths_against_workspace(&args.paths, &workspace)?;
use crate::cli_args::SearchTarget;
if matches!(args.target, SearchTarget::Files | SearchTarget::Both) {
let pat = if args.case_insensitive || args.smart_case {
args.pattern.to_ascii_lowercase()
} else {
args.pattern.clone()
};
// WalkParallel collect + parallel name filter; sort for stable NDJSON.
let walker = build_walker(args, &canonical_paths, global)?;
let files = crate::concurrency::collect_files_parallel(&walker);
let casefold = args.case_insensitive || args.smart_case;
let mut matches: Vec<(String, String)> =
if crate::concurrency::should_parallelize(files.len()) {
use rayon::prelude::*;
files
.par_iter()
.filter_map(|path| {
if shutdown.is_shutdown() {
return None;
}
let name = path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
let hay = if casefold {
name.to_ascii_lowercase()
} else {
name.clone()
};
if hay.contains(&pat) {
Some((path.display().to_string(), name))
} else {
None
}
})
.collect()
} else {
files
.iter()
.filter_map(|path| {
let name = path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
let hay = if casefold {
name.to_ascii_lowercase()
} else {
name.clone()
};
if hay.contains(&pat) {
Some((path.display().to_string(), name))
} else {
None
}
})
.collect()
};
crate::concurrency::sort_parallel(&mut matches);
let total_name_hits = matches.len() as u64;
let start_idx = args.offset.min(total_name_hits) as usize;
let end_idx = match args.limit {
Some(lim) => start_idx.saturating_add(lim as usize).min(matches.len()),
None => matches.len(),
};
for (path, name) in &matches[start_idx..end_idx] {
writer.write_event(&crate::ndjson_types::FileMatchEvent {
r#type: "file_match",
path: path.clone(),
name: name.clone(),
target: "files",
})?;
}
let matched_emitted = (end_idx - start_idx) as u64;
if matches!(args.target, SearchTarget::Files) {
writer.write_event(&Summary {
r#type: "summary",
// files_visited = all name hits (including those skipped by offset).
files_visited: total_name_hits,
files_matched: matched_emitted,
files_modified: None,
files_skipped: None,
total_matches: Some(matched_emitted),
total_replacements: None,
elapsed_ms: start.elapsed().as_millis() as u64,
})?;
return Ok(());
}
let _ = matched_emitted;
}
let matcher = build_matcher(args)?;
let walker = build_walker(args, &canonical_paths, global)?;
let (tx, rx) =
crossbeam_channel::bounded::<SearchEvent>(crate::concurrency::event_channel_cap());
// Per-run counters (not process-wide statics). Ordering::Relaxed is enough:
// each atomic is an independent tally; final loads happen after the
// parallel walker thread joins, so no cross-field data publication is required.
let files_visited = Arc::new(AtomicU64::new(0));
let files_matched = Arc::new(AtomicU64::new(0));
let total_matches = Arc::new(AtomicU64::new(0));
let fv = Arc::clone(&files_visited);
let fm = Arc::clone(&files_matched);
let tm = Arc::clone(&total_matches);
let context_lines = args.context;
let invert = args.invert;
let max_count = args.max_count;
let include_fifo = args.include_fifo;
let max_filesize = args.max_filesize;
let max_columns = args.max_columns;
let no_begin_end = args.no_begin_end;
let multiline = args.multiline;
let search_binary = args.binary;
let shutdown_flag = shutdown.flag();
let walker_thread = std::thread::spawn(move || {
walker.build_parallel().run(|| {
let matcher = matcher.clone();
let tx = tx.clone();
let fv = Arc::clone(&fv);
let fm = Arc::clone(&fm);
let tm = Arc::clone(&tm);
let shutdown_flag = Arc::clone(&shutdown_flag);
let mut searcher = SearcherBuilder::new()
.line_number(true)
.multi_line(multiline)
.invert_match(invert)
.before_context(context_lines)
.after_context(context_lines)
.max_matches(max_count)
.build();
Box::new(move |entry| {
if shutdown_flag.load(Ordering::Acquire) {
return ignore::WalkState::Quit;
}
let entry = match entry {
Ok(e) => e,
Err(_) => return ignore::WalkState::Continue,
};
if !entry.file_type().is_some_and(|ft| ft.is_file()) {
return ignore::WalkState::Continue;
}
// G56: skip FIFO/named pipe files unless --include-fifo is set.
// `open()` on a FIFO blocks indefinitely until the other end
// connects, which can cause atomwrite to hang in environments
// that have FIFOs in /tmp or /var (Docker, restricted, system dirs).
if !include_fifo {
if let Some(ft) = entry.file_type() {
#[cfg(unix)]
{
use std::os::unix::fs::FileTypeExt;
if ft.is_fifo() {
tracing::debug!(
path = %entry.path().display(),
"skipping FIFO (G56); pass --include-fifo to opt in"
);
return ignore::WalkState::Continue;
}
}
#[cfg(not(unix))]
let _ = ft; // no-op on Windows
}
}
// G68: skip files larger than --max-filesize.
if let Ok(meta) = entry.metadata() {
if meta.len() > max_filesize {
tracing::debug!(
path = %entry.path().display(),
size = meta.len(),
max = max_filesize,
"skipping oversized file (G68)"
);
return ignore::WalkState::Continue;
}
}
// A-008: skip binary files unless --binary (align with replace/ripgrep).
// Only probe the first BINARY_DETECT_SIZE bytes (do not load whole file).
if !search_binary {
use std::io::Read;
if let Ok(mut f) = std::fs::File::open(entry.path()) {
let mut head =
vec![0u8; crate::constants::BINARY_DETECT_SIZE];
if let Ok(n) = f.read(&mut head) {
head.truncate(n);
if crate::binary_detect::is_binary(&head) {
tracing::debug!(
path = %entry.path().display(),
"skipping binary file (A-008); pass --binary to opt in"
);
return ignore::WalkState::Continue;
}
}
}
}
fv.fetch_add(1, Ordering::Relaxed);
let path: Arc<std::path::Path> = Arc::from(entry.path());
let _span = tracing::debug_span!("process_file", path = %path.display()).entered();
let mut file_matches = 0u64;
let mut file_lines = 0u64;
// GAP-2026-010: --no-begin-end suppresses begin/end events for
// files with zero matches. Default (off) preserves the
// pre-v0.1.20 behaviour of emitting begin/end for every file.
// CRITICAL: Begin must be sent BEFORE the sink so that
// Match events emitted by SearchSink have a current open
// path to attach to in the consumer's BTreeMap.
if !no_begin_end {
let _ = tx.send(SearchEvent::Begin(Arc::clone(&path)));
}
let mut sink = SearchSink {
matcher: &matcher,
path: Arc::clone(&path),
tx: &tx,
file_matches: &mut file_matches,
file_lines: &mut file_lines,
max_columns,
};
let sink_result = searcher.search_path(&matcher, &*path, &mut sink);
if let Err(e) = sink_result {
tracing::warn!(path = %path.display(), error = %e, "search error");
}
if file_matches > 0 {
fm.fetch_add(1, Ordering::Relaxed);
tm.fetch_add(file_matches, Ordering::Relaxed);
}
if !(no_begin_end && file_matches == 0) {
let _ = tx.send(SearchEvent::End {
path,
matches: file_matches,
lines_searched: file_lines,
});
}
ignore::WalkState::Continue
})
});
});
let mut has_matches = false;
let sort_active = args.sort.is_some();
// Buffer events per path so that parallel walker threads do not interleave
// Begin/Match/End events for different files in the NDJSON output.
let mut buffer: std::collections::BTreeMap<std::path::PathBuf, Vec<SearchEvent>> =
std::collections::BTreeMap::new();
let mut open_paths: std::collections::BTreeSet<std::path::PathBuf> =
std::collections::BTreeSet::new();
for event in rx {
if shutdown.is_shutdown() {
break;
}
// Track open paths to know when we can flush a complete sequence
match &event {
SearchEvent::Begin(p) => {
open_paths.insert(p.to_path_buf());
buffer.entry(p.to_path_buf()).or_default().push(event);
}
SearchEvent::End { path, .. } => {
let path_buf = path.to_path_buf();
buffer.entry(path_buf.clone()).or_default().push(event);
if !sort_active {
if let Some(events) = buffer.remove(&path_buf) {
open_paths.remove(&path_buf);
for ev in events {
emit_search_event(ev, writer, args, &mut has_matches)?;
}
}
}
}
_ => {
// For Match/Context, attach to the most recent open path
if let Some(last) = open_paths.iter().next_back() {
buffer.entry(last.clone()).or_default().push(event);
}
}
}
}
// Flush remaining buffered events. When --sort is active, ALL events are
// here and BTreeMap emits them in sorted path order.
for (_, events) in buffer {
for ev in events {
emit_search_event(ev, writer, args, &mut has_matches)?;
}
}
if let Err(panic_payload) = walker_thread.join() {
std::panic::resume_unwind(panic_payload);
}
// If the search was interrupted by SIGINT/SIGTERM, the inner `for event`
// loop broke out before draining every Begin/End pair, so the buffered
// Begin events for in-flight files were never flushed and `has_matches`
// may be `false` even though the parallel walker did find matches
// (they are still sitting in the `buffer` and may also have been
// discarded by the break). Returning `Err(NoMatches)` here would be a
// lie — the search was killed, not empty — and would also cause the
// main entry point to take the `Err` branch and emit an error JSON
// envelope instead of the graceful "shutting down" banner that
// `atomwrite::signal::write_shutdown_message` writes from the main
// thread. Returning `Ok(())` lets the caller detect the shutdown via
// `shutdown.is_shutdown()` and emit the banner as designed.
if shutdown.is_shutdown() {
return Ok(());
}
let summary = Summary {
r#type: "summary",
files_visited: files_visited.load(Ordering::Relaxed),
files_matched: files_matched.load(Ordering::Relaxed),
files_modified: None,
files_skipped: None,
total_matches: Some(total_matches.load(Ordering::Relaxed)),
total_replacements: None,
elapsed_ms: start.elapsed().as_millis() as u64,
};
writer.write_event(&summary)?;
if !has_matches {
return Err(crate::error::AtomwriteError::NoMatches.into());
}
Ok(())
}
enum SearchEvent {
Begin(Arc<std::path::Path>),
Match {
path: Arc<std::path::Path>,
line_number: u64,
lines: String,
byte_offset: u64,
submatches: Vec<Submatch>,
},
Context {
path: Arc<std::path::Path>,
line_number: u64,
lines: String,
},
End {
path: Arc<std::path::Path>,
matches: u64,
lines_searched: u64,
},
}
include!("search_helpers.inc.rs");