Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make --config and --isolated global flags #10150

Merged
merged 19 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 75 additions & 68 deletions crates/ruff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ use ruff_workspace::configuration::{Configuration, RuleSelection};
use ruff_workspace::options::{Options, PycodestyleOptions};
use ruff_workspace::resolver::ConfigurationTransformer;

#[derive(Debug, Default, Clone)]
pub struct GlobalConfigArgs {
pub log_level: LogLevel,
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
pub config_flags: Vec<SingleConfigArgument>,
pub isolated: bool,
}

#[derive(Debug, Parser)]
#[command(
author,
Expand All @@ -38,9 +45,54 @@ use ruff_workspace::resolver::ConfigurationTransformer;
#[command(version)]
pub struct Args {
#[command(subcommand)]
pub command: Command,
command: Command,
#[clap(flatten)]
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
pub log_level_args: LogLevelArgs,
log_level_args: LogLevelArgs,
/// Either a path to a TOML configuration file (`pyproject.toml` or `ruff.toml`),
/// or a TOML `<KEY> = <VALUE>` pair
/// (such as you might find in a `ruff.toml` configuration file)
/// overriding a specific configuration option.
/// Overrides of individual settings using this option always take precedence
/// over all configuration files, including configuration files that were also
/// specified using `--config`.
#[arg(
long,
action = clap::ArgAction::Append,
value_name = "CONFIG_OPTION",
value_parser = ConfigArgumentParser,
global = true,
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
help_heading = "Global configuration options",
)]
config: Vec<SingleConfigArgument>,
/// Ignore all configuration files.
//
// Note: We can't mark this as conflicting with `--config` here
// as `--config` can be used for specifying configuration overrides
// as well as configuration files.
// Specifying a configuration file conflicts with `--isolated`;
// specifying a configuration override does not.
// If a user specifies `ruff check --isolated --config=ruff.toml`,
// we emit an error later on, after the initial parsing by clap.
#[arg(long, help_heading = "Global configuration options", global = true)]
isolated: bool,
}

impl Args {
#[must_use]
pub fn partition(self) -> (Command, GlobalConfigArgs) {
let Args {
command,
log_level_args,
config,
isolated,
} = self;
let global_config_args = GlobalConfigArgs {
log_level: LogLevel::from(&log_level_args),
config_flags: config,
isolated,
};
(command, global_config_args)
}
}

#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -161,20 +213,6 @@ pub struct CheckCommand {
preview: bool,
#[clap(long, overrides_with("preview"), hide = true)]
no_preview: bool,
/// Either a path to a TOML configuration file (`pyproject.toml` or `ruff.toml`),
/// or a TOML `<KEY> = <VALUE>` pair
/// (such as you might find in a `ruff.toml` configuration file)
/// overriding a specific configuration option.
/// Overrides of individual settings using this option always take precedence
/// over all configuration files, including configuration files that were also
/// specified using `--config`.
#[arg(
long,
action = clap::ArgAction::Append,
value_name = "CONFIG_OPTION",
value_parser = ConfigArgumentParser,
)]
pub config: Vec<SingleConfigArgument>,
/// Comma-separated list of rule codes to enable (or ALL, to enable all rules).
#[arg(
long,
Expand Down Expand Up @@ -306,17 +344,6 @@ pub struct CheckCommand {
/// Disable cache reads.
#[arg(short, long, env = "RUFF_NO_CACHE", help_heading = "Miscellaneous")]
pub no_cache: bool,
/// Ignore all configuration files.
//
// Note: We can't mark this as conflicting with `--config` here
// as `--config` can be used for specifying configuration overrides
// as well as configuration files.
// Specifying a configuration file conflicts with `--isolated`;
// specifying a configuration override does not.
// If a user specifies `ruff check --isolated --config=ruff.toml`,
// we emit an error later on, after the initial parsing by clap.
#[arg(long, help_heading = "Miscellaneous")]
pub isolated: bool,
/// Path to the cache directory.
#[arg(long, env = "RUFF_CACHE_DIR", help_heading = "Miscellaneous")]
pub cache_dir: Option<PathBuf>,
Expand Down Expand Up @@ -408,20 +435,6 @@ pub struct FormatCommand {
/// difference between the current file and how the formatted file would look like.
#[arg(long)]
pub diff: bool,
/// Either a path to a TOML configuration file (`pyproject.toml` or `ruff.toml`),
/// or a TOML `<KEY> = <VALUE>` pair
/// (such as you might find in a `ruff.toml` configuration file)
/// overriding a specific configuration option.
/// Overrides of individual settings using this option always take precedence
/// over all configuration files, including configuration files that were also
/// specified using `--config`.
#[arg(
long,
action = clap::ArgAction::Append,
value_name = "CONFIG_OPTION",
value_parser = ConfigArgumentParser,
)]
pub config: Vec<SingleConfigArgument>,

/// Disable cache reads.
#[arg(short, long, env = "RUFF_NO_CACHE", help_heading = "Miscellaneous")]
Expand Down Expand Up @@ -462,17 +475,6 @@ pub struct FormatCommand {
/// Set the line-length.
#[arg(long, help_heading = "Format configuration")]
pub line_length: Option<LineLength>,
/// Ignore all configuration files.
//
// Note: We can't mark this as conflicting with `--config` here
// as `--config` can be used for specifying configuration overrides
// as well as configuration files.
// Specifying a configuration file conflicts with `--isolated`;
// specifying a configuration override does not.
// If a user specifies `ruff check --isolated --config=ruff.toml`,
// we emit an error later on, after the initial parsing by clap.
#[arg(long, help_heading = "Miscellaneous")]
pub isolated: bool,
/// The name of the file when passing it through stdin.
#[arg(long, help_heading = "Miscellaneous")]
pub stdin_filename: Option<PathBuf>,
Expand Down Expand Up @@ -581,16 +583,15 @@ impl ConfigArguments {
}

fn from_cli_arguments(
config_options: Vec<SingleConfigArgument>,
global_config_flags: GlobalConfigArgs,
per_flag_overrides: ExplicitConfigOverrides,
isolated: bool,
) -> anyhow::Result<Self> {
let mut new = Self {
per_flag_overrides,
..Self::default()
};

for option in config_options {
for option in global_config_flags.config_flags {
match option {
SingleConfigArgument::SettingsOverride(overridden_option) => {
let overridden_option = Arc::try_unwrap(overridden_option)
Expand All @@ -602,7 +603,7 @@ impl ConfigArguments {
)?);
}
SingleConfigArgument::FilePath(path) => {
if isolated {
if global_config_flags.isolated {
bail!(
"\
The argument `--config={}` cannot be used with `--isolated`
Expand Down Expand Up @@ -643,7 +644,10 @@ impl ConfigurationTransformer for ConfigArguments {
impl CheckCommand {
/// Partition the CLI into command-line arguments and configuration
/// overrides.
pub fn partition(self) -> anyhow::Result<(CheckArguments, ConfigArguments)> {
pub fn partition(
self,
global_config_flags: GlobalConfigArgs,
) -> anyhow::Result<(CheckArguments, ConfigArguments, bool, LogLevel)> {
let check_arguments = CheckArguments {
add_noqa: self.add_noqa,
diff: self.diff,
Expand All @@ -652,7 +656,6 @@ impl CheckCommand {
exit_zero: self.exit_zero,
files: self.files,
ignore_noqa: self.ignore_noqa,
isolated: self.isolated,
no_cache: self.no_cache,
output_file: self.output_file,
show_files: self.show_files,
Expand Down Expand Up @@ -696,21 +699,25 @@ impl CheckCommand {
extension: self.extension,
};

let config_args =
ConfigArguments::from_cli_arguments(self.config, cli_overrides, self.isolated)?;
Ok((check_arguments, config_args))
let isolated = global_config_flags.isolated;
let log_level = global_config_flags.log_level;
let config_args = ConfigArguments::from_cli_arguments(global_config_flags, cli_overrides)?;

Ok((check_arguments, config_args, isolated, log_level))
}
}

impl FormatCommand {
/// Partition the CLI into command-line arguments and configuration
/// overrides.
pub fn partition(self) -> anyhow::Result<(FormatArguments, ConfigArguments)> {
pub fn partition(
self,
global_config_flags: GlobalConfigArgs,
) -> anyhow::Result<(FormatArguments, ConfigArguments, bool, LogLevel)> {
let format_arguments = FormatArguments {
check: self.check,
diff: self.diff,
files: self.files,
isolated: self.isolated,
no_cache: self.no_cache,
stdin_filename: self.stdin_filename,
range: self.range,
Expand All @@ -730,9 +737,11 @@ impl FormatCommand {
..ExplicitConfigOverrides::default()
};

let config_args =
ConfigArguments::from_cli_arguments(self.config, cli_overrides, self.isolated)?;
Ok((format_arguments, config_args))
let isolated = global_config_flags.isolated;
let log_level = global_config_flags.log_level;
let config_args = ConfigArguments::from_cli_arguments(global_config_flags, cli_overrides)?;

Ok((format_arguments, config_args, isolated, log_level))
}
}

Expand Down Expand Up @@ -957,7 +966,6 @@ pub struct CheckArguments {
pub exit_zero: bool,
pub files: Vec<PathBuf>,
pub ignore_noqa: bool,
pub isolated: bool,
pub no_cache: bool,
pub output_file: Option<PathBuf>,
pub show_files: bool,
Expand All @@ -975,7 +983,6 @@ pub struct FormatArguments {
pub no_cache: bool,
pub diff: bool,
pub files: Vec<PathBuf>,
pub isolated: bool,
pub stdin_filename: Option<PathBuf>,
pub range: Option<FormatRange>,
}
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use colored::Colorize;
use path_absolutize::path_dedot;
use walkdir::WalkDir;

use crate::args::GlobalConfigArgs;
use ruff_cache::CACHE_DIR_NAME;
use ruff_linter::fs;
use ruff_linter::logging::LogLevel;

/// Clear any caches in the current directory or any subdirectories.
pub(crate) fn clean(level: LogLevel) -> Result<()> {
pub(crate) fn clean(config: &GlobalConfigArgs) -> Result<()> {
let mut stderr = BufWriter::new(io::stderr().lock());
for entry in WalkDir::new(&*path_dedot::CWD)
.into_iter()
Expand All @@ -20,7 +21,7 @@ pub(crate) fn clean(level: LogLevel) -> Result<()> {
{
let cache = entry.path().join(CACHE_DIR_NAME);
if cache.is_dir() {
if level >= LogLevel::Default {
if config.log_level >= LogLevel::Default {
writeln!(
stderr,
"Removing cache at: {}",
Expand Down
7 changes: 2 additions & 5 deletions crates/ruff/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@ pub(crate) fn format(
cli: FormatArguments,
config_arguments: &ConfigArguments,
log_level: LogLevel,
isolated: bool,
) -> Result<ExitStatus> {
let pyproject_config = resolve(
cli.isolated,
config_arguments,
cli.stdin_filename.as_deref(),
)?;
let pyproject_config = resolve(isolated, config_arguments, cli.stdin_filename.as_deref())?;
let mode = FormatMode::from_cli(&cli);
let files = resolve_default_files(cli.files, false);
let (paths, resolver) = python_files_in_path(&files, &pyproject_config, config_arguments)?;
Expand Down
7 changes: 2 additions & 5 deletions crates/ruff/src/commands/format_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ use crate::ExitStatus;
pub(crate) fn format_stdin(
cli: &FormatArguments,
config_arguments: &ConfigArguments,
isolated: bool,
) -> Result<ExitStatus> {
let pyproject_config = resolve(
cli.isolated,
config_arguments,
cli.stdin_filename.as_deref(),
)?;
let pyproject_config = resolve(isolated, config_arguments, cli.stdin_filename.as_deref())?;

let mut resolver = Resolver::new(&pyproject_config);
warn_incompatible_formatter_settings(&resolver);
Expand Down
Loading
Loading