Move Configuration to ruff_workspace crate (#6920)

This commit is contained in:
Micha Reiser 2023-08-28 08:21:35 +02:00 committed by GitHub
parent 039694aaed
commit a6aa16630d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
77 changed files with 3704 additions and 4108 deletions

View file

@ -8,12 +8,12 @@ use rustc_hash::FxHashMap;
use ruff::line_width::LineLength;
use ruff::logging::LogLevel;
use ruff::registry::Rule;
use ruff::resolver::ConfigProcessor;
use ruff::settings::configuration::RuleSelection;
use ruff::settings::types::{
FilePattern, PatternPrefixPair, PerFileIgnore, PythonVersion, SerializationFormat,
};
use ruff::RuleSelector;
use ruff_workspace::configuration::{Configuration, RuleSelection};
use ruff_workspace::resolver::ConfigProcessor;
#[derive(Debug, Parser)]
#[command(
@ -514,7 +514,7 @@ pub struct Overrides {
}
impl ConfigProcessor for Overrides {
fn process_config(&self, config: &mut ruff::settings::configuration::Configuration) {
fn process_config(&self, config: &mut Configuration) {
if let Some(cache_dir) = &self.cache_dir {
config.cache_dir = Some(cache_dir.clone());
}

View file

@ -7,9 +7,9 @@ use log::{debug, error};
use rayon::prelude::*;
use ruff::linter::add_noqa_to_path;
use ruff::resolver::PyprojectConfig;
use ruff::{packaging, resolver, warn_user_once};
use ruff::warn_user_once;
use ruff_python_stdlib::path::{is_jupyter_notebook, is_project_toml};
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig};
use crate::args::Overrides;
@ -21,7 +21,7 @@ pub(crate) fn add_noqa(
) -> Result<usize> {
// Collect all the files to check.
let start = Instant::now();
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_config, overrides)?;
let (paths, resolver) = python_files_in_path(files, pyproject_config, overrides)?;
let duration = start.elapsed();
debug!("Identified files to lint in: {:?}", duration);
@ -31,13 +31,12 @@ pub(crate) fn add_noqa(
}
// Discover the package root for each Python file.
let package_roots = packaging::detect_package_roots(
let package_roots = resolver.package_roots(
&paths
.iter()
.flatten()
.map(ignore::DirEntry::path)
.collect::<Vec<_>>(),
&resolver,
pyproject_config,
);

View file

@ -1,6 +1,5 @@
use ruff::settings::options::Options;
use crate::ExitStatus;
use ruff_workspace::options::Options;
#[allow(clippy::print_stdout)]
pub(crate) fn config(key: Option<&str>) -> ExitStatus {

View file

@ -9,11 +9,11 @@ use thiserror::Error;
use tracing::{span, Level};
use ruff::fs;
use ruff::resolver::python_files_in_path;
use ruff::warn_user_once;
use ruff_formatter::LineWidth;
use ruff_python_ast::PySourceType;
use ruff_python_formatter::{format_module, FormatModuleError, PyFormatOptions};
use ruff_workspace::resolver::python_files_in_path;
use crate::args::{Arguments, Overrides};
use crate::resolve::resolve;

View file

@ -11,16 +11,16 @@ use itertools::Itertools;
use log::{debug, error, warn};
#[cfg(not(target_family = "wasm"))]
use rayon::prelude::*;
use ruff_text_size::{TextRange, TextSize};
use ruff::message::Message;
use ruff::registry::Rule;
use ruff::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};
use ruff::settings::{flags, AllSettings};
use ruff::{fs, packaging, resolver, warn_user_once, IOError};
use ruff::{fs, warn_user_once, IOError};
use ruff_diagnostics::Diagnostic;
use ruff_python_ast::imports::ImportMap;
use ruff_source_file::SourceFileBuilder;
use ruff_text_size::{TextRange, TextSize};
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig, PyprojectDiscoveryStrategy};
use crate::args::Overrides;
use crate::cache::{self, Cache};
@ -38,7 +38,7 @@ pub(crate) fn run(
) -> Result<Diagnostics> {
// Collect all the Python files to check.
let start = Instant::now();
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_config, overrides)?;
let (paths, resolver) = python_files_in_path(files, pyproject_config, overrides)?;
let duration = start.elapsed();
debug!("Identified files to lint in: {:?}", duration);
@ -60,7 +60,9 @@ pub(crate) fn run(
init_cache(&pyproject_config.settings.cli.cache_dir);
}
PyprojectDiscoveryStrategy::Hierarchical => {
for settings in std::iter::once(&pyproject_config.settings).chain(resolver.iter()) {
for settings in
std::iter::once(&pyproject_config.settings).chain(resolver.settings())
{
init_cache(&settings.cli.cache_dir);
}
}
@ -68,13 +70,12 @@ pub(crate) fn run(
};
// Discover the package root for each Python file.
let package_roots = packaging::detect_package_roots(
let package_roots = resolver.package_roots(
&paths
.iter()
.flatten()
.map(ignore::DirEntry::path)
.collect::<Vec<_>>(),
&resolver,
pyproject_config,
);
@ -230,18 +231,22 @@ with the relevant file contents, the `pyproject.toml` settings, and the followin
#[cfg(test)]
#[cfg(unix)]
mod test {
use super::run;
use crate::args::Overrides;
use anyhow::Result;
use ruff::message::{Emitter, EmitterContext, TextEmitter};
use ruff::registry::Rule;
use ruff::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};
use ruff::settings::{flags, AllSettings, CliSettings, Settings};
use rustc_hash::FxHashMap;
use std::fs;
use std::os::unix::fs::OpenOptionsExt;
use anyhow::Result;
use rustc_hash::FxHashMap;
use tempfile::TempDir;
use ruff::message::{Emitter, EmitterContext, TextEmitter};
use ruff::registry::Rule;
use ruff::settings::{flags, AllSettings, CliSettings, Settings};
use ruff_workspace::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};
use crate::args::Overrides;
use super::run;
/// We check that regular python files, pyproject.toml and jupyter notebooks all handle io
/// errors gracefully
#[test]

View file

@ -3,9 +3,9 @@ use std::path::Path;
use anyhow::Result;
use ruff::resolver::PyprojectConfig;
use ruff::packaging;
use ruff::settings::flags;
use ruff::{packaging, resolver};
use ruff_workspace::resolver::{python_file_at_path, PyprojectConfig};
use crate::args::Overrides;
use crate::diagnostics::{lint_stdin, Diagnostics};
@ -26,7 +26,7 @@ pub(crate) fn run_stdin(
autofix: flags::FixMode,
) -> Result<Diagnostics> {
if let Some(filename) = filename {
if !resolver::python_file_at_path(filename, pyproject_config, overrides)? {
if !python_file_at_path(filename, pyproject_config, overrides)? {
return Ok(Diagnostics::default());
}
}

View file

@ -4,8 +4,8 @@ use std::path::PathBuf;
use anyhow::Result;
use itertools::Itertools;
use ruff::resolver::PyprojectConfig;
use ruff::{resolver, warn_user_once};
use ruff::warn_user_once;
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig};
use crate::args::Overrides;
@ -17,7 +17,7 @@ pub(crate) fn show_files(
writer: &mut impl Write,
) -> Result<()> {
// Collect all files in the hierarchy.
let (paths, _resolver) = resolver::python_files_in_path(files, pyproject_config, overrides)?;
let (paths, _resolver) = python_files_in_path(files, pyproject_config, overrides)?;
if paths.is_empty() {
warn_user_once!("No Python files found under the given path(s)");

View file

@ -4,8 +4,7 @@ use std::path::PathBuf;
use anyhow::{bail, Result};
use itertools::Itertools;
use ruff::resolver;
use ruff::resolver::PyprojectConfig;
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig};
use crate::args::Overrides;
@ -17,7 +16,7 @@ pub(crate) fn show_settings(
writer: &mut impl Write,
) -> Result<()> {
// Collect all files in the hierarchy.
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_config, overrides)?;
let (paths, resolver) = python_files_in_path(files, pyproject_config, overrides)?;
// Print the list of files.
let Some(entry) = paths

View file

@ -4,12 +4,12 @@ use anyhow::Result;
use log::debug;
use path_absolutize::path_dedot;
use ruff::resolver::{
use ruff_workspace::configuration::Configuration;
use ruff_workspace::pyproject;
use ruff_workspace::resolver::{
resolve_settings_with_processor, ConfigProcessor, PyprojectConfig, PyprojectDiscoveryStrategy,
Relativity,
};
use ruff::settings::configuration::Configuration;
use ruff::settings::{pyproject, AllSettings};
use crate::args::Overrides;
@ -25,7 +25,7 @@ pub fn resolve(
if isolated {
let mut config = Configuration::default();
overrides.process_config(&mut config);
let settings = AllSettings::from_configuration(config, &path_dedot::CWD)?;
let settings = config.into_all_settings(&path_dedot::CWD)?;
debug!("Isolated mode, not reading any pyproject.toml");
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Fixed,
@ -42,7 +42,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_settings_with_processor(&pyproject, Relativity::Cwd, overrides)?;
debug!(
"Using user specified pyproject.toml at {}",
pyproject.display()
@ -65,7 +65,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_settings_with_processor(&pyproject, Relativity::Parent, overrides)?;
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
@ -79,7 +79,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_settings_with_processor(&pyproject, Relativity::Cwd, overrides)?;
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
@ -94,7 +94,7 @@ pub fn resolve(
debug!("Using Ruff default settings");
let mut config = Configuration::default();
overrides.process_config(&mut config);
let settings = AllSettings::from_configuration(config, &path_dedot::CWD)?;
let settings = config.into_all_settings(&path_dedot::CWD)?;
Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,