From 869a9543e46dcd4add00ae87dc38e8592fb41911 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 10 Feb 2025 10:27:14 +0530 Subject: [PATCH] Root exclusions in the server to project root (#16043) ## Summary fixes: #16041 ## Test Plan Using the [project](https://github.com/bwcc-clan/polebot) in the linked issue: Notice how the project "polebot" is in the "play" directory which is included in the `exclude` setting as: ```toml exclude = ["play"] ``` **Before this fix** ``` DEBUG ruff:worker:0 ruff_server::resolve: Ignored path via `exclude`: /private/tmp/ruff-test/play/polebot/src/utils/log_tools.py ``` **After this fix** ``` DEBUG ruff:worker:2 ruff_server::resolve: Included path via `include`: /private/tmp/ruff-test/play/polebot/src/utils/log_tools.py ``` I also updated the same project to remove the "play" directory from the `exclude` setting and made sure that anything under the `polebot/play` directory is included: ``` DEBUG ruff:worker:4 ruff_server::resolve: Included path via `include`: /private/tmp/ruff-test/play/polebot/play/test.py ``` And, excluded when I add the directory back: ``` DEBUG ruff:worker:2 ruff_server::resolve: Ignored path via `exclude`: /private/tmp/ruff-test/play/polebot/play/test.py ``` --- crates/ruff_server/src/resolve.rs | 9 ++------- crates/ruff_workspace/src/resolver.rs | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/crates/ruff_server/src/resolve.rs b/crates/ruff_server/src/resolve.rs index 27e33c4298..905512342f 100644 --- a/crates/ruff_server/src/resolve.rs +++ b/crates/ruff_server/src/resolve.rs @@ -59,8 +59,7 @@ fn is_document_excluded( ) -> bool { if let Some(exclusion) = match_any_exclusion( path, - &resolver_settings.exclude, - &resolver_settings.extend_exclude, + resolver_settings, linter_settings.map(|s| &*s.exclude), formatter_settings.map(|s| &*s.exclude), ) { @@ -68,11 +67,7 @@ fn is_document_excluded( return true; } - if let Some(inclusion) = match_any_inclusion( - path, - &resolver_settings.include, - &resolver_settings.extend_include, - ) { + if let Some(inclusion) = match_any_inclusion(path, resolver_settings) { tracing::debug!("Included path via `{}`: {}", inclusion, path.display()); false } else if let Some(LanguageId::Python) = language_id { diff --git a/crates/ruff_workspace/src/resolver.rs b/crates/ruff_workspace/src/resolver.rs index 080d96efb7..65a354c49d 100644 --- a/crates/ruff_workspace/src/resolver.rs +++ b/crates/ruff_workspace/src/resolver.rs @@ -23,9 +23,9 @@ use ruff_linter::package::PackageRoot; use ruff_linter::packaging::is_package; use crate::configuration::Configuration; -use crate::pyproject; use crate::pyproject::settings_toml; use crate::settings::Settings; +use crate::{pyproject, FileResolverSettings}; /// The configuration information from a `pyproject.toml` file. #[derive(Debug)] @@ -778,8 +778,7 @@ impl std::fmt::Display for ExclusionKind { /// any of the exclusion criteria. pub fn match_any_exclusion( path: &Path, - exclude: &GlobSet, - extend_exclude: &GlobSet, + resolver_settings: &FileResolverSettings, lint_exclude: Option<&GlobSet>, format_exclude: Option<&GlobSet>, ) -> Option { @@ -787,10 +786,10 @@ pub fn match_any_exclusion( if let Some(basename) = path.file_name() { let path = Candidate::new(path); let basename = Candidate::new(basename); - if match_candidate_exclusion(&path, &basename, exclude) { + if match_candidate_exclusion(&path, &basename, &resolver_settings.exclude) { return Some(ExclusionKind::Exclude); } - if match_candidate_exclusion(&path, &basename, extend_exclude) { + if match_candidate_exclusion(&path, &basename, &resolver_settings.extend_exclude) { return Some(ExclusionKind::ExtendExclude); } if let Some(lint_exclude) = lint_exclude { @@ -804,6 +803,11 @@ pub fn match_any_exclusion( } } } + if path == resolver_settings.project_root { + // Bail out; we'd end up past the project root on the next iteration + // (excludes etc. are thus "rooted" to the project). + break; + } } None } @@ -829,12 +833,11 @@ impl std::fmt::Display for InclusionKind { /// criteria. pub fn match_any_inclusion( path: &Path, - include: &GlobSet, - extend_include: &GlobSet, + resolver_settings: &FileResolverSettings, ) -> Option { - if include.is_match(path) { + if resolver_settings.include.is_match(path) { Some(InclusionKind::Include) - } else if extend_include.is_match(path) { + } else if resolver_settings.extend_include.is_match(path) { Some(InclusionKind::ExtendInclude) } else { None