google / google/yamlfmt

yamlfmt -dry and yamlfmt (no -dry) have a difference of opinion

Open
#155 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
1.8k
Forks
75
PR merge metrics
No merged PRs in 30d

Description

## SUMMARY

`yamlfmt` in fix mode will change files that `yamlfmt -dry` says don't need to be changed.

The changes made by `yamlfmt` are then noted as formatting failures by `yamlfmt -lint`.

Each time I run `yamlfmt`, it adds more newlines to the same file -- is not idempotent.

## DETAILS

Running fix mode / dry run does not find files to change. But running fix mode / live makes a change. Running lint mode complains a file that was just changed by fix mode / live needs to change again. I manually check the file and it does have blank lines that need to be removed. Thus fix mode / live seems to disagree with lint mode or fix mode / dry run:

```
❯ git status | grep yml

❯ yamlfmt -conf ./.yamlfmt -dry
2024/01/30 17:08:04 No files will be changed.

❯ yamlfmt -conf ./.yamlfmt

❯ git status | grep yml
modified: path/to/some/yaml/file.yml

❯ yamlfmt -conf ./.yamlfmt -lint -quiet
2024/01/30 17:18:13 The following files had formatting differences:

path/to/some/yaml/file.yml
```

Also, if I run fix mode / live on the same file repeatedly, it grows every time (as `yamlfmt` adds 4 newlines per run):

```
❯ wc path/to/some/yaml/file.yml
215 781 6800 path/to/some/yaml/file.yml

❯ yamlfmt -conf ./.yamlfmt

❯ wc path/to/some/yaml/file.yml
219 781 6804 path/to/some/yaml/file.yml

❯ yamlfmt -conf ./.yamlfmt

❯ wc path/to/some/yaml/file.yml
223 781 6808 path/to/some/yaml/file.yml
```

I'm doing this in a dev-local environment where the .yml/.yaml files are not being changed by anything but `yamlfmt`.

Observations:
- It seems `yamlfmt` fix mode isn't fully reliable.
- If `yamlfmt -dry` reports no changes and `yamlfmt` then makes changes, that is a defect. Maybe there should be tests that run `yamlfmt -dry` first - if it reports no changes needed then `yamlfmt` does make changes, that is a failure case.
- If `yamlfmt` makes changes and `yamlfmt -lint` fails on a just-changed file, that is a defect. Maybe there should be tests that run `yamlfmt` - note changed files - if `yamlfmt -lint` doesn't accept those files as-is, that is a failure case.
- If `yamlfmt` makes changes, then is run again and makes changes to the same file (like adding newlines), that is a defect. Maybe there should be tests that run `yamlfmt` twice - if any file changes in both runs, that is a failure case.
- This is failing in just one .yml file in a repo that has many .yml files - nothing special about that file.
- Every time I run `yamlfmt`, it adds one more blank line to that file (observed using `git diff`).

## ENVIRONMENT

I'm running `yamlfmt` version 0.10.0.

This happens with a completely empty config file.

## WHAT'S NEXT

I can provide a copy of the problem .yml file if that'd help.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.