Skip to content

Commit

Permalink
Exclude directly-passed files nested in excluded subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 22, 2022
1 parent 6907df4 commit 451047c
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/resolver.rs
Expand Up @@ -190,7 +190,7 @@ pub fn python_files_in_path(
overrides: &Overrides,
) -> Result<(Vec<Result<DirEntry, ignore::Error>>, Resolver)> {
// Normalize every path (e.g., convert from relative to absolute).
let paths: Vec<PathBuf> = paths.iter().map(fs::normalize_path).collect();
let mut paths: Vec<PathBuf> = paths.iter().map(fs::normalize_path).collect();

// Search for `pyproject.toml` files in all parent directories.
let mut resolver = Resolver::default();
Expand All @@ -207,6 +207,40 @@ pub fn python_files_in_path(
}
}

// Omit any paths that are nested within excluded ancestors. This ensures that
// if we pass `subdir/file.py`, and `subdir` is excluded, then we don't
// "miss" that exclusion when walking from `subdir/file.py` below.
if file_strategy.force_exclude {
paths.retain(|path| {
for path in path.ancestors() {
let settings = resolver.resolve(path, pyproject_strategy);
match fs::extract_path_names(path) {
Ok((file_path, file_basename)) => {
if !settings.exclude.is_empty()
&& is_excluded(file_path, file_basename, &settings.exclude)
{
debug!("Ignored path via `exclude`: {:?}", path);
return false;
} else if !settings.extend_exclude.is_empty()
&& is_excluded(file_path, file_basename, &settings.extend_exclude)
{
debug!("Ignored path via `extend-exclude`: {:?}", path);
return false;
}
}
Err(err) => {
debug!("Ignored path due to error in parsing: {:?}: {}", path, err);
return false;
}
}
}
true
});
if paths.is_empty() {
return Ok((vec![], resolver));
}
}

// Create the `WalkBuilder`.
let mut builder = WalkBuilder::new(
paths
Expand Down

0 comments on commit 451047c

Please sign in to comment.