From 57576fa139f6ce51ef6a60b67ba26fb9869dfcee Mon Sep 17 00:00:00 2001 From: Jane Lewis Date: Mon, 29 Jan 2024 16:57:56 -0800 Subject: [PATCH] Replace `--show-source` and `--no-show-source` with `--output_format=` (#9687) Fixes #7350 ## Summary * `--show-source` and `--no-show-source` are now deprecated. * `output-format` supports two new variants, `full` and `concise`. `text` is now a deprecated variant, and any use of it is treated as the default serialization format. * `--output-format` now default to `concise` * In preview mode, `--output-format` defaults to `full` * `--show-source` will still set `--output-format` to `full` if the output format is not otherwise specified. * likewise, `--no-show-source` can override an output format that was set in a file-based configuration, though it will also be overridden by `--output-format` ## Test Plan A lot of tests were updated to use `--output-format=full`. Additional tests were added to ensure the correct deprecation warnings appeared, and that deprecated options behaved as intended. --- crates/ruff/src/args.rs | 53 ++++++- crates/ruff/src/lib.rs | 17 +- crates/ruff/src/printer.rs | 21 ++- crates/ruff/tests/deprecation.rs | 149 ++++++++++++++++++ crates/ruff/tests/integration_test.rs | 85 ++++++++-- crates/ruff/tests/lint.rs | 2 +- ...ow_settings__display_default_settings.snap | 3 +- crates/ruff_linter/src/settings/types.rs | 14 +- crates/ruff_workspace/src/configuration.rs | 36 ++++- crates/ruff_workspace/src/options.rs | 11 +- crates/ruff_workspace/src/settings.rs | 6 +- docs/configuration.md | 12 +- ruff.schema.json | 5 +- 13 files changed, 348 insertions(+), 66 deletions(-) create mode 100644 crates/ruff/tests/deprecation.rs diff --git a/crates/ruff/src/args.rs b/crates/ruff/src/args.rs index 71890a0342c55..5742ed4fc176b 100644 --- a/crates/ruff/src/args.rs +++ b/crates/ruff/src/args.rs @@ -11,7 +11,7 @@ use ruff_linter::settings::types::{ ExtensionPair, FilePattern, PatternPrefixPair, PerFileIgnore, PreviewMode, PythonVersion, SerializationFormat, UnsafeFixes, }; -use ruff_linter::{RuleParser, RuleSelector, RuleSelectorParser}; +use ruff_linter::{warn_user, RuleParser, RuleSelector, RuleSelectorParser}; use ruff_workspace::configuration::{Configuration, RuleSelection}; use ruff_workspace::options::PycodestyleOptions; use ruff_workspace::resolver::ConfigurationTransformer; @@ -104,6 +104,7 @@ pub struct CheckCommand { no_unsafe_fixes: bool, /// Show violations with source code. /// Use `--no-show-source` to disable. + /// (Deprecated: use `--output-format=full` or `--output-format=concise` instead of `--show-source` and `--no-show-source`, respectively) #[arg(long, overrides_with("no_show_source"))] show_source: bool, #[clap(long, overrides_with("show_source"), hide = true)] @@ -131,6 +132,8 @@ pub struct CheckCommand { ignore_noqa: bool, /// Output serialization format for violations. + /// The default serialization format is "concise". + /// In preview mode, the default serialization format is "full". #[arg(long, value_enum, env = "RUFF_OUTPUT_FORMAT")] pub output_format: Option, @@ -533,7 +536,6 @@ impl CheckCommand { self.no_respect_gitignore, ), select: self.select, - show_source: resolve_bool_arg(self.show_source, self.no_show_source), target_version: self.target_version, unfixable: self.unfixable, // TODO(charlie): Included in `pyproject.toml`, but not inherited. @@ -543,7 +545,11 @@ impl CheckCommand { unsafe_fixes: resolve_bool_arg(self.unsafe_fixes, self.no_unsafe_fixes) .map(UnsafeFixes::from), force_exclude: resolve_bool_arg(self.force_exclude, self.no_force_exclude), - output_format: self.output_format, + output_format: resolve_output_format( + self.output_format, + resolve_bool_arg(self.show_source, self.no_show_source), + resolve_bool_arg(self.preview, self.no_preview).unwrap_or_default(), + ), show_fixes: resolve_bool_arg(self.show_fixes, self.no_show_fixes), extension: self.extension, }, @@ -594,6 +600,43 @@ fn resolve_bool_arg(yes: bool, no: bool) -> Option { } } +fn resolve_output_format( + output_format: Option, + show_sources: Option, + preview: bool, +) -> Option { + Some(match (output_format, show_sources) { + (Some(o), None) => o, + (Some(SerializationFormat::Grouped), Some(true)) => { + warn_user!("`--show-source` with `--output-format=grouped` is deprecated, and will not show source files. Use `--output-format=full` to show source information."); + SerializationFormat::Grouped + } + (Some(fmt), Some(true)) => { + warn_user!("The `--show-source` argument is deprecated and has been ignored in favor of `--output-format={fmt}`."); + fmt + } + (Some(fmt), Some(false)) => { + warn_user!("The `--no-show-source` argument is deprecated and has been ignored in favor of `--output-format={fmt}`."); + fmt + } + (None, Some(true)) => { + warn_user!("The `--show-source` argument is deprecated. Use `--output-format=full` instead."); + SerializationFormat::Full + } + (None, Some(false)) => { + warn_user!("The `--no-show-source` argument is deprecated. Use `--output-format=concise` instead."); + SerializationFormat::Concise + } + (None, None) => return None + }).map(|format| match format { + SerializationFormat::Text => { + warn_user!("`--output-format=text` is deprecated. Use `--output-format=full` or `--output-format=concise` instead. `text` will be treated as `{}`.", SerializationFormat::default(preview)); + SerializationFormat::default(preview) + }, + other => other + }) +} + /// CLI settings that are distinct from configuration (commands, lists of files, /// etc.). #[allow(clippy::struct_excessive_bools)] @@ -648,7 +691,6 @@ pub struct CliOverrides { pub preview: Option, pub respect_gitignore: Option, pub select: Option>, - pub show_source: Option, pub target_version: Option, pub unfixable: Option>, // TODO(charlie): Captured in pyproject.toml as a default, but not part of `Settings`. @@ -735,9 +777,6 @@ impl ConfigurationTransformer for CliOverrides { if let Some(respect_gitignore) = &self.respect_gitignore { config.respect_gitignore = Some(*respect_gitignore); } - if let Some(show_source) = &self.show_source { - config.show_source = Some(*show_source); - } if let Some(show_fixes) = &self.show_fixes { config.show_fixes = Some(*show_fixes); } diff --git a/crates/ruff/src/lib.rs b/crates/ruff/src/lib.rs index f8f99505b3c91..38fdcb8c4eea7 100644 --- a/crates/ruff/src/lib.rs +++ b/crates/ruff/src/lib.rs @@ -255,7 +255,6 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result { unsafe_fixes, output_format, show_fixes, - show_source, .. } = pyproject_config.settings; @@ -284,9 +283,6 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result { if show_fixes { printer_flags |= PrinterFlags::SHOW_FIX_SUMMARY; } - if show_source { - printer_flags |= PrinterFlags::SHOW_SOURCE; - } if cli.ecosystem_ci { warn_user!( "The formatting of fixes emitted by this option is a work-in-progress, subject to \ @@ -325,9 +321,14 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result { printer_flags, ); + let preview = overrides.preview.unwrap_or_default().is_enabled(); + if cli.watch { - if output_format != SerializationFormat::Text { - warn_user!("`--output-format text` is always used in watch mode."); + if output_format != SerializationFormat::default(preview) { + warn_user!( + "`--output-format {}` is always used in watch mode.", + SerializationFormat::default(preview) + ); } // Configure the file watcher. @@ -353,7 +354,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result { fix_mode, unsafe_fixes, )?; - printer.write_continuously(&mut writer, &messages)?; + printer.write_continuously(&mut writer, &messages, preview)?; // In watch mode, we may need to re-resolve the configuration. // TODO(charlie): Re-compute other derivative values, like the `printer`. @@ -386,7 +387,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result { fix_mode, unsafe_fixes, )?; - printer.write_continuously(&mut writer, &messages)?; + printer.write_continuously(&mut writer, &messages, preview)?; } Err(err) => return Err(err.into()), } diff --git a/crates/ruff/src/printer.rs b/crates/ruff/src/printer.rs index 5bfdece63beaa..13a0d966dfe3c 100644 --- a/crates/ruff/src/printer.rs +++ b/crates/ruff/src/printer.rs @@ -27,8 +27,6 @@ bitflags! { pub(crate) struct Flags: u8 { /// Whether to show violations when emitting diagnostics. const SHOW_VIOLATIONS = 0b0000_0001; - /// Whether to show the source code when emitting diagnostics. - const SHOW_SOURCE = 0b000_0010; /// Whether to show a summary of the fixed violations when emitting diagnostics. const SHOW_FIX_SUMMARY = 0b0000_0100; /// Whether to show a diff of each fixed violation when emitting diagnostics. @@ -218,7 +216,10 @@ impl Printer { if !self.flags.intersects(Flags::SHOW_VIOLATIONS) { if matches!( self.format, - SerializationFormat::Text | SerializationFormat::Grouped + SerializationFormat::Text + | SerializationFormat::Full + | SerializationFormat::Concise + | SerializationFormat::Grouped ) { if self.flags.intersects(Flags::SHOW_FIX_SUMMARY) { if !diagnostics.fixed.is_empty() { @@ -245,11 +246,12 @@ impl Printer { SerializationFormat::Junit => { JunitEmitter.emit(writer, &diagnostics.messages, &context)?; } - SerializationFormat::Text => { + SerializationFormat::Concise + | SerializationFormat::Full => { TextEmitter::default() .with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref())) .with_show_fix_diff(self.flags.intersects(Flags::SHOW_FIX_DIFF)) - .with_show_source(self.flags.intersects(Flags::SHOW_SOURCE)) + .with_show_source(self.format == SerializationFormat::Full) .with_unsafe_fixes(self.unsafe_fixes) .emit(writer, &diagnostics.messages, &context)?; @@ -265,7 +267,6 @@ impl Printer { } SerializationFormat::Grouped => { GroupedEmitter::default() - .with_show_source(self.flags.intersects(Flags::SHOW_SOURCE)) .with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref())) .with_unsafe_fixes(self.unsafe_fixes) .emit(writer, &diagnostics.messages, &context)?; @@ -294,6 +295,7 @@ impl Printer { SerializationFormat::Sarif => { SarifEmitter.emit(writer, &diagnostics.messages, &context)?; } + SerializationFormat::Text => unreachable!("Text is deprecated and should have been automatically converted to the default serialization format") } writer.flush()?; @@ -342,7 +344,9 @@ impl Printer { } match self.format { - SerializationFormat::Text => { + SerializationFormat::Text + | SerializationFormat::Full + | SerializationFormat::Concise => { // Compute the maximum number of digits in the count and code, for all messages, // to enable pretty-printing. let count_width = num_digits( @@ -403,6 +407,7 @@ impl Printer { &self, writer: &mut dyn Write, diagnostics: &Diagnostics, + preview: bool, ) -> Result<()> { if matches!(self.log_level, LogLevel::Silent) { return Ok(()); @@ -430,7 +435,7 @@ impl Printer { let context = EmitterContext::new(&diagnostics.notebook_indexes); TextEmitter::default() .with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref())) - .with_show_source(self.flags.intersects(Flags::SHOW_SOURCE)) + .with_show_source(preview) .with_unsafe_fixes(self.unsafe_fixes) .emit(writer, &diagnostics.messages, &context)?; } diff --git a/crates/ruff/tests/deprecation.rs b/crates/ruff/tests/deprecation.rs new file mode 100644 index 0000000000000..0d3215d3bdeb3 --- /dev/null +++ b/crates/ruff/tests/deprecation.rs @@ -0,0 +1,149 @@ +//! A test suite that ensures deprecated command line options have appropriate warnings / behaviors + +use ruff_linter::settings::types::SerializationFormat; +use std::process::Command; + +use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; + +const BIN_NAME: &str = "ruff"; + +const STDIN: &str = "l = 1"; + +fn ruff_check(show_source: Option, output_format: Option) -> Command { + let mut cmd = Command::new(get_cargo_bin(BIN_NAME)); + let output_format = output_format.unwrap_or(format!("{}", SerializationFormat::default(false))); + cmd.arg("--output-format"); + cmd.arg(output_format); + cmd.arg("--no-cache"); + match show_source { + Some(true) => { + cmd.arg("--show-source"); + } + Some(false) => { + cmd.arg("--no-show-source"); + } + None => {} + } + cmd.arg("-"); + + cmd +} + +#[test] +fn ensure_show_source_is_deprecated() { + assert_cmd_snapshot!(ruff_check(Some(true), None).pass_stdin(STDIN), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + Found 1 error. + + ----- stderr ----- + warning: The `--show-source` argument is deprecated and has been ignored in favor of `--output-format=concise`. + "###); +} + +#[test] +fn ensure_no_show_source_is_deprecated() { + assert_cmd_snapshot!(ruff_check(Some(false), None).pass_stdin(STDIN), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + Found 1 error. + + ----- stderr ----- + warning: The `--no-show-source` argument is deprecated and has been ignored in favor of `--output-format=concise`. + "###); +} + +#[test] +fn ensure_output_format_is_deprecated() { + assert_cmd_snapshot!(ruff_check(None, Some("text".into())).pass_stdin(STDIN), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + Found 1 error. + + ----- stderr ----- + warning: `--output-format=text` is deprecated. Use `--output-format=full` or `--output-format=concise` instead. `text` will be treated as `concise`. + "###); +} + +#[test] +fn ensure_output_format_overrides_show_source() { + assert_cmd_snapshot!(ruff_check(Some(true), Some("concise".into())).pass_stdin(STDIN), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + Found 1 error. + + ----- stderr ----- + warning: The `--show-source` argument is deprecated and has been ignored in favor of `--output-format=concise`. + "###); +} + +#[test] +fn ensure_full_output_format_overrides_no_show_source() { + assert_cmd_snapshot!(ruff_check(Some(false), Some("full".into())).pass_stdin(STDIN), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + | + 1 | l = 1 + | ^ E741 + | + + Found 1 error. + + ----- stderr ----- + warning: The `--no-show-source` argument is deprecated and has been ignored in favor of `--output-format=full`. + "###); +} + +#[test] +fn ensure_output_format_uses_concise_over_no_show_source() { + assert_cmd_snapshot!(ruff_check(Some(false), Some("concise".into())).pass_stdin(STDIN), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + Found 1 error. + + ----- stderr ----- + warning: The `--no-show-source` argument is deprecated and has been ignored in favor of `--output-format=concise`. + "###); +} + +#[test] +fn ensure_deprecated_output_format_overrides_show_source() { + assert_cmd_snapshot!(ruff_check(Some(true), Some("text".into())).pass_stdin(STDIN), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + Found 1 error. + + ----- stderr ----- + warning: The `--show-source` argument is deprecated and has been ignored in favor of `--output-format=text`. + warning: `--output-format=text` is deprecated. Use `--output-format=full` or `--output-format=concise` instead. `text` will be treated as `concise`. + "###); +} + +#[test] +fn ensure_deprecated_output_format_overrides_no_show_source() { + assert_cmd_snapshot!(ruff_check(Some(false), Some("text".into())).pass_stdin(STDIN), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + Found 1 error. + + ----- stderr ----- + warning: The `--no-show-source` argument is deprecated and has been ignored in favor of `--output-format=text`. + warning: `--output-format=text` is deprecated. Use `--output-format=full` or `--output-format=concise` instead. `text` will be treated as `concise`. + "###); +} diff --git a/crates/ruff/tests/integration_test.rs b/crates/ruff/tests/integration_test.rs index 7aed76fee2df3..9b44347374dcf 100644 --- a/crates/ruff/tests/integration_test.rs +++ b/crates/ruff/tests/integration_test.rs @@ -31,25 +31,14 @@ fn ruff_cmd() -> Command { } /// Builder for `ruff check` commands. -#[derive(Debug)] +#[derive(Debug, Default)] struct RuffCheck<'a> { - output_format: &'a str, + output_format: Option<&'a str>, config: Option<&'a Path>, filename: Option<&'a str>, args: Vec<&'a str>, } -impl<'a> Default for RuffCheck<'a> { - fn default() -> RuffCheck<'a> { - RuffCheck { - output_format: "text", - config: None, - filename: None, - args: vec![], - } - } -} - impl<'a> RuffCheck<'a> { /// Set the `--config` option. #[must_use] @@ -61,7 +50,7 @@ impl<'a> RuffCheck<'a> { /// Set the `--output-format` option. #[must_use] fn output_format(mut self, format: &'a str) -> Self { - self.output_format = format; + self.output_format = Some(format); self } @@ -82,7 +71,11 @@ impl<'a> RuffCheck<'a> { /// Generate a [`Command`] for the `ruff check` command. fn build(self) -> Command { let mut cmd = ruff_cmd(); - cmd.args(["--output-format", self.output_format, "--no-cache"]); + if let Some(output_format) = self.output_format { + cmd.args(["--output-format", output_format]); + } + cmd.arg("--no-cache"); + if let Some(path) = self.config { cmd.arg("--config"); cmd.arg(path); @@ -743,8 +736,28 @@ fn stdin_parse_error() { } #[test] -fn show_source() { - let mut cmd = RuffCheck::default().args(["--show-source"]).build(); +fn full_output_preview() { + let mut cmd = RuffCheck::default().args(["--preview"]).build(); + assert_cmd_snapshot!(cmd + .pass_stdin("l = 1"), @r###" + success: false + exit_code: 1 + ----- stdout ----- + -:1:1: E741 Ambiguous variable name: `l` + | + 1 | l = 1 + | ^ E741 + | + + Found 1 error. + + ----- stderr ----- + "###); +} + +#[test] +fn full_output_format() { + let mut cmd = RuffCheck::default().output_format("full").build(); assert_cmd_snapshot!(cmd .pass_stdin("l = 1"), @r###" success: false @@ -899,7 +912,18 @@ fn preview_enabled_prefix() { exit_code: 1 ----- stdout ----- -:1:1: E741 Ambiguous variable name: `I` + | + 1 | I=42 + | ^ E741 + | + -:1:2: E225 [*] Missing whitespace around operator + | + 1 | I=42 + | ^ E225 + | + = help: Add missing whitespace + Found 2 errors. [*] 1 fixable with the `--fix` option. @@ -918,9 +942,30 @@ fn preview_enabled_all() { exit_code: 1 ----- stdout ----- -:1:1: E741 Ambiguous variable name: `I` + | + 1 | I=42 + | ^ E741 + | + -:1:1: D100 Missing docstring in public module + | + 1 | I=42 + | D100 + | + -:1:1: CPY001 Missing copyright notice at top of file + | + 1 | I=42 + | CPY001 + | + -:1:2: E225 [*] Missing whitespace around operator + | + 1 | I=42 + | ^ E225 + | + = help: Add missing whitespace + Found 4 errors. [*] 1 fixable with the `--fix` option. @@ -942,6 +987,12 @@ fn preview_enabled_direct() { exit_code: 1 ----- stdout ----- -:1:2: E225 [*] Missing whitespace around operator + | + 1 | I=42 + | ^ E225 + | + = help: Add missing whitespace + Found 1 error. [*] 1 fixable with the `--fix` option. diff --git a/crates/ruff/tests/lint.rs b/crates/ruff/tests/lint.rs index 8c37469dc0686..7cc55eab8ccd9 100644 --- a/crates/ruff/tests/lint.rs +++ b/crates/ruff/tests/lint.rs @@ -11,7 +11,7 @@ use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; use tempfile::TempDir; const BIN_NAME: &str = "ruff"; -const STDIN_BASE_OPTIONS: &[&str] = &["--no-cache", "--output-format", "text"]; +const STDIN_BASE_OPTIONS: &[&str] = &["--no-cache", "--output-format", "concise"]; #[test] fn top_level_options() -> Result<()> { diff --git a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap index ae92e615f048f..37b5699286474 100644 --- a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap +++ b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap @@ -17,9 +17,8 @@ Settings path: "[BASEPATH]/pyproject.toml" cache_dir = "[BASEPATH]/.ruff_cache" fix = false fix_only = false -output_format = text +output_format = concise show_fixes = false -show_source = false unsafe_fixes = hint # File Resolver Settings diff --git a/crates/ruff_linter/src/settings/types.rs b/crates/ruff_linter/src/settings/types.rs index 18da863b692b6..d2106e351eec3 100644 --- a/crates/ruff_linter/src/settings/types.rs +++ b/crates/ruff_linter/src/settings/types.rs @@ -492,6 +492,8 @@ impl FromIterator for ExtensionMapping { #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub enum SerializationFormat { Text, + Concise, + Full, Json, JsonLines, Junit, @@ -507,6 +509,8 @@ impl Display for SerializationFormat { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Self::Text => write!(f, "text"), + Self::Concise => write!(f, "concise"), + Self::Full => write!(f, "full"), Self::Json => write!(f, "json"), Self::JsonLines => write!(f, "json_lines"), Self::Junit => write!(f, "junit"), @@ -520,9 +524,13 @@ impl Display for SerializationFormat { } } -impl Default for SerializationFormat { - fn default() -> Self { - Self::Text +impl SerializationFormat { + pub fn default(preview: bool) -> Self { + if preview { + Self::Full + } else { + Self::Concise + } } } diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 3025d50acd9f1..e49508bae17f2 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -118,7 +118,6 @@ pub struct Configuration { pub required_version: Option, pub extension: Option, pub show_fixes: Option, - pub show_source: Option, // File resolver options pub exclude: Option>, @@ -221,9 +220,10 @@ impl Configuration { fix: self.fix.unwrap_or(false), fix_only: self.fix_only.unwrap_or(false), unsafe_fixes: self.unsafe_fixes.unwrap_or_default(), - output_format: self.output_format.unwrap_or_default(), + output_format: self + .output_format + .unwrap_or_else(|| SerializationFormat::default(global_preview.is_enabled())), show_fixes: self.show_fixes.unwrap_or(false), - show_source: self.show_source.unwrap_or(false), file_resolver: FileResolverSettings { exclude: FilePatternSet::try_from_iter( @@ -417,6 +417,32 @@ impl Configuration { options.indent_width.or(options.tab_size) }; + #[allow(deprecated)] + let output_format = { + if options.show_source.is_some() { + warn_user_once!( + r#"The `show-source` option has been deprecated in favor of `output-format`'s "full" and "concise" variants. Please update your configuration to use `output-format = ` instead."# + ); + } + + options + .output_format + .map(|format| match format { + SerializationFormat::Text => { + warn_user!(r#"Setting `output_format` to "text" is deprecated. Use "full" or "concise" instead. "text" will be treated as "{}"."#, SerializationFormat::default(options.preview.unwrap_or_default())); + SerializationFormat::default(options.preview.unwrap_or_default()) + }, + other => other + }) + .or(options.show_source.map(|show_source| { + if show_source { + SerializationFormat::Full + } else { + SerializationFormat::Concise + } + })) + }; + Ok(Self { builtins: options.builtins, cache_dir: options @@ -481,7 +507,7 @@ impl Configuration { fix: options.fix, fix_only: options.fix_only, unsafe_fixes: options.unsafe_fixes.map(UnsafeFixes::from), - output_format: options.output_format, + output_format, force_exclude: options.force_exclude, line_length: options.line_length, indent_width, @@ -492,7 +518,6 @@ impl Configuration { preview: options.preview.map(PreviewMode::from), required_version: options.required_version, respect_gitignore: options.respect_gitignore, - show_source: options.show_source, show_fixes: options.show_fixes, src: options .src @@ -539,7 +564,6 @@ impl Configuration { namespace_packages: self.namespace_packages.or(config.namespace_packages), required_version: self.required_version.or(config.required_version), respect_gitignore: self.respect_gitignore.or(config.respect_gitignore), - show_source: self.show_source.or(config.show_source), show_fixes: self.show_fixes.or(config.show_fixes), src: self.src.or(config.src), target_version: self.target_version.or(config.target_version), diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index f2fabac55f65b..a6b0cdd9ba7af 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -72,14 +72,14 @@ pub struct Options { )] pub extend: Option, - /// The style in which violation messages should be formatted: `"text"` - /// (default), `"grouped"` (group messages by file), `"json"` + /// The style in which violation messages should be formatted: `"full"` + /// (shows source),`"concise"` (default), `"grouped"` (group messages by file), `"json"` /// (machine-readable), `"junit"` (machine-readable XML), `"github"` (GitHub /// Actions annotations), `"gitlab"` (GitLab CI code quality report), /// `"pylint"` (Pylint text format) or `"azure"` (Azure Pipeline logging commands). #[option( - default = r#""text""#, - value_type = r#""text" | "json" | "junit" | "github" | "gitlab" | "pylint" | "azure""#, + default = r#""concise""#, + value_type = r#""full" | "concise" | "grouped" | "json" | "junit" | "github" | "gitlab" | "pylint" | "azure""#, example = r#" # Group violations by containing file. output-format = "grouped" @@ -117,6 +117,9 @@ pub struct Options { show-source = true "# )] + #[deprecated( + note = "`show_source` is deprecated and is now part of `output_format` in the form of `full` or `concise` options. Please update your configuration." + )] pub show_source: Option, /// Whether to show an enumeration of all fixed lint violations diff --git a/crates/ruff_workspace/src/settings.rs b/crates/ruff_workspace/src/settings.rs index a4b5b785d0454..f37dd3723b692 100644 --- a/crates/ruff_workspace/src/settings.rs +++ b/crates/ruff_workspace/src/settings.rs @@ -31,8 +31,6 @@ pub struct Settings { pub output_format: SerializationFormat, #[cache_key(ignore)] pub show_fixes: bool, - #[cache_key(ignore)] - pub show_source: bool, pub file_resolver: FileResolverSettings, pub linter: LinterSettings, @@ -46,9 +44,8 @@ impl Default for Settings { cache_dir: cache_dir(project_root), fix: false, fix_only: false, - output_format: SerializationFormat::default(), + output_format: SerializationFormat::default(false), show_fixes: false, - show_source: false, unsafe_fixes: UnsafeFixes::default(), linter: LinterSettings::new(project_root), file_resolver: FileResolverSettings::new(project_root), @@ -70,7 +67,6 @@ impl fmt::Display for Settings { self.fix_only, self.output_format, self.show_fixes, - self.show_source, self.unsafe_fixes, self.file_resolver | nested, self.linter | nested, diff --git a/docs/configuration.md b/docs/configuration.md index 2114f2f6e774b..715ef73bccf83 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -510,7 +510,9 @@ Options: Include fixes that may not retain the original intent of the code. Use `--no-unsafe-fixes` to disable --show-source - Show violations with source code. Use `--no-show-source` to disable + Show violations with source code. Use `--no-show-source` to disable. + (Deprecated: use `--output-format=full` or `--output-format=concise` + instead of `--show-source` and `--no-show-source`, respectively) --show-fixes Show an enumeration of all fixed lint violations. Use `--no-show-fixes` to disable @@ -526,9 +528,11 @@ Options: --ignore-noqa Ignore any `# noqa` comments --output-format - Output serialization format for violations [env: RUFF_OUTPUT_FORMAT=] - [possible values: text, json, json-lines, junit, grouped, github, - gitlab, pylint, azure, sarif] + Output serialization format for violations. The default serialization + format is "concise". In preview mode, the default serialization + format is "full" [env: RUFF_OUTPUT_FORMAT=] [possible values: text, + concise, full, json, json-lines, junit, grouped, github, gitlab, + pylint, azure, sarif] -o, --output-file Specify file to write the linter output to (default: stdout) --target-version diff --git a/ruff.schema.json b/ruff.schema.json index 6f8639199f71e..7636e304a13e0 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -519,7 +519,7 @@ } }, "output-format": { - "description": "The style in which violation messages should be formatted: `\"text\"` (default), `\"grouped\"` (group messages by file), `\"json\"` (machine-readable), `\"junit\"` (machine-readable XML), `\"github\"` (GitHub Actions annotations), `\"gitlab\"` (GitLab CI code quality report), `\"pylint\"` (Pylint text format) or `\"azure\"` (Azure Pipeline logging commands).", + "description": "The style in which violation messages should be formatted: `\"full\"` (shows source),`\"concise\"` (default), `\"grouped\"` (group messages by file), `\"json\"` (machine-readable), `\"junit\"` (machine-readable XML), `\"github\"` (GitHub Actions annotations), `\"gitlab\"` (GitLab CI code quality report), `\"pylint\"` (Pylint text format) or `\"azure\"` (Azure Pipeline logging commands).", "anyOf": [ { "$ref": "#/definitions/SerializationFormat" @@ -660,6 +660,7 @@ }, "show-source": { "description": "Whether to show source code snippets when reporting lint violations (overridden by the `--show-source` command-line flag).", + "deprecated": true, "type": [ "boolean", "null" @@ -3816,6 +3817,8 @@ "type": "string", "enum": [ "text", + "concise", + "full", "json", "json-lines", "junit",