Skip to content

Commit 19a531b

Browse files
nojafcknitt
authored andcommitted
Only save formatted files if they changed (#8209)
* Only save formatted files if they changed * Add changelog
1 parent 77226c6 commit 19a531b

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#### :nail_care: Polish
3636

37+
- Formatter no longer writes files when contents are already correctly formatted. https://github.com/rescript-lang/rescript/pull/8209
38+
3739
#### :house: Internal
3840

3941
# 12.1.0

rewatch/src/format.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,21 @@ fn format_files(bsc_exe: &Path, files: Vec<String>, check: bool) -> Result<()> {
8989
files.par_chunks(batch_size).try_for_each(|batch| {
9090
batch.iter().try_for_each(|file| {
9191
let mut cmd = Command::new(bsc_exe);
92-
if check {
93-
cmd.arg("-format").arg(file);
94-
} else {
95-
cmd.arg("-o").arg(file).arg("-format").arg(file);
96-
}
92+
// Always get formatted output to stdout for comparison
93+
cmd.arg("-format").arg(file);
9794

9895
let output = cmd.output()?;
9996

10097
if output.status.success() {
101-
if check {
102-
let original_content = fs::read_to_string(file)?;
103-
let formatted_content = String::from_utf8_lossy(&output.stdout);
104-
if original_content != formatted_content {
98+
let original_content = fs::read_to_string(file)?;
99+
let formatted_content = String::from_utf8_lossy(&output.stdout);
100+
if original_content != formatted_content {
101+
if check {
105102
eprintln!("[format check] {file}");
106103
incorrectly_formatted_files.fetch_add(1, Ordering::SeqCst);
104+
} else {
105+
// Only write if content actually changed
106+
fs::write(file, &*formatted_content)?;
107107
}
108108
}
109109
} else {

0 commit comments

Comments
 (0)