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/gofmt: add a flag to return an error exit status. #45953

Closed
jlamur opened this issue May 4, 2021 · 1 comment
Closed

proposal: cmd/gofmt: add a flag to return an error exit status. #45953

jlamur opened this issue May 4, 2021 · 1 comment

Comments

@jlamur
Copy link

jlamur commented May 4, 2021

The most convenient way to check in a script the result of a command or another shell script is to check its exit code. However checking in a script (eg. in a CI job) that the formatting of source files is correct requires a bit of scripting with gofmt. In particular, a one-liner is not very readable as demonstrated below.

As an example, here's what I do on my CI job:

test "`gofmt $src_files -l | wc -l`" -eq 0 || exit 1

(granted that my CI job fails if the command returns an error code != 0, and fails otherwise)

To simplify such a use case, I propose to add a flag to the gofmt: -x which exits the program with an error code != 0 iff there were any files that were modified (or any file that must be modified in case the program is dry-run with -l).

I proposed an implementation on https://go-review.googlesource.com/c/go/+/316930 that was -2 because its an API change, hence this proposal.

On the implementation itself, there are two things that could be discussed IMO:

  • the program returns an error code 2; returning anything else would complicate the implementation as an error during the formatting (eg. permissions insufficient) also returns 2. However I think it's safe to assume nobody want to differentiate between errors and wrongly formatted source code.
  • the -x flag name is maybe not the best choice, I chose it because of e[x]it and both [e]xit and [r]eturn code are taken.
@gopherbot gopherbot added this to the Proposal milestone May 4, 2021
@seankhliao
Copy link
Member

Duplicate of #24230 #24427 #38551 #41189

@golang golang locked and limited conversation to collaborators May 4, 2022
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