Skip to content

Commit

Permalink
Use fallback settings when indexing the project (#12362)
Browse files Browse the repository at this point in the history
## Summary

This PR updates the settings index building logic in the language server
to consider the fallback settings for applying ignore filters in
`WalkBuilder` and the exclusion via `exclude` / `extend-exclude`.

This flow matches the one in the `ruff` CLI where the root settings is
built by (1) finding the workspace setting in the ancestor directory (2)
finding the user configuration if that's missing and (3) fallback to
using the default configuration.

Previously, the index building logic was being executed before (2) and
(3). This PR reverses the logic so that the exclusion /
`respect_gitignore` is being considered from the default settings if
there's no workspace / user settings. This has the benefit that the
server no longer enters the `.git` directory or any other excluded
directory when a user opens a file in the home directory.

Related to #11366

## Test plan

Opened a test file from the home directory and confirmed with the debug
trace (removed in #12360) that the server excludes the `.git` directory
when indexing.
  • Loading branch information
dhruvmanila authored Jul 18, 2024
1 parent b2a49d8 commit ebe5b06
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions crates/ruff_server/src/session/index/ruff_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl RuffSettings {
impl RuffSettingsIndex {
pub(super) fn new(root: &Path, editor_settings: &ResolvedEditorSettings) -> Self {
let mut index = BTreeMap::default();
let mut respect_gitignore = true;
let mut respect_gitignore = None;

// Add any settings from above the workspace root.
for directory in root.ancestors() {
Expand All @@ -113,7 +113,8 @@ impl RuffSettingsIndex {
continue;
};

respect_gitignore = settings.file_resolver.respect_gitignore;
respect_gitignore = Some(settings.file_resolver.respect_gitignore);

index.insert(
directory.to_path_buf(),
Arc::new(RuffSettings {
Expand All @@ -127,9 +128,13 @@ impl RuffSettingsIndex {
}
}

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

// Add any settings within the workspace itself
let mut builder = WalkBuilder::new(root);
builder.standard_filters(respect_gitignore);
builder.standard_filters(
respect_gitignore.unwrap_or_else(|| fallback.file_resolver().respect_gitignore),
);
builder.hidden(false);
builder.threads(
std::thread::available_parallelism()
Expand Down Expand Up @@ -157,26 +162,27 @@ impl RuffSettingsIndex {

// If the directory is excluded from the workspace, skip it.
if let Some(file_name) = directory.file_name() {
if let Some((_, settings)) = index
let settings = index
.read()
.unwrap()
.range(..directory.clone())
.rfind(|(path, _)| directory.starts_with(path))
{
if match_exclusion(&directory, file_name, &settings.file_resolver.exclude) {
tracing::debug!("Ignored path via `exclude`: {}", directory.display());
return WalkState::Continue;
} else if match_exclusion(
&directory,
file_name,
&settings.file_resolver.extend_exclude,
) {
tracing::debug!(
"Ignored path via `extend-exclude`: {}",
directory.display()
);
return WalkState::Continue;
}
.map(|(_, settings)| settings.clone())
.unwrap_or_else(|| fallback.clone());

if match_exclusion(&directory, file_name, &settings.file_resolver.exclude) {
tracing::debug!("Ignored path via `exclude`: {}", directory.display());
return WalkState::Skip;
} else if match_exclusion(
&directory,
file_name,
&settings.file_resolver.extend_exclude,
) {
tracing::debug!(
"Ignored path via `extend-exclude`: {}",
directory.display()
);
return WalkState::Skip;
}
}

Expand All @@ -203,8 +209,6 @@ impl RuffSettingsIndex {
})
});

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

Self {
index: index.into_inner().unwrap(),
fallback,
Expand Down

0 comments on commit ebe5b06

Please sign in to comment.