diff --git a/crates/ruff/src/args.rs b/crates/ruff/src/args.rs index 77399ee2db0b2..0340463674476 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,9 @@ 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)] + #[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 7281a3be87266..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; @@ -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(); @@ -145,23 +144,8 @@ 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())?; - 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..94becf6841812 100644 --- a/crates/ruff/src/main.rs +++ b/crates/ruff/src/main.rs @@ -2,9 +2,11 @@ use std::process::ExitCode; use clap::{Parser, Subcommand}; use colored::Colorize; +use log::error; use ruff::args::{Args, Command}; use ruff::{run, ExitStatus}; +use ruff_linter::logging::{set_up_logging, LogLevel}; #[cfg(target_os = "windows")] #[global_allocator] @@ -23,23 +25,33 @@ 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 mut args = - argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX).unwrap(); + 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_warning = match args.get(1).and_then(|arg| arg.to_str()) { + 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 ` is deprecated. Use `ruff rule ` instead.") + Some("`ruff --explain ` has been removed. Use `ruff rule ` instead.") } Some("--clean") => { - Some("`ruff --clean` is deprecated. Use `ruff clean` instead.") + Some("`ruff --clean` has been removed. Use `ruff clean` instead.") } Some("--generate-shell-completion") => { - Some("`ruff --generate-shell-completion ` is deprecated. Use `ruff generate-shell-completion ` instead.") + 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 @@ -51,18 +63,26 @@ pub fn main() -> ExitCode { && arg != "-V" && arg != "--version" && arg != "help" => { - { - args.insert(1, "check".into()); - Some("`ruff ` is deprecated. Use `ruff check ` instead.") + 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, 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..c38b7447c795b 100644 --- a/crates/ruff/tests/integration_test.rs +++ b/crates/ruff/tests/integration_test.rs @@ -1377,7 +1377,8 @@ 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)