Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions rust/crates/runtime/src/file_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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"]);
Expand Down Expand Up @@ -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);
}

Expand Down