Show settings path in --show-settings output (#4199)

This commit is contained in:
Dhruv Manilawala 2023-05-04 11:52:31 +05:30 committed by GitHub
parent 37aae666c7
commit 59d40f9f81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 152 additions and 102 deletions

View file

@ -7,7 +7,7 @@ use log::{debug, error};
use rayon::prelude::*;
use ruff::linter::add_noqa_to_path;
use ruff::resolver::PyprojectDiscovery;
use ruff::resolver::PyprojectConfig;
use ruff::{packaging, resolver, warn_user_once};
use crate::args::Overrides;
@ -15,12 +15,12 @@ use crate::args::Overrides;
/// Add `noqa` directives to a collection of files.
pub fn add_noqa(
files: &[PathBuf],
pyproject_strategy: &PyprojectDiscovery,
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
) -> Result<usize> {
// Collect all the files to check.
let start = Instant::now();
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_strategy, overrides)?;
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_config, overrides)?;
let duration = start.elapsed();
debug!("Identified files to lint in: {:?}", duration);
@ -37,7 +37,7 @@ pub fn add_noqa(
.map(ignore::DirEntry::path)
.collect::<Vec<_>>(),
&resolver,
pyproject_strategy,
pyproject_config,
);
let start = Instant::now();
@ -50,7 +50,7 @@ pub fn add_noqa(
.parent()
.and_then(|parent| package_roots.get(parent))
.and_then(|package| *package);
let settings = resolver.resolve(path, pyproject_strategy);
let settings = resolver.resolve(path, pyproject_config);
match add_noqa_to_path(path, package, settings) {
Ok(count) => Some(count),
Err(e) => {

View file

@ -12,7 +12,7 @@ use ruff_text_size::{TextRange, TextSize};
use ruff::message::Message;
use ruff::registry::Rule;
use ruff::resolver::PyprojectDiscovery;
use ruff::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};
use ruff::settings::{flags, AllSettings};
use ruff::{fs, packaging, resolver, warn_user_once, IOError};
use ruff_diagnostics::Diagnostic;
@ -27,7 +27,7 @@ use crate::panic::catch_unwind;
/// Run the linter over a collection of files.
pub fn run(
files: &[PathBuf],
pyproject_strategy: &PyprojectDiscovery,
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
cache: flags::Cache,
noqa: flags::Noqa,
@ -35,7 +35,7 @@ pub 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_strategy, overrides)?;
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_config, overrides)?;
let duration = start.elapsed();
debug!("Identified files to lint in: {:?}", duration);
@ -52,12 +52,12 @@ pub fn run(
}
}
match &pyproject_strategy {
PyprojectDiscovery::Fixed(settings) => {
init_cache(&settings.cli.cache_dir);
match pyproject_config.strategy {
PyprojectDiscoveryStrategy::Fixed => {
init_cache(&pyproject_config.settings.cli.cache_dir);
}
PyprojectDiscovery::Hierarchical(default) => {
for settings in std::iter::once(default).chain(resolver.iter()) {
PyprojectDiscoveryStrategy::Hierarchical => {
for settings in std::iter::once(&pyproject_config.settings).chain(resolver.iter()) {
init_cache(&settings.cli.cache_dir);
}
}
@ -72,7 +72,7 @@ pub fn run(
.map(ignore::DirEntry::path)
.collect::<Vec<_>>(),
&resolver,
pyproject_strategy,
pyproject_config,
);
let start = Instant::now();
@ -86,7 +86,7 @@ pub fn run(
.parent()
.and_then(|parent| package_roots.get(parent))
.and_then(|package| *package);
let settings = resolver.resolve_all(path, pyproject_strategy);
let settings = resolver.resolve_all(path, pyproject_config);
lint_path(path, package, settings, cache, noqa, autofix).map_err(|e| {
(Some(path.to_owned()), {
@ -116,7 +116,7 @@ pub fn run(
fs::relativize_path(path).bold(),
":".bold()
);
let settings = resolver.resolve(path, pyproject_strategy);
let settings = resolver.resolve(path, pyproject_config);
if settings.rules.enabled(Rule::IOError) {
let file =
SourceFileBuilder::new(path.to_string_lossy().as_ref(), "").finish();
@ -196,7 +196,7 @@ mod test {
use path_absolutize::Absolutize;
use ruff::logging::LogLevel;
use ruff::resolver::PyprojectDiscovery;
use ruff::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};
use ruff::settings::configuration::{Configuration, RuleSelection};
use ruff::settings::flags::FixMode;
use ruff::settings::flags::{Cache, Noqa};
@ -238,7 +238,11 @@ mod test {
let diagnostics = run(
&[root_path.join("valid.ipynb")],
&PyprojectDiscovery::Fixed(AllSettings::from_configuration(configuration, &root_path)?),
&PyprojectConfig::new(
PyprojectDiscoveryStrategy::Fixed,
AllSettings::from_configuration(configuration, &root_path)?,
None,
),
&overrides,
Cache::Disabled,
Noqa::Enabled,

View file

@ -3,7 +3,7 @@ use std::path::Path;
use anyhow::Result;
use ruff::resolver::PyprojectDiscovery;
use ruff::resolver::PyprojectConfig;
use ruff::settings::flags;
use ruff::{packaging, resolver};
@ -20,22 +20,28 @@ fn read_from_stdin() -> Result<String> {
/// Run the linter over a single file, read from `stdin`.
pub fn run_stdin(
filename: Option<&Path>,
pyproject_strategy: &PyprojectDiscovery,
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
noqa: flags::Noqa,
autofix: flags::FixMode,
) -> Result<Diagnostics> {
if let Some(filename) = filename {
if !resolver::python_file_at_path(filename, pyproject_strategy, overrides)? {
if !resolver::python_file_at_path(filename, pyproject_config, overrides)? {
return Ok(Diagnostics::default());
}
}
let settings = pyproject_strategy.top_level_settings();
let package_root = filename
.and_then(Path::parent)
.and_then(|path| packaging::detect_package_root(path, &settings.lib.namespace_packages));
let package_root = filename.and_then(Path::parent).and_then(|path| {
packaging::detect_package_root(path, &pyproject_config.settings.lib.namespace_packages)
});
let stdin = read_from_stdin()?;
let mut diagnostics = lint_stdin(filename, package_root, &stdin, &settings.lib, noqa, autofix)?;
let mut diagnostics = lint_stdin(
filename,
package_root,
&stdin,
&pyproject_config.settings.lib,
noqa,
autofix,
)?;
diagnostics.messages.sort_unstable();
Ok(diagnostics)
}

View file

@ -4,7 +4,7 @@ use std::path::PathBuf;
use anyhow::Result;
use itertools::Itertools;
use ruff::resolver::PyprojectDiscovery;
use ruff::resolver::PyprojectConfig;
use ruff::{resolver, warn_user_once};
use crate::args::Overrides;
@ -12,11 +12,11 @@ use crate::args::Overrides;
/// Show the list of files to be checked based on current settings.
pub fn show_files(
files: &[PathBuf],
pyproject_strategy: &PyprojectDiscovery,
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
) -> Result<()> {
// Collect all files in the hierarchy.
let (paths, _resolver) = resolver::python_files_in_path(files, pyproject_strategy, overrides)?;
let (paths, _resolver) = 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

@ -5,18 +5,18 @@ use anyhow::{bail, Result};
use itertools::Itertools;
use ruff::resolver;
use ruff::resolver::PyprojectDiscovery;
use ruff::resolver::PyprojectConfig;
use crate::args::Overrides;
/// Print the user-facing configuration settings.
pub fn show_settings(
files: &[PathBuf],
pyproject_strategy: &PyprojectDiscovery,
pyproject_config: &PyprojectConfig,
overrides: &Overrides,
) -> Result<()> {
// Collect all files in the hierarchy.
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_strategy, overrides)?;
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_config, overrides)?;
// Print the list of files.
let Some(entry) = paths
@ -26,10 +26,13 @@ pub fn show_settings(
bail!("No files found under the given path");
};
let path = entry.path();
let settings = resolver.resolve(path, pyproject_strategy);
let settings = resolver.resolve(path, pyproject_config);
let mut stdout = BufWriter::new(io::stdout().lock());
writeln!(stdout, "Resolved settings for: {path:?}")?;
if let Some(settings_path) = pyproject_config.path.as_ref() {
writeln!(stdout, "Settings path: {settings_path:?}")?;
}
writeln!(stdout, "{settings:#?}")?;
Ok(())

View file

@ -97,7 +97,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
// Construct the "default" settings. These are used when no `pyproject.toml`
// files are present, or files are injected from outside of the hierarchy.
let pyproject_strategy = resolve::resolve(
let pyproject_config = resolve::resolve(
cli.isolated,
cli.config.as_deref(),
&overrides,
@ -105,16 +105,14 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
)?;
if cli.show_settings {
commands::show_settings::show_settings(&cli.files, &pyproject_strategy, &overrides)?;
commands::show_settings::show_settings(&cli.files, &pyproject_config, &overrides)?;
return Ok(ExitStatus::Success);
}
if cli.show_files {
commands::show_files::show_files(&cli.files, &pyproject_strategy, &overrides)?;
commands::show_files::show_files(&cli.files, &pyproject_config, &overrides)?;
return Ok(ExitStatus::Success);
}
let top_level_settings = pyproject_strategy.top_level_settings();
// Extract options that are included in `Settings`, but only apply at the top
// level.
let CliSettings {
@ -124,7 +122,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
show_fixes,
update_check,
..
} = top_level_settings.cli.clone();
} = pyproject_config.settings.cli;
// Autofix rules are as follows:
// - If `--fix` or `--fix-only` is set, always apply fixes to the filesystem (or
@ -155,7 +153,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
printer_flags |= PrinterFlags::SHOW_FIXES;
}
if top_level_settings.lib.show_source {
if pyproject_config.settings.lib.show_source {
printer_flags |= PrinterFlags::SHOW_SOURCE;
}
@ -171,7 +169,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
warn_user_once!("--fix is incompatible with --add-noqa.");
}
let modifications =
commands::add_noqa::add_noqa(&cli.files, &pyproject_strategy, &overrides)?;
commands::add_noqa::add_noqa(&cli.files, &pyproject_config, &overrides)?;
if modifications > 0 && log_level >= LogLevel::Default {
let s = if modifications == 1 { "" } else { "s" };
#[allow(clippy::print_stderr)]
@ -195,7 +193,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
let messages = commands::run::run(
&cli.files,
&pyproject_strategy,
&pyproject_config,
&overrides,
cache.into(),
noqa.into(),
@ -225,7 +223,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
let messages = commands::run::run(
&cli.files,
&pyproject_strategy,
&pyproject_config,
&overrides,
cache.into(),
noqa.into(),
@ -244,7 +242,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
let diagnostics = if is_stdin {
commands::run_stdin::run_stdin(
cli.stdin_filename.map(fs::normalize_path).as_deref(),
&pyproject_strategy,
&pyproject_config,
&overrides,
noqa.into(),
autofix,
@ -252,7 +250,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
} else {
commands::run::run(
&cli.files,
&pyproject_strategy,
&pyproject_config,
&overrides,
cache.into(),
noqa.into(),

View file

@ -4,7 +4,8 @@ use anyhow::Result;
use path_absolutize::path_dedot;
use ruff::resolver::{
resolve_settings_with_processor, ConfigProcessor, PyprojectDiscovery, Relativity,
resolve_settings_with_processor, ConfigProcessor, PyprojectConfig, PyprojectDiscoveryStrategy,
Relativity,
};
use ruff::settings::configuration::Configuration;
use ruff::settings::{pyproject, AllSettings};
@ -18,13 +19,17 @@ pub fn resolve(
config: Option<&Path>,
overrides: &Overrides,
stdin_filename: Option<&Path>,
) -> Result<PyprojectDiscovery> {
) -> Result<PyprojectConfig> {
// First priority: if we're running in isolated mode, use the default settings.
if isolated {
let mut config = Configuration::default();
overrides.process_config(&mut config);
let settings = AllSettings::from_configuration(config, &path_dedot::CWD)?;
return Ok(PyprojectDiscovery::Fixed(settings));
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Fixed,
settings,
None,
));
}
// Second priority: the user specified a `pyproject.toml` file. Use that
@ -36,7 +41,11 @@ pub fn resolve(
.transpose()?
{
let settings = resolve_settings_with_processor(&pyproject, &Relativity::Cwd, overrides)?;
return Ok(PyprojectDiscovery::Fixed(settings));
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Fixed,
settings,
Some(pyproject),
));
}
// Third priority: find a `pyproject.toml` file in either an ancestor of
@ -50,7 +59,11 @@ pub fn resolve(
.unwrap_or(&path_dedot::CWD.as_path()),
)? {
let settings = resolve_settings_with_processor(&pyproject, &Relativity::Parent, overrides)?;
return Ok(PyprojectDiscovery::Hierarchical(settings));
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
Some(pyproject),
));
}
// Fourth priority: find a user-specific `pyproject.toml`, but resolve all paths
@ -59,7 +72,11 @@ pub fn resolve(
// these act as the "default" settings.)
if let Some(pyproject) = pyproject::find_user_settings_toml() {
let settings = resolve_settings_with_processor(&pyproject, &Relativity::Cwd, overrides)?;
return Ok(PyprojectDiscovery::Hierarchical(settings));
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
Some(pyproject),
));
}
// Fallback: load Ruff's default settings, and resolve all paths relative to the
@ -69,5 +86,9 @@ pub fn resolve(
let mut config = Configuration::default();
overrides.process_config(&mut config);
let settings = AllSettings::from_configuration(config, &path_dedot::CWD)?;
Ok(PyprojectDiscovery::Hierarchical(settings))
Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
None,
))
}