mirror of
https://github.com/instructkr/claw-code.git
synced 2026-11-27 18:37:54 +08:00
fix(runtime): prune heavy directories in grep_search (#3388)
grep_search walked and read every file under its root, including .git, node_modules and target. glob_search already prunes these directories (#2986), and docs/g002-security-verification-map.md says both search tools do. In a Rust checkout with a 2.5 GB target/ directory, one grep_search call took about 2.3 s instead of about 0.13 s, with the same results. Use the same pruning for grep_search, and never prune the search root itself, so a search the caller points into node_modules or target still works for both tools.
This commit is contained in:
1 parent
f3b0bd5058
commit
4fb176c65d
1 file changed
+60
-2
@@ -950,8 +950,13 @@ fn normalize_for_comparison(path: &Path) -> PathBuf {
|
||||
path.to_path_buf()
|
||||
}
|
||||
|
||||
/// Whether a directory below the search root is one of the heavy
|
||||
/// directories (VCS data, dependencies, build output) that glob and grep
|
||||
/// searches skip. The root itself is always searched, so a search the caller
|
||||
/// points into one of these directories still works.
|
||||
fn should_skip_glob_dir(entry: &DirEntry) -> bool {
|
||||
entry.file_type().is_dir()
|
||||
entry.depth() > 0
|
||||
&& entry.file_type().is_dir()
|
||||
&& entry
|
||||
.file_name()
|
||||
.to_str()
|
||||
@@ -989,7 +994,10 @@ fn collect_search_files(base_path: &Path) -> io::Result<Vec<PathBuf>> {
|
||||
}
|
||||
|
||||
let mut files = Vec::new();
|
||||
for entry in WalkDir::new(base_path) {
|
||||
for entry in WalkDir::new(base_path)
|
||||
.into_iter()
|
||||
.filter_entry(|entry| !should_skip_glob_dir(entry))
|
||||
{
|
||||
let entry = entry.map_err(|error| io::Error::other(error.to_string()))?;
|
||||
if entry.file_type().is_file() {
|
||||
files.push(entry.path().to_path_buf());
|
||||
@@ -1565,6 +1573,50 @@ mod tests {
|
||||
assert!(grep_output.content.unwrap_or_default().contains("hello"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grep_search_skips_common_heavy_directories() {
|
||||
let dir = temp_path("grep-ignored-dirs");
|
||||
for sub in ["src", ".git/refs", "node_modules/pkg", "target/debug/build"] {
|
||||
std::fs::create_dir_all(dir.join(sub)).unwrap();
|
||||
std::fs::write(dir.join(sub).join("notes.txt"), "needle").unwrap();
|
||||
}
|
||||
let grep = |path: PathBuf| {
|
||||
grep_search(&GrepSearchInput {
|
||||
pattern: String::from("needle"),
|
||||
path: Some(path.to_string_lossy().into_owned()),
|
||||
glob: None,
|
||||
output_mode: Some(String::from("files_with_matches")),
|
||||
before: None,
|
||||
after: None,
|
||||
context_short: None,
|
||||
context: None,
|
||||
line_numbers: None,
|
||||
case_insensitive: None,
|
||||
file_type: None,
|
||||
head_limit: None,
|
||||
offset: None,
|
||||
multiline: None,
|
||||
})
|
||||
.expect("grep should succeed")
|
||||
.filenames
|
||||
};
|
||||
|
||||
let filenames = grep(dir.clone());
|
||||
assert_eq!(
|
||||
filenames.len(),
|
||||
1,
|
||||
"ignored dirs should be pruned: {filenames:?}"
|
||||
);
|
||||
assert!(Path::new(&filenames[0]).ends_with("src/notes.txt"));
|
||||
|
||||
// A search rooted inside an ignored directory still searches it.
|
||||
let filenames = grep(dir.join("node_modules"));
|
||||
assert_eq!(filenames.len(), 1, "{filenames:?}");
|
||||
assert!(Path::new(&filenames[0]).ends_with("node_modules/pkg/notes.txt"));
|
||||
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expand_braces_no_braces() {
|
||||
assert_eq!(expand_braces("*.rs"), vec!["*.rs"]);
|
||||
@@ -1646,6 +1698,12 @@ mod tests {
|
||||
|| path.contains(".build")
|
||||
|| path.contains("/target/")));
|
||||
|
||||
// A glob rooted inside an ignored directory still searches it.
|
||||
let inside = dir.join("node_modules");
|
||||
let result = glob_search("**/AGENTS.md", Some(inside.to_str().unwrap()))
|
||||
.expect("glob should succeed");
|
||||
assert_eq!(result.num_files, 1, "{:?}", result.filenames);
|
||||
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user