From 839a55ff9d0f5b02d4d257b4bb4aa9214914c906 Mon Sep 17 00:00:00 2001 From: leiserfg Date: Thu, 21 Sep 2023 22:02:40 +0200 Subject: [PATCH] Make ruff format idempotent when using stdin input --- crates/ruff_cli/src/commands/format_stdin.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/ruff_cli/src/commands/format_stdin.rs b/crates/ruff_cli/src/commands/format_stdin.rs index b428d3d14724d..f4d8a6b089e5b 100644 --- a/crates/ruff_cli/src/commands/format_stdin.rs +++ b/crates/ruff_cli/src/commands/format_stdin.rs @@ -71,15 +71,16 @@ fn format_source( let formatted = format_module(&unformatted, options) .map_err(|err| FormatCommandError::FormatModule(path.map(Path::to_path_buf), err))?; let formatted = formatted.as_code(); + + if mode.is_write() { + stdout() + .lock() + .write_all(formatted.as_bytes()) + .map_err(|err| FormatCommandError::Write(path.map(Path::to_path_buf), err))?; + } if formatted.len() == unformatted.len() && formatted == unformatted { Ok(FormatCommandResult::Unchanged) } else { - if mode.is_write() { - stdout() - .lock() - .write_all(formatted.as_bytes()) - .map_err(|err| FormatCommandError::Write(path.map(Path::to_path_buf), err))?; - } Ok(FormatCommandResult::Formatted) } }