Skip to content

Commit cf192fa

Browse files
committed
make --write only write on change
Currently, even if there are no changes `--write` will still write to file, changing its last modified timestamp. This causes a problem for things that monitors file modification by that timestamp. For example treefmt `--fail-on-change` will fail, since it expects formatters to not write to file if nothing changes: https://github.com/numtide/treefmt/blob/main/docs/site/reference/formatter-spec.md#2-write-to-changed-files This commit changes the behavior to only write if there are changes.
1 parent 2de904f commit cf192fa

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

cmd/root.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ func processInput(inputName string, inputBytes []byte, config *lib.Config) (form
8787
}
8888
return true
8989
} else if writeFlag {
90-
err := os.WriteFile(inputName, []byte(formattedContent), 0644)
91-
if err != nil {
92-
log.Fatalf("Failed to write to file %s: %v", inputName, err)
90+
if originalContent != formattedContent {
91+
err := os.WriteFile(inputName, []byte(formattedContent), 0644)
92+
if err != nil {
93+
log.Fatalf("Failed to write to file %s: %v", inputName, err)
94+
}
9395
}
9496
} else {
9597
_, err := os.Stdout.Write([]byte(formattedContent))

0 commit comments

Comments
 (0)