Skip to content

Commit

Permalink
Respect excludes in ruff server configuration discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 27, 2024
1 parent b5d147d commit 2425af0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ruff_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ruff_workspace = { workspace = true }

anyhow = { workspace = true }
crossbeam = { workspace = true }
globset = { workspace = true }
jod-thread = { workspace = true }
lsp-server = { workspace = true }
lsp-types = { workspace = true }
Expand Down
69 changes: 55 additions & 14 deletions crates/ruff_server/src/session/index/ruff_settings.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use globset::Candidate;
use ruff_linter::{
display_settings, fs::normalize_path_to, settings::types::FilePattern,
settings::types::PreviewMode,
};
use ruff_workspace::resolver::match_candidate_exclusion;
use ruff_workspace::{
configuration::{Configuration, FormatConfiguration, LintConfiguration, RuleSelection},
pyproject::{find_user_settings_toml, settings_toml},
resolver::{ConfigurationTransformer, Relativity},
};
use std::sync::RwLock;
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
Expand All @@ -16,8 +19,9 @@ use walkdir::{DirEntry, WalkDir};

use crate::session::settings::{ConfigurationPreference, ResolvedEditorSettings};

#[derive(Default)]
pub(crate) struct RuffSettings {
/// Settings used to manage file inclusion and exclusion.
file_resolver: ruff_workspace::FileResolverSettings,
/// Settings to pass into the Ruff linter.
linter: ruff_linter::settings::LinterSettings,
/// Settings to pass into the Ruff formatter.
Expand Down Expand Up @@ -54,7 +58,7 @@ impl RuffSettings {
.ok()
})
.unwrap_or_else(|| {
let default_configuration = ruff_workspace::configuration::Configuration::default();
let default_configuration = Configuration::default();
EditorConfigurationTransformer(editor_settings, root)
.transform(default_configuration)
.into_settings(root)
Expand All @@ -64,6 +68,7 @@ impl RuffSettings {
});

RuffSettings {
file_resolver: fallback.file_resolver,
formatter: fallback.formatter,
linter: fallback.linter,
}
Expand All @@ -80,25 +85,22 @@ impl RuffSettings {

impl RuffSettingsIndex {
pub(super) fn new(root: &Path, editor_settings: &ResolvedEditorSettings) -> Self {
let mut index = BTreeMap::default();
let index = RwLock::new(BTreeMap::new());

// Add any settings from above the workspace root.
for directory in root.ancestors() {
if let Some(pyproject) = settings_toml(directory).ok().flatten() {
if index.contains_key(&pyproject) {
continue;
}

let Ok(settings) = ruff_workspace::resolver::resolve_root_settings(
&pyproject,
Relativity::Parent,
&EditorConfigurationTransformer(editor_settings, root),
) else {
continue;
};
index.insert(
index.write().unwrap().insert(
directory.to_path_buf(),
Arc::new(RuffSettings {
file_resolver: settings.file_resolver,
linter: settings.linter,
formatter: settings.formatter,
}),
Expand All @@ -110,32 +112,71 @@ impl RuffSettingsIndex {
// Add any settings within the workspace itself.
for directory in WalkDir::new(root)
.into_iter()
.filter_entry(|entry: &DirEntry| {
if !entry.file_type().is_dir() {
return true;
}

let directory = entry.path();

// If the directory is excluded from the workspace, skip it.
if let Some((_, settings)) = index
.read()
.unwrap()
.range(..directory.to_path_buf())
.rev()
.find(|(path, _)| directory.starts_with(path))
{
if let Some(file_name) = directory.file_name() {
let candidate = Candidate::new(&directory);
let basename = Candidate::new(file_name);
if match_candidate_exclusion(
&candidate,
&basename,
&settings.file_resolver.exclude,
) {
tracing::debug!("Ignored path via `exclude`: {}", directory.display());
return false;
} else if match_candidate_exclusion(
&candidate,
&basename,
&settings.file_resolver.extend_exclude,
) {
tracing::debug!(
"Ignored path via `extend-exclude`: {}",
directory.display()
);
return false;
}
}
}

true
})
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_dir())
.map(DirEntry::into_path)
{
if let Some(pyproject) = settings_toml(&directory).ok().flatten() {
if index.contains_key(&pyproject) {
continue;
}

let Ok(settings) = ruff_workspace::resolver::resolve_root_settings(
&pyproject,
Relativity::Parent,
&EditorConfigurationTransformer(editor_settings, root),
) else {
continue;
};
index.insert(
index.write().unwrap().insert(
directory,
Arc::new(RuffSettings {
file_resolver: settings.file_resolver,
linter: settings.linter,
formatter: settings.formatter,
}),
);
}
}

let index = index.into_inner().unwrap();

let fallback = Arc::new(RuffSettings::fallback(editor_settings, root));

Self { index, fallback }
Expand Down

0 comments on commit 2425af0

Please sign in to comment.