Skip to content
Open
Show file tree
Hide file tree
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
41 changes: 39 additions & 2 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,18 @@ impl WorkerState {
fn build_overrides(&self, paths: &[PathBuf]) -> Result<Override> {
let first_path = &paths[0];
let config = &self.config;
let multi_root = paths.len() > 1;

let mut builder = OverrideBuilder::new(first_path);

for pattern in &config.exclude_patterns {
let pattern = if multi_root {
Cow::Owned(normalize_exclude_pattern_for_multi_root(pattern))
} else {
Cow::Borrowed(pattern.as_str())
};
builder
.add(pattern)
.add(pattern.as_ref())
.map_err(|e| anyhow!("Malformed exclude pattern: {}", e))?;
}

Expand Down Expand Up @@ -687,6 +693,21 @@ fn search_str_for_entry<'a>(
}
}

/// When multiple `--search-path` roots are used, exclude globs are matched relative to the
/// first root in the `ignore` crate. Prefix with `**/` so the pattern applies under each root.
fn normalize_exclude_pattern_for_multi_root(pattern: &str) -> String {
let (prefix, glob) = match pattern.strip_prefix('!') {
Some(rest) => ("!", rest),
None => ("", pattern),
};

if glob.starts_with("**/") || glob.starts_with('/') {
return pattern.to_string();
}

format!("{prefix}**/{glob}")
}

/// Recursively scan the given search path for files / pathnames matching the patterns.
///
/// If the `--exec` argument was supplied, this will create a thread pool for executing
Expand All @@ -698,9 +719,25 @@ pub fn scan(paths: &[PathBuf], patterns: Vec<Regex>, config: Config) -> Result<E

#[cfg(test)]
mod tests {
use super::search_str_for_entry;
use super::{normalize_exclude_pattern_for_multi_root, search_str_for_entry};
use std::path::{Path, PathBuf};

#[test]
fn normalize_exclude_pattern_for_multi_root_prefixes_relative_globs() {
assert_eq!(
normalize_exclude_pattern_for_multi_root("!ghi/def"),
"!**/ghi/def"
);
assert_eq!(
normalize_exclude_pattern_for_multi_root("!**/ghi/def"),
"!**/ghi/def"
);
assert_eq!(
normalize_exclude_pattern_for_multi_root("!/abs/path"),
"!/abs/path"
);
}

#[test]
fn search_str_for_entry_with_relative_path() {
let full_path_base = Some(Path::new("/home/user"));
Expand Down
29 changes: 29 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,35 @@ fn test_excludes() {
);
}

/// Regression test for #1919
#[test]
fn test_exclude_with_multiple_search_paths() {
let te = TestEnv::new(
&["sp1/abc/def", "sp1/ghi/def", "sp2/abc/def", "sp2/ghi/def"],
&[
"sp1/abc/def/match.txt",
"sp1/ghi/def/excluded.txt",
"sp2/abc/def/match.txt",
"sp2/ghi/def/excluded.txt",
],
);

te.assert_output(
&[
"--type",
"file",
"--search-path",
"sp1",
"--search-path",
"sp2",
"--exclude",
"ghi/def",
],
"sp1/abc/def/match.txt
sp2/abc/def/match.txt",
);
}

#[test]
fn format() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
Expand Down
Loading