Skip to content

Commit 74dbd87

Browse files
authored
Make ruff format idempotent when using stdin input (#7581)
## Summary Currently, this happens ```sh $ echo "print()" | ruff format - #Notice that nothing went to stdout ``` Which does not match `ruff check --fix - ` behavior and deletes my code every time I format it (more or less 5 times per minute 😄). I just checked that my example works as the change was very straightforward.
1 parent d7508af commit 74dbd87

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

crates/ruff_cli/src/commands/format_stdin.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ fn format_source(
7171
let formatted = format_module(&unformatted, options)
7272
.map_err(|err| FormatCommandError::FormatModule(path.map(Path::to_path_buf), err))?;
7373
let formatted = formatted.as_code();
74+
75+
if mode.is_write() {
76+
stdout()
77+
.lock()
78+
.write_all(formatted.as_bytes())
79+
.map_err(|err| FormatCommandError::Write(path.map(Path::to_path_buf), err))?;
80+
}
7481
if formatted.len() == unformatted.len() && formatted == unformatted {
7582
Ok(FormatCommandResult::Unchanged)
7683
} else {
77-
if mode.is_write() {
78-
stdout()
79-
.lock()
80-
.write_all(formatted.as_bytes())
81-
.map_err(|err| FormatCommandError::Write(path.map(Path::to_path_buf), err))?;
82-
}
8384
Ok(FormatCommandResult::Formatted)
8485
}
8586
}

0 commit comments

Comments
 (0)