Extract ResolverSettings

## Stack Summary

This stack splits `Settings` into `FormatterSettings` and `LinterSettings` and moves it into `ruff_workspace`. This change is necessary to add the `FormatterSettings` to `Settings` without adding `ruff_python_formatter` as a dependency to `ruff_linter` (and the linter should not contain the formatter settings). 

A quick overview of our settings struct at play:

* `Options`: 1:1 representation of the options in the `pyproject.toml` or `ruff.toml`.  Used for deserialization.
* `Configuration`: Resolved `Options`, potentially merged from multiple configurations (when using `extend`). The representation is very close if not identical to the `Options`.
* `Settings`: The resolved configuration that uses a data format optimized for reading. Optional fields are initialized with their default values. Initialized by `Configuration::into_settings` .

The goal of this stack is to split `Settings` into tool-specific resolved `Settings` that are independent of each other. This comes at the advantage that the individual crates don't need to know anything about the other tools. The downside is that information gets duplicated between `Settings`. Right now the duplication is minimal (`line-length`, `tab-width`) but we may need to come up with a solution if more expensive data needs sharing.

This stack focuses on `Settings`. Splitting `Configuration` into some smaller structs is something I'll follow up on later. 

## PR Summary

This PR extracts a `ResolverSettings` struct that holds all the resolver-relevant fields (uninteresting for the `Formatter` or `Linter`). This will allow us to move the `ResolverSettings` out of `ruff_linter` further up in the stack.


## Test Plan

`cargo test`

(I'll to more extensive testing at the top of this stack)
This commit is contained in:
Micha Reiser 2023-09-20 16:37:49 +02:00 committed by GitHub
parent 83daddbeb7
commit 8f41eab0c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 117 additions and 86 deletions

View file

@ -27,7 +27,9 @@ use ruff_linter::settings::types::{
FilePattern, FilePatternSet, PerFileIgnore, PreviewMode, PythonVersion, SerializationFormat,
Version,
};
use ruff_linter::settings::{defaults, resolve_per_file_ignores, Settings};
use ruff_linter::settings::{
defaults, resolve_per_file_ignores, FileResolverSettings, Settings, EXCLUDE, INCLUDE,
};
use ruff_linter::{
fs, warn_user, warn_user_once, warn_user_once_by_id, RuleSelector, RUFF_PKG_VERSION,
};
@ -139,16 +141,20 @@ impl Configuration {
dummy_variable_rgx: self
.dummy_variable_rgx
.unwrap_or_else(|| defaults::DUMMY_VARIABLE_RGX.clone()),
exclude: FilePatternSet::try_from_vec(
self.exclude.unwrap_or_else(|| defaults::EXCLUDE.clone()),
)?,
extend_exclude: FilePatternSet::try_from_vec(self.extend_exclude)?,
extend_include: FilePatternSet::try_from_vec(self.extend_include)?,
file_resolver: FileResolverSettings {
exclude: FilePatternSet::try_from_vec(
self.exclude.unwrap_or_else(|| EXCLUDE.clone()),
)?,
extend_exclude: FilePatternSet::try_from_vec(self.extend_exclude)?,
extend_include: FilePatternSet::try_from_vec(self.extend_include)?,
force_exclude: self.force_exclude.unwrap_or(false),
include: FilePatternSet::try_from_vec(
self.include.unwrap_or_else(|| INCLUDE.clone()),
)?,
respect_gitignore: self.respect_gitignore.unwrap_or(true),
project_root: project_root.to_path_buf(),
},
external: FxHashSet::from_iter(self.external.unwrap_or_default()),
force_exclude: self.force_exclude.unwrap_or(false),
include: FilePatternSet::try_from_vec(
self.include.unwrap_or_else(|| defaults::INCLUDE.clone()),
)?,
ignore_init_module_imports: self.ignore_init_module_imports.unwrap_or_default(),
line_length: self.line_length.unwrap_or_default(),
tab_size: self.tab_size.unwrap_or_default(),
@ -160,9 +166,7 @@ impl Configuration {
.chain(self.extend_per_file_ignores)
.collect(),
)?,
respect_gitignore: self.respect_gitignore.unwrap_or(true),
src: self.src.unwrap_or_else(|| vec![project_root.to_path_buf()]),
project_root: project_root.to_path_buf(),
target_version: self.target_version.unwrap_or_default(),
task_tags: self.task_tags.unwrap_or_else(|| {
defaults::TASK_TAGS

View file

@ -299,7 +299,7 @@ pub fn python_files_in_path(
}
// Check if the paths themselves are excluded.
if pyproject_config.settings.force_exclude {
if pyproject_config.settings.file_resolver.force_exclude {
paths.retain(|path| !is_file_excluded(path, &resolver, pyproject_config));
if paths.is_empty() {
return Ok((vec![], resolver));
@ -315,7 +315,7 @@ pub fn python_files_in_path(
for path in &paths[1..] {
builder.add(path);
}
builder.standard_filters(pyproject_config.settings.respect_gitignore);
builder.standard_filters(pyproject_config.settings.file_resolver.respect_gitignore);
builder.hidden(false);
let walker = builder.build_parallel();
@ -333,13 +333,17 @@ pub fn python_files_in_path(
let resolver = resolver.read().unwrap();
let settings = resolver.resolve(path, pyproject_config);
if let Some(file_name) = path.file_name() {
if !settings.exclude.is_empty()
&& match_exclusion(path, file_name, &settings.exclude)
if !settings.file_resolver.exclude.is_empty()
&& match_exclusion(path, file_name, &settings.file_resolver.exclude)
{
debug!("Ignored path via `exclude`: {:?}", path);
return WalkState::Skip;
} else if !settings.extend_exclude.is_empty()
&& match_exclusion(path, file_name, &settings.extend_exclude)
} else if !settings.file_resolver.extend_exclude.is_empty()
&& match_exclusion(
path,
file_name,
&settings.file_resolver.extend_exclude,
)
{
debug!("Ignored path via `extend-exclude`: {:?}", path);
return WalkState::Skip;
@ -395,10 +399,10 @@ pub fn python_files_in_path(
let path = entry.path();
let resolver = resolver.read().unwrap();
let settings = resolver.resolve(path, pyproject_config);
if settings.include.is_match(path) {
if settings.file_resolver.include.is_match(path) {
debug!("Included path via `include`: {:?}", path);
true
} else if settings.extend_include.is_match(path) {
} else if settings.file_resolver.extend_include.is_match(path) {
debug!("Included path via `extend-include`: {:?}", path);
true
} else {
@ -424,7 +428,7 @@ pub fn python_file_at_path(
pyproject_config: &PyprojectConfig,
transformer: &dyn ConfigurationTransformer,
) -> Result<bool> {
if !pyproject_config.settings.force_exclude {
if !pyproject_config.settings.file_resolver.force_exclude {
return Ok(true);
}
@ -460,11 +464,13 @@ fn is_file_excluded(
}
let settings = resolver.resolve(path, pyproject_strategy);
if let Some(file_name) = path.file_name() {
if !settings.exclude.is_empty() && match_exclusion(path, file_name, &settings.exclude) {
if !settings.file_resolver.exclude.is_empty()
&& match_exclusion(path, file_name, &settings.file_resolver.exclude)
{
debug!("Ignored path via `exclude`: {:?}", path);
return true;
} else if !settings.extend_exclude.is_empty()
&& match_exclusion(path, file_name, &settings.extend_exclude)
} else if !settings.file_resolver.extend_exclude.is_empty()
&& match_exclusion(path, file_name, &settings.file_resolver.extend_exclude)
{
debug!("Ignored path via `extend-exclude`: {:?}", path);
return true;
@ -473,7 +479,7 @@ fn is_file_excluded(
debug!("Ignored path due to error in parsing: {:?}", path);
return true;
}
if path == settings.project_root {
if path == settings.file_resolver.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;