mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Allow arbitrary configuration options to be overridden via the CLI (#9599)
Fixes #8368 Fixes https://github.com/astral-sh/ruff/issues/9186 ## Summary Arbitrary TOML strings can be provided via the command-line to override configuration options in `pyproject.toml` or `ruff.toml`. As an example: to run over typeshed and respect typeshed's `pyproject.toml`, but override a specific isort setting and enable an additional pep8-naming setting: ``` cargo run -- check ../typeshed --no-cache --config ../typeshed/pyproject.toml --config "lint.isort.combine-as-imports=false" --config "lint.extend-select=['N801']" ``` --------- Co-authored-by: Micha Reiser <micha@reiser.io> Co-authored-by: Zanie Blue <contact@zanie.dev>
This commit is contained in:
parent
b21ba71ef4
commit
8ec56277e9
21 changed files with 1099 additions and 235 deletions
|
@ -51,7 +51,7 @@ use crate::settings::{
|
|||
FileResolverSettings, FormatterSettings, LineEnding, Settings, EXCLUDE, INCLUDE,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct RuleSelection {
|
||||
pub select: Option<Vec<RuleSelector>>,
|
||||
pub ignore: Vec<RuleSelector>,
|
||||
|
@ -106,7 +106,7 @@ impl RuleSelection {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Configuration {
|
||||
// Global options
|
||||
pub cache_dir: Option<PathBuf>,
|
||||
|
@ -397,7 +397,13 @@ impl Configuration {
|
|||
}
|
||||
|
||||
/// Convert the [`Options`] read from the given [`Path`] into a [`Configuration`].
|
||||
pub fn from_options(options: Options, path: &Path, project_root: &Path) -> Result<Self> {
|
||||
/// If `None` is supplied for `path`, it indicates that the `Options` instance
|
||||
/// was created via "inline TOML" from the `--config` flag
|
||||
pub fn from_options(
|
||||
options: Options,
|
||||
path: Option<&Path>,
|
||||
project_root: &Path,
|
||||
) -> Result<Self> {
|
||||
warn_about_deprecated_top_level_lint_options(&options.lint_top_level.0, path);
|
||||
|
||||
let lint = if let Some(mut lint) = options.lint {
|
||||
|
@ -578,7 +584,7 @@ impl Configuration {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct LintConfiguration {
|
||||
pub exclude: Option<Vec<FilePattern>>,
|
||||
pub preview: Option<PreviewMode>,
|
||||
|
@ -1155,7 +1161,7 @@ impl LintConfiguration {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct FormatConfiguration {
|
||||
pub exclude: Option<Vec<FilePattern>>,
|
||||
pub preview: Option<PreviewMode>,
|
||||
|
@ -1263,7 +1269,7 @@ pub fn resolve_src(src: &[String], project_root: &Path) -> Result<Vec<PathBuf>>
|
|||
|
||||
fn warn_about_deprecated_top_level_lint_options(
|
||||
top_level_options: &LintCommonOptions,
|
||||
path: &Path,
|
||||
path: Option<&Path>,
|
||||
) {
|
||||
let mut used_options = Vec::new();
|
||||
|
||||
|
@ -1454,9 +1460,14 @@ fn warn_about_deprecated_top_level_lint_options(
|
|||
.map(|option| format!("- '{option}' -> 'lint.{option}'"))
|
||||
.join("\n ");
|
||||
|
||||
let thing_to_update = path.map_or_else(
|
||||
|| String::from("your `--config` CLI arguments"),
|
||||
|path| format!("`{}`", fs::relativize_path(path)),
|
||||
);
|
||||
|
||||
warn_user_once_by_message!(
|
||||
"The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `{}`:\n {options_mapping}",
|
||||
fs::relativize_path(path),
|
||||
"The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. \
|
||||
Please update the following options in {thing_to_update}:\n {options_mapping}",
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue