diff --git a/crates/ruff/src/linter.rs b/crates/ruff/src/linter.rs index c657017a2d..f7972f92d7 100644 --- a/crates/ruff/src/linter.rs +++ b/crates/ruff/src/linter.rs @@ -220,8 +220,8 @@ pub fn check_path( const MAX_ITERATIONS: usize = 100; -/// Add any missing `#noqa` pragmas to the source code at the given `Path`. -pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result { +/// Add any missing `# noqa` pragmas to the source code at the given `Path`. +pub fn add_noqa_to_path(path: &Path, package: Option<&Path>, settings: &Settings) -> Result { // Read the file from disk. let contents = fs::read_file(path)?; @@ -247,7 +247,7 @@ pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result { error, } = check_path( path, - None, + package, &contents, tokens, &locator, diff --git a/crates/ruff_cli/src/commands/add_noqa.rs b/crates/ruff_cli/src/commands/add_noqa.rs index abe916a8de..b3d2b0af6f 100644 --- a/crates/ruff_cli/src/commands/add_noqa.rs +++ b/crates/ruff_cli/src/commands/add_noqa.rs @@ -8,7 +8,7 @@ use rayon::prelude::*; use ruff::linter::add_noqa_to_path; use ruff::resolver::PyprojectDiscovery; -use ruff::{resolver, warn_user_once}; +use ruff::{packaging, resolver, warn_user_once}; use crate::args::Overrides; use crate::iterators::par_iter; @@ -30,13 +30,28 @@ pub fn add_noqa( return Ok(0); } + // Discover the package root for each Python file. + let package_roots = packaging::detect_package_roots( + &paths + .iter() + .flatten() + .map(ignore::DirEntry::path) + .collect::>(), + &resolver, + pyproject_strategy, + ); + let start = Instant::now(); let modifications: usize = par_iter(&paths) .flatten() .filter_map(|entry| { let path = entry.path(); + let package = path + .parent() + .and_then(|parent| package_roots.get(parent)) + .and_then(|package| *package); let settings = resolver.resolve(path, pyproject_strategy); - match add_noqa_to_path(path, settings) { + match add_noqa_to_path(path, package, settings) { Ok(count) => Some(count), Err(e) => { error!("Failed to add noqa to {}: {e}", path.to_string_lossy());