numtide / treefmt

one CLI to format your repo
https://treefmt.com
MIT License
534 stars 32 forks source link

changed files are reported incorrectly with yamlfmt #279

Closed nokazn closed 2 months ago

nokazn commented 2 months ago

Describe the bug

I set up treefmt with google/yamlfmt, and when running treefmt --fail-on-change, encountered unexpected behavior.

There seems to be no unformatted files, but treefmt reports some files changed and fails in most cases. Sometimes I can get correct reports (0 files changed) and the command not fails in some reason.

To Reproduce

Prepare some unformatted YAML files and set up yamlfmt in treefmt.toml like https://github.com/nokazn/treefmt-yamlfmt-repro-279

# treefmt.toml
[formatter.yaml]
command = "yamlfmt"
includes = ["*.yaml", "*.yml"]
  1. Run nix develop --command treefmt --no-cache
  2. 1 files changed message is reported and the unformatted files are formatted
  3. Run nix develop --command treefmt --no-cache again
  4. 1 files changed message is shown again and nothing seems to happen to the YAML files

Expected behavior

In step 4, 0 files changed message is shown. (And never fails with --fail-on-change)

System information

$ treefmt --version
treefmt 0.6.1

Additional context

N/A

zimbatm commented 2 months ago

This is probably a bug with yamlfmt. treefmt assumes that the formatting tool won't write to the files if the formatting hasn't changed.

you can test that by running

stat myfile.yaml
yamlfmt myfile.yaml
stat myfile.yaml

If the mtime has changed, then it means that yamlfmt is not compatible with treefmt.

nokazn commented 2 months ago

@zimbatm

Indeed mtime is updated even though the file has no changes.

$ stat example.yaml; yamlfmt example.yaml; stat example.yaml                                                                                                                                    9:37:40
  File: example.yaml
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 8,32    Inode: 62136       Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  nokazn)   Gid: ( 1000/  nokazn)
Access: 2024-04-06 09:37:40.200167151 +0900
Modify: 2024-04-06 09:37:40.200167151 +0900
Change: 2024-04-06 09:37:40.200167151 +0900
 Birth: 2024-04-06 00:55:13.169203563 +0900
  File: example.yaml
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 8,32    Inode: 62136       Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  nokazn)   Gid: ( 1000/  nokazn)
Access: 2024-04-06 09:37:44.890167171 +0900
Modify: 2024-04-06 09:37:44.890167171 +0900
Change: 2024-04-06 09:37:44.890167171 +0900
 Birth: 2024-04-06 00:55:13.169203563 +0900

I'll ask yamlfmt side about this and close this issue. Thank yoy for your reply!

Edit: https://github.com/google/yamlfmt/pull/165 will resolve this. Sorry for my quick judgement...