Skip to content

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

@stevenmatthewt

Description

@stevenmatthewt

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions