M2.3: find in files backend

crates/search: parallel ignore walk + grep-searcher with the project's
ignore rules (vendor searched as library, node_modules and .git skipped),
literal or regex, case, whole word, include/exclude globs, binary and
> 8 MiB skips, match cap, cancel token, buffer overlay.

Engine: search.find returns {searchId} at once and runs off the lock;
pump_vfs drains results into searchResults (one per file, at most 500
per pump) and searchDone; search.cancel. Testkit: generated search tree.
Tests: library unit and property tests, engine tests, TCP control test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-16 22:03:17 +02:00
parent a6b2fd1300
commit 172693c8dd
21 changed files with 2231 additions and 5 deletions

View file

@ -0,0 +1,207 @@
//! `search.find` and `search.cancel`: results arrive through the pump.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::fs;
use std::time::{Duration, Instant};
use lutra_engine::cmd::search::MAX_RESULTS_PER_PUMP;
use lutra_engine::{Engine, Event};
use serde_json::{Value, json};
use tempfile::TempDir;
/// A temp folder with `files`, opened in a fresh engine.
fn open_project(files: &[(&str, &str)]) -> (TempDir, Engine) {
let dir = tempfile::tempdir().unwrap();
for (path, text) in files {
let abs = dir.path().join(path);
fs::create_dir_all(abs.parent().unwrap()).unwrap();
fs::write(abs, text).unwrap();
}
let mut engine = Engine::new().unwrap();
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
(dir, engine)
}
fn find(engine: &mut Engine, params: Value) -> u64 {
let out = engine.execute("search.find", params).unwrap();
assert!(out.events.is_empty(), "results only come from the pump");
out.value["searchId"].as_u64().unwrap()
}
/// Pumps until `searchDone` for `id`; returns the results (sorted by path)
/// and the done event, and the number of pumps.
fn collect(engine: &mut Engine, id: u64) -> (Vec<Event>, Event, usize) {
let deadline = Instant::now() + Duration::from_secs(10);
let mut results = Vec::new();
let mut pumps = 0;
while Instant::now() < deadline {
pumps += 1;
for event in engine.pump_vfs().unwrap() {
match &event {
Event::SearchResults { search_id, .. } if *search_id == id => results.push(event),
Event::SearchDone { search_id, .. } if *search_id == id => {
results.sort_by_key(path);
return (results, event, pumps);
}
_ => {}
}
}
std::thread::sleep(Duration::from_millis(10));
}
panic!("no searchDone within 10 s");
}
fn path(event: &Event) -> String {
match event {
Event::SearchResults { path, .. } => path.clone(),
_ => String::new(),
}
}
#[test]
fn finds_in_files_and_dirty_buffers() {
let (_dir, mut engine) = open_project(&[
("src/a.php", "<?php\necho 'needle';\n"),
("src/b.php", "<?php\n"),
("vendor/x/c.php", "<?php // needle\n"),
("node_modules/d.js", "needle\n"),
]);
let out = engine
.execute("file.open", json!({"path": "src/b.php"}))
.unwrap();
let buffer = out.value["id"].as_u64().unwrap();
engine
.execute(
"buffer.edit",
json!({"id": buffer, "baseVersion": 1, "edits": [{"from": 6, "to": 6, "insert": "$needle = 1;\n"}]}),
)
.unwrap();
let id = find(&mut engine, json!({"query": "NEEDLE"}));
let (results, done, _) = collect(&mut engine, id);
let paths: Vec<String> = results.iter().map(path).collect();
assert_eq!(paths, ["src/a.php", "src/b.php", "vendor/x/c.php"]);
let Event::SearchResults { matches, .. } = &results[1] else {
panic!()
};
assert_eq!(
(matches[0].line, matches[0].column, matches[0].length),
(1, 1, 6)
);
assert_eq!(matches[0].preview, "$needle = 1;");
let Event::SearchDone {
files,
matches,
truncated,
cancelled,
..
} = done
else {
panic!()
};
assert_eq!((files, matches, truncated, cancelled), (3, 3, false, false));
let id2 = find(
&mut engine,
json!({"query": "needle", "caseSensitive": true, "include": ["src/*.php"]}),
);
assert!(id2 > id);
let (results, _, _) = collect(&mut engine, id2);
assert_eq!(results.len(), 2);
}
#[test]
fn invalid_queries_fail_with_invalid_params() {
let (_dir, mut engine) = open_project(&[("a.php", "x")]);
for params in [
json!({"query": "(", "regex": true}),
json!({"query": ""}),
json!({"query": "x", "include": ["["]}),
json!({"query": "x", "bogus": 1}),
json!({}),
] {
let err = engine.execute("search.find", params.clone()).unwrap_err();
assert_eq!(err.code(), "invalid_params", "{params}");
}
assert_eq!(engine.state().searches.running(), 0);
engine.execute("project.close", json!({})).unwrap();
let err = engine
.execute("search.find", json!({"query": "x"}))
.unwrap_err();
assert_eq!(err.code(), "disabled");
}
#[test]
fn cancel_and_project_close_end_searches() {
let dir = tempfile::tempdir().unwrap();
lutra_testkit::generate_search_tree(dir.path(), 2_000);
let mut engine = Engine::new().unwrap();
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
let id = find(&mut engine, json!({"query": "function"}));
let out = engine
.execute("search.cancel", json!({"searchId": id}))
.unwrap();
assert_eq!(out.value["cancelled"], true);
let (_, done, _) = collect(&mut engine, id);
let Event::SearchDone {
cancelled, matches, ..
} = done
else {
panic!()
};
assert!(
cancelled || matches == 10_000,
"cancelled unless it had already finished"
);
let out = engine
.execute("search.cancel", json!({"searchId": id}))
.unwrap();
assert_eq!(out.value["cancelled"], false);
let id = find(&mut engine, json!({"query": "function"}));
engine.execute("project.close", json!({})).unwrap();
let (results, done, _) = collect(&mut engine, id);
assert!(results.is_empty());
assert!(matches!(
done,
Event::SearchDone {
cancelled: true,
..
}
));
assert_eq!(engine.state().searches.running(), 0);
}
#[test]
fn pumps_spread_many_results() {
let dir = tempfile::tempdir().unwrap();
for i in 0..(MAX_RESULTS_PER_PUMP + 100) {
fs::write(dir.path().join(format!("f{i}.txt")), "hit\n").unwrap();
}
let mut engine = Engine::new().unwrap();
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
let id = find(&mut engine, json!({"query": "hit"}));
std::thread::sleep(Duration::from_millis(500));
let first = engine.pump_vfs().unwrap();
let batch = first
.iter()
.filter(|e| matches!(e, Event::SearchResults { .. }))
.count();
assert!(batch <= MAX_RESULTS_PER_PUMP, "{batch}");
let (rest, done, _) = collect(&mut engine, id);
assert_eq!(batch + rest.len(), MAX_RESULTS_PER_PUMP + 100);
assert!(matches!(done, Event::SearchDone { files, .. } if files == MAX_RESULTS_PER_PUMP + 100));
}