Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: cmd/fmt: flag to set exit code when formatting occurs #24427

Closed
stevenmatthewt opened this issue Mar 16, 2018 · 1 comment
Closed

Comments

@stevenmatthewt
Copy link

stevenmatthewt commented Mar 16, 2018

The 'Problem'

Currently, when gofmt is executed, it will identify and correct formatting issues, but will exit with an exit code of 0. This is ordinarily acceptable, but can be annoying in particular instances.

I always use gofmt ./... in my Continuous Integration pipelines, and fail the pipeline if any formatting errors were present. At the moment, I use some bash magic like this:

echo 'running go fmt on all packages...'
invalidFiles=$(gofmt -l ./... 2>&1)
if [ "$invalidFiles" ]; then
  echo "These files did not pass the 'go fmt' check, please run 'go fmt' on them:"
  echo $invalidFiles
  exit 1
fi

(I realize this could be shortened, but it's easiest to understand in this form)

Obviously, this works fine. But It's slightly annoying repeating this when it seems logical that the gofmt command could simply exit with a non-zero exit code.


The Proposal

Add a flag to the gofmt tool that will set the exit code to 1 when any formatting issues are detected. I was originally thinking -e, but this is already taken. Perhaps -x is acceptable?

This is the behavior I would like to see with this flag:

> gofmt -l -x ./...
  src/some/file.go
> echo $?
  1

Thoughts? Is the simplification of the testing pipeline worth the (minor) complexity added to the gofmt tool? If it sounds good, I can probably have a PR up for it pretty quickly.

@gopherbot gopherbot added this to the Proposal milestone Mar 16, 2018
@ianlancetaylor
Copy link
Contributor

Dup of #24230, which was closed.

@golang golang locked and limited conversation to collaborators Mar 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants