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: Allow gofmt to return non-zero if code violations are found #24230

Closed
ryanm101 opened this issue Mar 3, 2018 · 9 comments
Closed

Comments

@ryanm101
Copy link

ryanm101 commented Mar 3, 2018

What version of Go are you using (go version)?

1.8.3

Does this issue reproduce with the latest release?

Yes

gofmt is a great way to refactor code for developers however in it's current form it requires the developer to actively execute it against their code.

If we add an option to allow gofmt to return a non-zero value if violations are found it would facilitate the integration of it into our build pipelines as we could force builds to fail upon a violation.

This would also reduce the need for 3rd party linting tools such as golint.

@gopherbot gopherbot added this to the Proposal milestone Mar 3, 2018
@davecheney
Copy link
Contributor

davecheney commented Mar 3, 2018 via email

@ryanm101
Copy link
Author

ryanm101 commented Mar 3, 2018

I'm not overly experienced with GO but running "go test -z" does nothing for me even once I put in a badly indented line

@mvdan
Copy link
Member

mvdan commented Mar 3, 2018

Dave likely meant something like test -z $(gofmt -l your-dir). That checks if there are zero files that gofmt would modify.

@ryanm101
Copy link
Author

ryanm101 commented Mar 3, 2018

lol.. of course. I thought there was a go specific command :)

@davecheney
Copy link
Contributor

davecheney commented Mar 4, 2018 via email

@magical
Copy link
Contributor

magical commented Mar 5, 2018

Possibly worth noting that this sort of thing is discouraged by the go team:

Note that these kinds of minor updates to gofmt are expected from time to time. In general, we recommend against building systems that check that source code matches the output of a specific version of gofmt. For example, a continuous integration test that fails if any code already checked into a repository is not “properly formatted” is inherently fragile and not recommended.

@davecheney
Copy link
Contributor

davecheney commented Mar 5, 2018 via email

@dmitris
Copy link
Contributor

dmitris commented Mar 5, 2018

FWIW we always fail the builds in our CI system if gofmt violations are found - that really helps to have the consistent codebase and is very effective in getting engineers new to Go or to the company used to properly format the code. It also prevents having non-compliant files from accumulating in the codebase. So far we haven't seen any problems with gofmt changes leading to fragility etc, adn go1.9 to go1.10 transition was OK. (We did see several failures due to https://golang.org/doc/go1.10#test-vet but those got quickly fixed)

@rsc
Copy link
Contributor

rsc commented Mar 5, 2018

As @mvdan said, you can already check whether the output of gofmt -l is empty. Please do that. That's more than one bit anyway.

@rsc rsc closed this as completed Mar 5, 2018
prashanthpai pushed a commit to gluster/glusterd2 that referenced this issue Sep 24, 2018
`gofmt` is returning zero even if format is not proper. Instead it
returns list of files which are not properly formatted.

This issue in golang repo suggested to use `test -z` for the same
golang/go#24230

Signed-off-by: Aravinda VK <avishwan@redhat.com>
lukpueh added a commit to lukpueh/in-toto-golang that referenced this issue Jan 24, 2019
Test if go code is compliant with gofmt by checking the commands
output in the automated builds scripts.

Method adopted from
golang/go#24230
lukpueh added a commit to lukpueh/in-toto-golang that referenced this issue Jan 24, 2019
Test if go code is compliant with gofmt by checking the commands
output in the automated builds scripts.

Method adopted from
golang/go#24230
@golang golang locked and limited conversation to collaborators Mar 5, 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

7 participants