From 41d9c6e209c41e2a5840cbf51f37d0c3a8bcd80a Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Mon, 24 Jun 2024 11:28:45 +0200 Subject: [PATCH 1/2] Remove `check`, `--explain`, `--clean`, `--generate-shell-completion` aliases` --- crates/ruff/src/args.rs | 3 -- crates/ruff/src/lib.rs | 5 ---- crates/ruff/src/main.rs | 43 +++------------------------ crates/ruff/tests/integration_test.rs | 2 +- docs/configuration.md | 24 +++++++++------ 5 files changed, 20 insertions(+), 57 deletions(-) diff --git a/crates/ruff/src/args.rs b/crates/ruff/src/args.rs index 77399ee2db0b2..e080c5e59b165 100644 --- a/crates/ruff/src/args.rs +++ b/crates/ruff/src/args.rs @@ -95,7 +95,6 @@ pub enum Command { /// Run Ruff on the given files or directories (default). Check(CheckCommand), /// Explain a rule (or all rules). - #[clap(alias = "--explain")] #[command(group = clap::ArgGroup::new("selector").multiple(false).required(true))] Rule { /// Rule to explain @@ -125,10 +124,8 @@ pub enum Command { output_format: HelpFormat, }, /// Clear any caches in the current directory and any subdirectories. - #[clap(alias = "--clean")] Clean, /// Generate shell completion. - #[clap(alias = "--generate-shell-completion", hide = true)] GenerateShellCompletion { shell: clap_complete_command::Shell }, /// Run the Ruff formatter on the given files or directories. Format(FormatCommand), diff --git a/crates/ruff/src/lib.rs b/crates/ruff/src/lib.rs index 7281a3be87266..f15a99aad19b4 100644 --- a/crates/ruff/src/lib.rs +++ b/crates/ruff/src/lib.rs @@ -121,7 +121,6 @@ pub fn run( command, global_options, }: Args, - deprecated_alias_warning: Option<&'static str>, ) -> Result { { let default_panic_hook = std::panic::take_hook(); @@ -158,10 +157,6 @@ pub fn run( set_up_logging(global_options.log_level())?; - if let Some(deprecated_alias_warning) = deprecated_alias_warning { - warn_user!("{}", deprecated_alias_warning); - } - match command { Command::Version { output_format } => { commands::version::version(output_format)?; diff --git a/crates/ruff/src/main.rs b/crates/ruff/src/main.rs index 2f5fe9cfab95b..cbe265a5a1a68 100644 --- a/crates/ruff/src/main.rs +++ b/crates/ruff/src/main.rs @@ -1,9 +1,9 @@ use std::process::ExitCode; -use clap::{Parser, Subcommand}; +use clap::Parser; use colored::Colorize; -use ruff::args::{Args, Command}; +use ruff::args::Args; use ruff::{run, ExitStatus}; #[cfg(target_os = "windows")] @@ -24,45 +24,10 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; pub fn main() -> ExitCode { let args = wild::args_os(); - let mut args = - argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX).unwrap(); - - // We can't use `warn_user` here because logging isn't set up at this point - // and we also don't know if the user runs ruff with quiet. - // Keep the message and pass it to `run` that is responsible for emitting the warning. - let deprecated_alias_warning = match args.get(1).and_then(|arg| arg.to_str()) { - // Deprecated aliases that are handled by clap - Some("--explain") => { - Some("`ruff --explain ` is deprecated. Use `ruff rule ` instead.") - } - Some("--clean") => { - Some("`ruff --clean` is deprecated. Use `ruff clean` instead.") - } - Some("--generate-shell-completion") => { - Some("`ruff --generate-shell-completion ` is deprecated. Use `ruff generate-shell-completion ` instead.") - } - // Deprecated `ruff` alias to `ruff check` - // Clap doesn't support default subcommands but we want to run `check` by - // default for convenience and backwards-compatibility, so we just - // preprocess the arguments accordingly before passing them to Clap. - Some(arg) if !Command::has_subcommand(arg) - && arg != "-h" - && arg != "--help" - && arg != "-V" - && arg != "--version" - && arg != "help" => { - - { - args.insert(1, "check".into()); - Some("`ruff ` is deprecated. Use `ruff check ` instead.") - } - }, - _ => None - }; - + let args = argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX).unwrap(); let args = Args::parse_from(args); - match run(args, deprecated_alias_warning) { + match run(args) { Ok(code) => code.into(), Err(err) => { #[allow(clippy::print_stderr)] diff --git a/crates/ruff/tests/integration_test.rs b/crates/ruff/tests/integration_test.rs index dc096f47181f0..88a1a34b8d52e 100644 --- a/crates/ruff/tests/integration_test.rs +++ b/crates/ruff/tests/integration_test.rs @@ -1377,7 +1377,7 @@ fn unreadable_pyproject_toml() -> Result<()> { // Don't `--isolated` since the configuration discovery is where the error happens let args = Args::parse_from(["", "check", "--no-cache", tempdir.path().to_str().unwrap()]); - let err = run(args, None).err().context("Unexpected success")?; + let err = run(args).err().context("Unexpected success")?; assert_eq!( err.chain() .map(std::string::ToString::to_string) diff --git a/docs/configuration.md b/docs/configuration.md index 949e22f8801fd..260620b589103 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -521,15 +521,21 @@ Ruff: An extremely fast Python linter. Usage: ruff [OPTIONS] Commands: - check Run Ruff on the given files or directories (default) - rule Explain a rule (or all rules) - config List or describe the available configuration options - linter List all supported upstream linters - clean Clear any caches in the current directory and any subdirectories - format Run the Ruff formatter on the given files or directories - server Run the language server - version Display Ruff's version - help Print this message or the help of the given subcommand(s) + check Run Ruff on the given files or directories + (default) + rule Explain a rule (or all rules) + config List or describe the available configuration + options + linter List all supported upstream linters + clean Clear any caches in the current directory and + any subdirectories + generate-shell-completion Generate shell completion + format Run the Ruff formatter on the given files or + directories + server Run the language server + version Display Ruff's version + help Print this message or the help of the given + subcommand(s) Options: -h, --help Print help From ac2cc5bfc3eae8094092b6e7aba2815796b31e8f Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 25 Jun 2024 08:50:14 +0200 Subject: [PATCH 2/2] Add error message instead of simply removing the command/options --- crates/ruff/src/args.rs | 1 + crates/ruff/src/lib.rs | 13 +----- crates/ruff/src/main.rs | 59 ++++++++++++++++++++++++++- crates/ruff/tests/integration_test.rs | 1 + docs/configuration.md | 24 ++++------- 5 files changed, 69 insertions(+), 29 deletions(-) diff --git a/crates/ruff/src/args.rs b/crates/ruff/src/args.rs index e080c5e59b165..0340463674476 100644 --- a/crates/ruff/src/args.rs +++ b/crates/ruff/src/args.rs @@ -126,6 +126,7 @@ pub enum Command { /// Clear any caches in the current directory and any subdirectories. Clean, /// Generate shell completion. + #[clap(hide = true)] GenerateShellCompletion { shell: clap_complete_command::Shell }, /// Run the Ruff formatter on the given files or directories. Format(FormatCommand), diff --git a/crates/ruff/src/lib.rs b/crates/ruff/src/lib.rs index f15a99aad19b4..1bbb8105f8273 100644 --- a/crates/ruff/src/lib.rs +++ b/crates/ruff/src/lib.rs @@ -8,12 +8,12 @@ use std::process::ExitCode; use std::sync::mpsc::channel; use anyhow::Result; -use args::{GlobalConfigArgs, ServerCommand}; use clap::CommandFactory; use colored::Colorize; use log::warn; use notify::{recommended_watcher, RecursiveMode, Watcher}; +use args::{GlobalConfigArgs, ServerCommand}; use ruff_linter::logging::{set_up_logging, LogLevel}; use ruff_linter::settings::flags::FixMode; use ruff_linter::settings::types::SerializationFormat; @@ -144,17 +144,6 @@ pub fn run( })); } - // Enabled ANSI colors on Windows 10. - #[cfg(windows)] - assert!(colored::control::set_virtual_terminal(true).is_ok()); - - // support FORCE_COLOR env var - if let Some(force_color) = std::env::var_os("FORCE_COLOR") { - if force_color.len() > 0 { - colored::control::set_override(true); - } - } - set_up_logging(global_options.log_level())?; match command { diff --git a/crates/ruff/src/main.rs b/crates/ruff/src/main.rs index cbe265a5a1a68..94becf6841812 100644 --- a/crates/ruff/src/main.rs +++ b/crates/ruff/src/main.rs @@ -1,10 +1,12 @@ use std::process::ExitCode; -use clap::Parser; +use clap::{Parser, Subcommand}; use colored::Colorize; +use log::error; -use ruff::args::Args; +use ruff::args::{Args, Command}; use ruff::{run, ExitStatus}; +use ruff_linter::logging::{set_up_logging, LogLevel}; #[cfg(target_os = "windows")] #[global_allocator] @@ -23,8 +25,61 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; pub fn main() -> ExitCode { + // Enabled ANSI colors on Windows 10. + #[cfg(windows)] + assert!(colored::control::set_virtual_terminal(true).is_ok()); + + // support FORCE_COLOR env var + if let Some(force_color) = std::env::var_os("FORCE_COLOR") { + if force_color.len() > 0 { + colored::control::set_override(true); + } + } + let args = wild::args_os(); let args = argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX).unwrap(); + + // We can't use `warn_user` here because logging isn't set up at this point + // and we also don't know if the user runs ruff with quiet. + // Keep the message and pass it to `run` that is responsible for emitting the warning. + let deprecated_alias_error = match args.get(1).and_then(|arg| arg.to_str()) { + // Deprecated aliases that are handled by clap + Some("--explain") => { + Some("`ruff --explain ` has been removed. Use `ruff rule ` instead.") + } + Some("--clean") => { + Some("`ruff --clean` has been removed. Use `ruff clean` instead.") + } + Some("--generate-shell-completion") => { + Some("`ruff --generate-shell-completion ` has been removed. Use `ruff generate-shell-completion ` instead.") + } + // Deprecated `ruff` alias to `ruff check` + // Clap doesn't support default subcommands but we want to run `check` by + // default for convenience and backwards-compatibility, so we just + // preprocess the arguments accordingly before passing them to Clap. + Some(arg) if !Command::has_subcommand(arg) + && arg != "-h" + && arg != "--help" + && arg != "-V" + && arg != "--version" + && arg != "help" => { + { + Some("`ruff ` has been removed. Use `ruff check ` instead.") + } + }, + _ => None + }; + + if let Some(error) = deprecated_alias_error { + #[allow(clippy::print_stderr)] + if set_up_logging(LogLevel::Default).is_ok() { + error!("{}", error); + } else { + eprintln!("{}", error.red().bold()); + } + return ExitCode::FAILURE; + } + let args = Args::parse_from(args); match run(args) { diff --git a/crates/ruff/tests/integration_test.rs b/crates/ruff/tests/integration_test.rs index 88a1a34b8d52e..c38b7447c795b 100644 --- a/crates/ruff/tests/integration_test.rs +++ b/crates/ruff/tests/integration_test.rs @@ -1378,6 +1378,7 @@ fn unreadable_pyproject_toml() -> Result<()> { // Don't `--isolated` since the configuration discovery is where the error happens let args = Args::parse_from(["", "check", "--no-cache", tempdir.path().to_str().unwrap()]); let err = run(args).err().context("Unexpected success")?; + assert_eq!( err.chain() .map(std::string::ToString::to_string) diff --git a/docs/configuration.md b/docs/configuration.md index 260620b589103..949e22f8801fd 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -521,21 +521,15 @@ Ruff: An extremely fast Python linter. Usage: ruff [OPTIONS] Commands: - check Run Ruff on the given files or directories - (default) - rule Explain a rule (or all rules) - config List or describe the available configuration - options - linter List all supported upstream linters - clean Clear any caches in the current directory and - any subdirectories - generate-shell-completion Generate shell completion - format Run the Ruff formatter on the given files or - directories - server Run the language server - version Display Ruff's version - help Print this message or the help of the given - subcommand(s) + check Run Ruff on the given files or directories (default) + rule Explain a rule (or all rules) + config List or describe the available configuration options + linter List all supported upstream linters + clean Clear any caches in the current directory and any subdirectories + format Run the Ruff formatter on the given files or directories + server Run the language server + version Display Ruff's version + help Print this message or the help of the given subcommand(s) Options: -h, --help Print help