Use fallback settings when indexing the project (#12362)

## 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.
This commit is contained in:
Dhruv Manilawala 2024-07-18 09:16:45 +05:30 committed by GitHub
parent b2a49d8140
commit ebe5b06c95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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() {
@ -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 {
@ -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()
@ -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;
}
}
@ -203,8 +209,6 @@ impl RuffSettingsIndex {
})
});
let fallback = Arc::new(RuffSettings::fallback(editor_settings, root));
Self {
index: index.into_inner().unwrap(),
fallback,