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

Add gofmt -s to CI #146

Merged
merged 8 commits into from
Jun 29, 2020
Merged

Add gofmt -s to CI #146

merged 8 commits into from
Jun 29, 2020

Conversation

jojo43
Copy link
Contributor

@jojo43 jojo43 commented Jun 6, 2020

Hi, this is my first contribution.
Please feedback me if there's anything.

This PR resolves a part of #108 .

@hashicorp-cla
Copy link

hashicorp-cla commented Jun 6, 2020

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR.

I reckon it's sensible to run these checks only on Linux, assuming that gofmt behaves consistently the same on Windows/macOS/Linux.

It seems that this won't be solved natively in gofmt any time soon: golang/go#24230
and the only recommendation seems Linux/Unix-specific (test -z "$(gofmt -s -l "$file")")

I'm not sure about introducing platform-specific logic into the makefile as we just removed it recently in #98 but arguably Make is a unix tool anyway, so maybe that's expected? 🤔

Makefile Outdated
@gofmt -s -w $(SOURCE_FILES)

fmtcheck:
@gofmt -s -l $(SOURCE_FILES) | grep ^; if [ $$? -eq 0 ]; then exit 1; fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this won't work on Windows, which I suspect is the reason the first commit has failed CI checks.

Makefile Outdated
@@ -1,6 +1,14 @@
SOURCE_FILES ?= $$(find . -not -path "./vendor/*" -type f -name "*.go")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work on Windows I think

@radeksimko radeksimko added the ci Continuous integration/delivery related label Jun 9, 2020
@jojo43
Copy link
Contributor Author

jojo43 commented Jun 10, 2020

Thank you for the review. 😃

How about simply not using Makefile like this?

@radeksimko
Copy link
Member

radeksimko commented Jun 18, 2020

How about simply not using Makefile like this?

I'm not opposed to it, but I'm also thinking about this from the perspective of a new contributor. If we enforce some checks in the CI, then contributors should have an easy way of (a) checking that their contribution complies, and (b) fix code that isn't compliant - and do this before they make that contribution.

Right now it appears that Windows users would be left with the only option of either using IDE which does the appropriate formatting for them, or create their own PowerShell/other script. While I'm not Windows user myself, I think that's suboptimal.

@jojo43
Copy link
Contributor Author

jojo43 commented Jun 19, 2020

If we enforce some checks in the CI, then contributors should have an easy way of (a) checking that their contribution complies, and (b) fix code that isn't compliant - and do this before they make that contribution.

Make sense.

My another idea is using golangci-lint.
It needs installation step, but could be run on any OS by the same command.

@radeksimko
Copy link
Member

radeksimko commented Jun 24, 2020

Yeah, I think that's a decent solution, but I'd like to first understand all the complexity introduced by golangci-lint and all the other things it does.

My understanding is that it has some opinions on how code should be written and I wouldn't want to treat these opinions as a barrier for contributions to the language server. If CI does flag up something I'd like it to be really justified and I'd like to be sure that these are genuine problems or serious risks.

With that in mind the -s flag of gofmt is an opinion which does not seem to be shared by go fmt, so I'm already on the edge of whether to enforce that in CI checks, but I can be convinced about that more easily than about any linter, assuming we can provide a way for contributors to auto-format the code easily.

@radeksimko
Copy link
Member

I haven't tried this myself, but https://github.com/mh-cbon/go-fmt-fail looks like something I would otherwise build in this situation, so that may be worth checking too.

@jojo43
Copy link
Contributor Author

jojo43 commented Jun 28, 2020

My understanding is that it has some opinions on how code should be written and I wouldn't want to treat these opinions as a barrier for contributions to the language server. If CI does flag up something I'd like it to be really justified and I'd like to be sure that these are genuine problems or serious risks.

Thank you for your polite comment.
I want to respect your thoughts.

I haven't tried this myself, but https://github.com/mh-cbon/go-fmt-fail looks like something I would otherwise build in this situation, so that may be worth checking too.

I've tried go-fmt-fail in my forked repository ( jojo43#3 ), and it worked as same even on Windows.
So, if you don't mind if -s flag can't be used, I think it is reasonable solution.

@radeksimko
Copy link
Member

radeksimko commented Jun 28, 2020

Sounds great - feel free to modify your PR to use that utility then 👍

Just a few suggestions wrt the linked PR in your repository

(1) I believe it can be significantly simplified by invoking the tool via go run github.com/mh-cbon/go-fmt-fail instead of running the binary directly.

(2) Most Go commands (including go test and go run) support -mod=vendor flag which forces them to only look into the vendor folder - that means we can avoid reinstalling it in every CI check.

That flag is something we should (and currently don't) generally use for all Go commands in CI/makefile to ensure reproducibility and consistency.

That can be fixed by setting GOFLAGS variable to -mod=vendor in the Makefile, e.g. by adding the following line at the top

export GOFLAGS = -mod=vendor

(which seems to work on Windows too FWIW)

and by setting the same variable in the CI via env.

(3)The failure you saw earlier in your Windows CI run is most likely related to the default git settings on Windows where it converts \n to \r\n (i.e. adds carriage return characters). This can (and IMO should) be turned off for all builds via git config --global core.autocrlf false prior to the checkout step.

Copy link
Contributor Author

@jojo43 jojo43 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your advice. 😃
I've learned a lot from you.

I added some fixes.
Please take another look.

.gitattributes Show resolved Hide resolved
@radeksimko
Copy link
Member

Thank you for making these last changes and for your contribution overall. 👍

@radeksimko radeksimko merged commit c3bdf23 into hashicorp:master Jun 29, 2020
@jojo43 jojo43 deleted the gofmt-ci branch June 29, 2020 19:57
@ghost
Copy link

ghost commented Jul 29, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the context necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ci Continuous integration/delivery related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants