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
54 changes: 53 additions & 1 deletion rust/crates/zg-engine/src/lexical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ fn build_walker(
checked_paths
.existing
.iter()
.map(|path| resolve_path(root, path))
.map(|path| resolve_match_path(root, path))
.collect()
};
let mut walker = WalkBuilder::from_iter(paths);
Expand Down Expand Up @@ -832,6 +832,19 @@ fn resolve_path(root: &Path, path: &Path) -> PathBuf {
}
}

/// Resolves a search path whose descendants are matched against globs and ignore files.
///
/// The `ignore` crate strips the workspace root from candidates as a byte
/// prefix, so `<root>/./src/a.rs` would be matched as `./src/a.rs` and miss
/// `src/**`. Absolute paths are kept as given, as ripgrep keeps them.
fn resolve_match_path(root: &Path, path: &Path) -> PathBuf {
if path.is_absolute() {
path.to_path_buf()
} else {
root.join(path).components().collect()
}
}

fn is_hard_ignored_path(path: &Path) -> bool {
path.components().any(|component| {
let Component::Normal(name) = component else {
Expand Down Expand Up @@ -1548,6 +1561,45 @@ mod tests {
let reply = search(&LexicalSearchService::new(), root.path(), &unfiltered).await;
assert_eq!(reply.matches.len(), 3);
}

#[tokio::test]
async fn path_rules_match_search_paths_with_a_current_directory_prefix() {
let root = TempDir::new().expect("temp dir");
fs::create_dir(root.path().join("src")).expect("src dir");
fs::create_dir(root.path().join("docs")).expect("docs dir");
fs::write(root.path().join("src/keep.rs"), "needle\n").expect("src fixture");
fs::write(root.path().join("docs/drop.rs"), "needle\n").expect("docs fixture");
fs::write(root.path().join("custom.ignore"), "docs/*.rs\n").expect("ignore fixture");

let mut failures = Vec::new();
for (paths, rule) in [
(&[][..], "src/**"),
(&["."][..], "src/**"),
(&["./"][..], "src/**"),
(&["./src"][..], "src/*.rs"),
(&["."][..], "!docs/**"),
(&["./docs", "./src"][..], "!docs/**"),
(&[][..], "--ignore-file custom.ignore"),
(&["."][..], "--ignore-file custom.ignore"),
] {
let mut filtered = request("needle");
filtered.paths = paths.iter().map(PathBuf::from).collect();
match rule.strip_prefix("--ignore-file ") {
Some(file) => filtered.options.ignore_files = vec![PathBuf::from(file)],
None => filtered.options.globs = vec![rule.to_owned()],
}
let reply = search(&LexicalSearchService::new(), root.path(), &filtered).await;
let found = reply
.matches
.iter()
.map(|item| item.relative_path.clone())
.collect::<Vec<_>>();
if found != [Path::new("src/keep.rs")] {
failures.push(format!("paths {paths:?} {rule}: {found:?}"));
}
}
assert!(failures.is_empty(), "{failures:#?}");
}
}

#[cfg(test)]
Expand Down