-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Description
First, I know that this has been asked many times before (#24230 #24427 #38551), and I was even on the other side of this argument for a long time.
The argument usually goes that, if you want to know if any files aren't gofmt-ed, you check that gofmt -l
is empty, for example:
test -z $(gofmt -l .)
And this works, if one can assume a POSIX-like shell.
However, in light of #42119, I think we should make "verify that many files comply with gofmt" a trivial command, without extra machinery like checking for non-empty output.
In that issue, we suggest a number of commands that we'd encourage people to run on CI, from go test -race ./...
to go mod verify
and go vet ./...
. You'll note that none of the commands require being wrapped with shell or extra commands to do their job, with two exceptions:
go mod tidy
, where one needs to check if files likego.mod
andgo.sum
were modified on disk. cmd/go: add mod tidy -diff #27005 will fix that.gofmt
, which needs testing for empty output, hence this issue.
Once go mod tidy -check
is implemented, gofmt
will stand alone in that list as being the only command that most people should run on their CI, but which is non-portable or just not easy to use directly.
--
So, as others have proposed before: I suggest a very simple fix, which is to make gofmt -d
exit with a status code of 1 if any of the input files have a non-empty diff. Then, CI could simply run gofmt -d .
and get a good result: CI would fail if any files don't comply, and the diff will show exactly which ones and why.
It's also arguably more consistent to behave this way, as the diff
tool usually behaves this way too. For example, from GNU diffutils 3.7:
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
If we think this change is too invasive, or could break too many users, we could also consider a separate flag like -exitcode
, simialr to git diff --exit-code
. It's really not a big deal if the command run on CI gets a bit longer.
I also don't think a solution should come from go fmt
. I think gofmt
is simply superior for use in CI; it allows formatting files which are not part of Go packages, formatting many modules at once, and printing diffs.
It's also worth noting I plan to expose this feature in gofumpt
: mvdan/gofumpt#114
cc @griesemer
cc @bcmills @jayconrod @stevetraut for "CI best practices"
cc @ryanm101 @stevenmatthewt @belm0 as previous posters
Metadata
Metadata
Assignees
Type
Projects
Status