From a1d24fc353ffe1e20ee17da4e9f4db640486b2c2 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 7 Mar 2023 09:41:28 +0000 Subject: [PATCH 1/4] fix(rome_cli): exit with error code while applying fixes --- crates/rome_cli/src/diagnostics.rs | 59 +++++++++++++++++-- crates/rome_cli/src/traversal.rs | 8 ++- .../main_commands_check/apply_noop.snap | 8 +++ .../main_commands_check/apply_ok.snap | 8 +++ .../should_disable_a_rule_group.snap | 8 +++ .../src/file_handlers/javascript.rs | 16 ++++- crates/rome_service/src/file_handlers/mod.rs | 23 +++++++- crates/rome_service/src/workspace.rs | 3 + 8 files changed, 125 insertions(+), 8 deletions(-) diff --git a/crates/rome_cli/src/diagnostics.rs b/crates/rome_cli/src/diagnostics.rs index 8527188653c..8d6d1bb2b32 100644 --- a/crates/rome_cli/src/diagnostics.rs +++ b/crates/rome_cli/src/diagnostics.rs @@ -1,4 +1,4 @@ -use rome_console::fmt::Formatter; +use rome_console::fmt::{Display, Formatter}; use rome_console::markup; use rome_diagnostics::adapters::{IoError, PicoArgsError}; use rome_diagnostics::{ @@ -147,9 +147,49 @@ pub struct IncompatibleArguments { #[diagnostic( category = "internalError/io", severity = Error, - message = "Some errors were emitted while running checks" + message( + description = "{action_kind}", + message({{self.action_kind}}) + ) )] -pub struct CheckError; +pub struct CheckError { + action_kind: CheckActionKind, +} + +#[derive(Debug, Clone, Copy)] +pub enum CheckActionKind { + Check, + Apply, +} + +impl Display for CheckActionKind { + fn fmt(&self, fmt: &mut Formatter) -> std::io::Result<()> { + match self { + CheckActionKind::Check => fmt.write_markup(markup! { + "Some errors were emitted while ""running checks" + }), + CheckActionKind::Apply => fmt.write_markup(markup! { + "Fixes applied"", but there are still diagnostics to be addressed" + }), + } + } +} + +impl std::fmt::Display for CheckActionKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + CheckActionKind::Check => { + write!(f, "Some errors were emitted while running checks") + } + CheckActionKind::Apply => { + write!( + f, + "Fixes applied but there are still diagnostics to be addressed" + ) + } + } + } +} #[derive(Debug, Diagnostic)] #[diagnostic( @@ -300,9 +340,18 @@ impl CliDiagnostic { }) } - /// Emitted when errors were emitted while traversing the file system + /// Emitted when errors were emitted while running `check` command pub fn check_error() -> Self { - Self::CheckError(CheckError) + Self::CheckError(CheckError { + action_kind: CheckActionKind::Check, + }) + } + + /// Emitted when errors were emitted while apply code fixes + pub fn apply_error() -> Self { + Self::CheckError(CheckError { + action_kind: CheckActionKind::Apply, + }) } /// Emitted when the server is not running diff --git a/crates/rome_cli/src/traversal.rs b/crates/rome_cli/src/traversal.rs index e9b370899ca..444847eb24f 100644 --- a/crates/rome_cli/src/traversal.rs +++ b/crates/rome_cli/src/traversal.rs @@ -825,7 +825,13 @@ fn process_file(ctx: &TraversalOptions, path: &Path) -> FileResult { .with_file_path(path.display().to_string())?; } - return Ok(FileStatus::Success); + return if fixed.errors == 0 { + Ok(FileStatus::Success) + } else { + Ok(FileStatus::Message(Message::Error( + CliDiagnostic::apply_error().into(), + ))) + }; } let categories = if ctx.execution.is_format() || supported_lint.reason.is_some() { diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/apply_noop.snap b/crates/rome_cli/tests/snapshots/main_commands_check/apply_noop.snap index f9ba2ba534a..08a8eeb66d5 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_check/apply_noop.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_check/apply_noop.snap @@ -12,6 +12,14 @@ if(a != 0) {} # Emitted Messages +```block +internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Fixes applied, but there are still diagnostics to be addressed + + +``` + ```block Skipped 1 suggested fixes. If you wish to apply the suggested (unsafe) fixes, use the command rome check --apply-unsafe diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/apply_ok.snap b/crates/rome_cli/tests/snapshots/main_commands_check/apply_ok.snap index f9ba2ba534a..08a8eeb66d5 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_check/apply_ok.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_check/apply_ok.snap @@ -12,6 +12,14 @@ if(a != 0) {} # Emitted Messages +```block +internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Fixes applied, but there are still diagnostics to be addressed + + +``` + ```block Skipped 1 suggested fixes. If you wish to apply the suggested (unsafe) fixes, use the command rome check --apply-unsafe diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/should_disable_a_rule_group.snap b/crates/rome_cli/tests/snapshots/main_commands_check/should_disable_a_rule_group.snap index 8b17cd83dd7..01c0283960f 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_check/should_disable_a_rule_group.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_check/should_disable_a_rule_group.snap @@ -30,6 +30,14 @@ try { # Emitted Messages +```block +internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Fixes applied, but there are still diagnostics to be addressed + + +``` + ```block Fixed 1 file(s) in