Skip to content

Commit

Permalink
Unify Settings and AllSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 20, 2023
1 parent 192463c commit c13f93f
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 123 deletions.
39 changes: 18 additions & 21 deletions crates/ruff_cli/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,18 @@ pub(crate) struct Cache {
impl Cache {
/// Open or create a new cache.
///
/// `cache_dir` is considered the root directory of the cache, which can be
/// local to the project, global or otherwise set by the user.
///
/// `package_root` is the path to root of the package that is contained
/// within this cache and must be canonicalized (to avoid considering `./`
/// and `../project` being different).
///
/// Finally `settings` is used to ensure we don't open a cache for different
/// settings.
pub(crate) fn open(cache_dir: &Path, package_root: PathBuf, settings: &Settings) -> Cache {
/// settings. It also defines the directory where to store the cache.
pub(crate) fn open(package_root: PathBuf, settings: &Settings) -> Cache {
debug_assert!(package_root.is_absolute(), "package root not canonicalized");

let mut buf = itoa::Buffer::new();
let key = Path::new(buf.format(cache_key(&package_root, settings)));
let path = PathBuf::from_iter([cache_dir, Path::new("content"), key]);
let path = PathBuf::from_iter([&settings.cache_dir, Path::new("content"), key]);

let file = match File::open(&path) {
Ok(file) => file,
Expand Down Expand Up @@ -350,7 +347,7 @@ mod tests {

use itertools::Itertools;
use ruff_cache::CACHE_DIR_NAME;
use ruff_linter::settings::{flags, AllSettings, Settings};
use ruff_linter::settings::{flags, Settings};

use crate::cache::RelativePathBuf;
use crate::cache::{self, Cache, FileCache};
Expand All @@ -371,10 +368,13 @@ mod tests {
let _ = fs::remove_dir_all(&cache_dir);
cache::init(&cache_dir).unwrap();

let settings = Settings::default();
let settings = Settings {
cache_dir,
..Settings::default()
};

let package_root = fs::canonicalize(package_root).unwrap();
let cache = Cache::open(&cache_dir, package_root.clone(), &settings);
let cache = Cache::open(package_root.clone(), &settings);
assert_eq!(cache.new_files.lock().unwrap().len(), 0);

let mut paths = Vec::new();
Expand Down Expand Up @@ -426,7 +426,7 @@ mod tests {

cache.store().unwrap();

let cache = Cache::open(&cache_dir, package_root.clone(), &settings);
let cache = Cache::open(package_root.clone(), &settings);
assert_ne!(cache.package.files.len(), 0);

parse_errors.sort();
Expand Down Expand Up @@ -651,9 +651,8 @@ mod tests {
}

struct TestCache {
cache_dir: PathBuf,
package_root: PathBuf,
settings: AllSettings,
settings: Settings,
}

impl TestCache {
Expand All @@ -672,10 +671,12 @@ mod tests {
cache::init(&cache_dir).unwrap();
fs::create_dir(package_root.clone()).unwrap();

let settings = AllSettings::default();
let settings = Settings {
cache_dir,
..Settings::default()
};

Self {
cache_dir,
package_root,
settings,
}
Expand All @@ -695,11 +696,7 @@ mod tests {
}

fn open(&self) -> Cache {
Cache::open(
&self.cache_dir,
self.package_root.clone(),
&self.settings.lib,
)
Cache::open(self.package_root.clone(), &self.settings)
}

fn lint_file_with_cache(
Expand All @@ -710,7 +707,7 @@ mod tests {
lint_path(
&self.package_root.join(path),
Some(&self.package_root),
&self.settings.lib,
&self.settings,
Some(cache),
flags::Noqa::Enabled,
flags::FixMode::Generate,
Expand All @@ -720,7 +717,7 @@ mod tests {

impl Drop for TestCache {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.cache_dir);
let _ = fs::remove_dir_all(&self.settings.cache_dir);
}
}
}
21 changes: 7 additions & 14 deletions crates/ruff_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ pub(crate) fn check(

match pyproject_config.strategy {
PyprojectDiscoveryStrategy::Fixed => {
init_cache(&pyproject_config.settings.cli.cache_dir);
init_cache(&pyproject_config.settings.cache_dir);
}
PyprojectDiscoveryStrategy::Hierarchical => {
for settings in
std::iter::once(&pyproject_config.settings).chain(resolver.settings())
{
init_cache(&settings.cli.cache_dir);
init_cache(&settings.cache_dir);
}
}
}
Expand All @@ -88,12 +88,8 @@ pub(crate) fn check(
.unique()
.par_bridge()
.map(|cache_root| {
let settings = resolver.resolve_all(cache_root, pyproject_config);
let cache = Cache::open(
&settings.cli.cache_dir,
cache_root.to_path_buf(),
&settings.lib,
);
let settings = resolver.resolve(cache_root, pyproject_config);
let cache = Cache::open(cache_root.to_path_buf(), settings);
(cache_root, cache)
})
.collect::<HashMap<&Path, Cache>>()
Expand Down Expand Up @@ -242,7 +238,7 @@ mod test {

use ruff_linter::message::{Emitter, EmitterContext, TextEmitter};
use ruff_linter::registry::Rule;
use ruff_linter::settings::{flags, AllSettings, CliSettings, Settings};
use ruff_linter::settings::{flags, Settings};
use ruff_workspace::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};

use crate::args::Overrides;
Expand Down Expand Up @@ -271,11 +267,8 @@ mod test {

// Configure
let snapshot = format!("{}_{}", rule_code.noqa_code(), path);
let settings = AllSettings {
cli: CliSettings::default(),
// invalid pyproject.toml is not active by default
lib: Settings::for_rules(vec![rule_code, Rule::InvalidPyprojectToml]),
};
// invalid pyproject.toml is not active by default
let settings = Settings::for_rules(vec![rule_code, Rule::InvalidPyprojectToml]);
let pyproject_config =
PyprojectConfig::new(PyprojectDiscoveryStrategy::Fixed, settings, None);

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/check_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ pub(crate) fn check_stdin(
}
}
let package_root = filename.and_then(Path::parent).and_then(|path| {
packaging::detect_package_root(path, &pyproject_config.settings.lib.namespace_packages)
packaging::detect_package_root(path, &pyproject_config.settings.namespace_packages)
});
let stdin = read_from_stdin()?;
let mut diagnostics = lint_stdin(
filename,
package_root,
stdin,
&pyproject_config.settings.lib,
&pyproject_config.settings,
noqa,
autofix,
)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/format_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ pub(crate) fn format_stdin(cli: &FormatArguments, overrides: &Overrides) -> Resu
// Format the file.
let path = cli.stdin_filename.as_deref();

let preview = match pyproject_config.settings.lib.preview {
let preview = match pyproject_config.settings.preview {
PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled,
PreviewMode::Disabled => ruff_python_formatter::PreviewMode::Disabled,
};
let line_length = pyproject_config.settings.lib.line_length;
let line_length = pyproject_config.settings.line_length;

let options = path
.map(PyFormatOptions::from_extension)
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use notify::{recommended_watcher, RecursiveMode, Watcher};

use ruff_linter::logging::{set_up_logging, LogLevel};
use ruff_linter::settings::types::SerializationFormat;
use ruff_linter::settings::{flags, CliSettings};
use ruff_linter::settings::{flags, Settings};
use ruff_linter::{fs, warn_user_once};

use crate::args::{Args, CheckCommand, Command, FormatCommand};
Expand Down Expand Up @@ -216,14 +216,14 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {

// Extract options that are included in `Settings`, but only apply at the top
// level.
let CliSettings {
let Settings {
fix,
fix_only,
format,
show_fixes,
show_source,
..
} = pyproject_config.settings.cli;
} = pyproject_config.settings;

// Autofix rules are as follows:
// - By default, generate all fixes, but don't apply them to the filesystem.
Expand Down
13 changes: 6 additions & 7 deletions crates/ruff_cli/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use path_absolutize::path_dedot;
use ruff_workspace::configuration::Configuration;
use ruff_workspace::pyproject;
use ruff_workspace::resolver::{
resolve_settings_with_processor, ConfigProcessor, PyprojectConfig, PyprojectDiscoveryStrategy,
Relativity,
resolve_root_settings, ConfigProcessor, PyprojectConfig, PyprojectDiscoveryStrategy, Relativity,
};

use crate::args::Overrides;
Expand All @@ -25,7 +24,7 @@ pub fn resolve(
if isolated {
let mut config = Configuration::default();
overrides.process_config(&mut config);
let settings = config.into_all_settings(&path_dedot::CWD)?;
let settings = config.into_settings(&path_dedot::CWD)?;
debug!("Isolated mode, not reading any pyproject.toml");
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Fixed,
Expand All @@ -42,7 +41,7 @@ pub fn resolve(
.map(|config| shellexpand::full(&config).map(|config| PathBuf::from(config.as_ref())))
.transpose()?
{
let settings = resolve_settings_with_processor(&pyproject, Relativity::Cwd, overrides)?;
let settings = resolve_root_settings(&pyproject, Relativity::Cwd, overrides)?;
debug!(
"Using user specified pyproject.toml at {}",
pyproject.display()
Expand All @@ -65,7 +64,7 @@ pub fn resolve(
.unwrap_or(&path_dedot::CWD.as_path()),
)? {
debug!("Using pyproject.toml (parent) at {}", pyproject.display());
let settings = resolve_settings_with_processor(&pyproject, Relativity::Parent, overrides)?;
let settings = resolve_root_settings(&pyproject, Relativity::Parent, overrides)?;
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
Expand All @@ -79,7 +78,7 @@ pub fn resolve(
// these act as the "default" settings.)
if let Some(pyproject) = pyproject::find_user_settings_toml() {
debug!("Using pyproject.toml (cwd) at {}", pyproject.display());
let settings = resolve_settings_with_processor(&pyproject, Relativity::Cwd, overrides)?;
let settings = resolve_root_settings(&pyproject, Relativity::Cwd, overrides)?;
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
Expand All @@ -94,7 +93,7 @@ pub fn resolve(
debug!("Using Ruff default settings");
let mut config = Configuration::default();
overrides.process_config(&mut config);
let settings = config.into_all_settings(&path_dedot::CWD)?;
let settings = config.into_settings(&path_dedot::CWD)?;
Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_dev/src/format_dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn ruff_check_paths(
cli.stdin_filename.as_deref(),
)?;
// We don't want to format pyproject.toml
pyproject_config.settings.lib.include = FilePatternSet::try_from_vec(vec![
pyproject_config.settings.include = FilePatternSet::try_from_vec(vec![
FilePattern::Builtin("*.py"),
FilePattern::Builtin("*.pyi"),
])
Expand Down
13 changes: 11 additions & 2 deletions crates/ruff_linter/src/settings/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use once_cell::sync::Lazy;
use path_absolutize::path_dedot;
use regex::Regex;
use ruff_cache::cache_dir;
use rustc_hash::FxHashSet;
use std::collections::HashSet;

Expand All @@ -17,7 +18,7 @@ use crate::rules::{
flake8_tidy_imports, flake8_type_checking, flake8_unused_arguments, isort, mccabe, pep8_naming,
pycodestyle, pydocstyle, pyflakes, pylint, pyupgrade,
};
use crate::settings::types::FilePatternSet;
use crate::settings::types::{FilePatternSet, SerializationFormat};

pub const PREFIXES: &[RuleSelector] = &[
RuleSelector::Prefix {
Expand Down Expand Up @@ -72,7 +73,15 @@ pub static INCLUDE: Lazy<Vec<FilePattern>> = Lazy::new(|| {

impl Default for Settings {
fn default() -> Self {
let project_root = path_dedot::CWD.clone();
Self {
cache_dir: cache_dir(&project_root),
fix: false,
fix_only: false,
format: SerializationFormat::default(),
show_fixes: false,
show_source: false,

rules: PREFIXES
.iter()
.flat_map(|selector| selector.rules(PreviewMode::default()))
Expand All @@ -92,7 +101,7 @@ impl Default for Settings {
namespace_packages: vec![],
preview: PreviewMode::default(),
per_file_ignores: vec![],
project_root: path_dedot::CWD.clone(),
project_root,
respect_gitignore: true,
src: vec![path_dedot::CWD.clone()],
tab_size: TabSize::default(),
Expand Down
21 changes: 8 additions & 13 deletions crates/ruff_linter/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,22 @@ pub mod flags;
pub mod rule_table;
pub mod types;

#[derive(Debug, Default)]
pub struct AllSettings {
pub cli: CliSettings,
pub lib: Settings,
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, CacheKey)]
#[allow(clippy::struct_excessive_bools)]
/// Settings that are not used by this library and only here so that `ruff_cli` can use them.
pub struct CliSettings {
pub struct Settings {
#[cache_key(ignore)]
pub cache_dir: PathBuf,
#[cache_key(ignore)]
pub fix: bool,
#[cache_key(ignore)]
pub fix_only: bool,
#[cache_key(ignore)]
pub format: SerializationFormat,
#[cache_key(ignore)]
pub show_fixes: bool,
#[cache_key(ignore)]
pub show_source: bool,
}

#[derive(Debug, CacheKey)]
#[allow(clippy::struct_excessive_bools)]
pub struct Settings {
pub rules: RuleTable,
pub per_file_ignores: Vec<(GlobMatcher, GlobMatcher, RuleSet)>,

Expand Down
Loading

0 comments on commit c13f93f

Please sign in to comment.