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

Use gofumpt if available, and enable gofumpt linter #3798

Merged
merged 2 commits into from
Nov 4, 2022

Conversation

thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Sep 30, 2022

gofumpt (https://github.com/mvdan/gofumpt) provides a supserset of gofmt / go fmt, but not every developer may have
it installed, so for situations where it's not available, fall back to gofmt.

As our code has been formatted with gofumpt already, in most cases contributions
will follow those formatting rules, but in some cases there may be a difference,
which would already be flagged by manual code review, but let's also enable the
gofumpt linter.

With this change, make fmt will use gofumpt is available; gofumpt has been
added to the dev-container, so make -f docker.Makefile fmt will always use it.

@thaJeztah thaJeztah force-pushed the gofumpt_linting branch 2 times, most recently from 380d7fc to a92c219 Compare September 30, 2022 11:51
@thaJeztah thaJeztah marked this pull request as ready for review September 30, 2022 11:51
@thaJeztah
Copy link
Member Author

@crazy-max @vvoland ptal 😄

Makefile Outdated
go list -f {{.Dir}} ./... | xargs gofmt -w -s -d
fmt: ## run gofumpt (if present) or gofmt
@if command -v gofumpt > /dev/null; then \
gofumpt -w -d . ; \
Copy link
Member Author

Choose a reason for hiding this comment

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

gofumpt automatically excludes vendor/ so no need to use go list

@thaJeztah
Copy link
Member Author

Hm... interesting; somehow my gofumpt didn't format all files?

#17 87.07 e2e/plugin/basic/basic.go:17: File is not `gofumpt`-ed (gofumpt)
#17 87.07 	if err := os.MkdirAll(p, 0755); err != nil {
#17 87.07 cli/context/store/metadatastore.go:31: File is not `gofumpt`-ed (gofumpt)
#17 87.07 	if err := os.MkdirAll(contextDir, 0755); err != nil {
#17 87.07 cli/context/store/metadatastore.go:38: File is not `gofumpt`-ed (gofumpt)
#17 87.07 	return os.WriteFile(filepath.Join(contextDir, metaFile), bytes, 0644)
#17 87.07 cli/context/store/store.go:249: File is not `gofumpt`-ed (gofumpt)

Looks like the linter uses an explicit -lang, which (for go1.19)
results in some additional formatting for octal values.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
gofumpt provides a supserset of gofmt / go fmt, but not every developer may have
it installed, so for situations where it's not available, fall back to gofmt.

As our code has been formatted with gofumpt already, in most cases contributions
will follow those formatting rules, but in some cases there may be a difference,
which would already be flagged by manual code review, but let's also enable the
gofumpt linter.

With this change, `make fmt` will use gofumpt is available; gofumpt has been
added to the dev-container, so `make -f docker.Makefile fmt` will always use it.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@codecov-commenter
Copy link

codecov-commenter commented Sep 30, 2022

Codecov Report

Merging #3798 (c2f1671) into master (65438e0) will not change coverage.
The diff coverage is 66.66%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3798   +/-   ##
=======================================
  Coverage   59.21%   59.21%           
=======================================
  Files         288      288           
  Lines       24605    24605           
=======================================
  Hits        14571    14571           
  Misses       9159     9159           
  Partials      875      875           

go list -f {{.Dir}} ./... | xargs gofmt -w -s -d
fmt: ## run gofumpt (if present) or gofmt
@if command -v gofumpt > /dev/null; then \
gofumpt -w -d -lang=1.19 . ; \
Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like I need to add an explicit -lang=1.19, otherwise it doesn't pick up the go version for which to format. Probably because we don't have a go.mod

@thaJeztah
Copy link
Member Author

@crazy-max ptal 🤗

Copy link
Member

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

LGTM if opt-in.

Do we want a scheduled worklow to fix-up and open PR as semi-automation process?

@thaJeztah
Copy link
Member Author

Let me bring this one in 👍

@thaJeztah thaJeztah merged commit 8a19043 into docker:master Nov 4, 2022
@thaJeztah thaJeztah deleted the gofumpt_linting branch November 4, 2022 18:04
@thaJeztah thaJeztah added this to the 23.0.0 milestone May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants