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

formatting: format code and imports with gci #2045

Open
wants to merge 4 commits into
base: stage
Choose a base branch
from

Conversation

iurii-ssv
Copy link
Contributor

@iurii-ssv iurii-ssv commented Feb 24, 2025

This PR:

  • changes make format command to use https://github.com/daixiang0/gci (it's fast and simple) for code & imports formatting, it is recommended to run it for every PR before we merge it (make lint will fail otherwise)
  • applies make format to group & sort imports according to the following order: golang-pkgs -> 3rd-party-pkgs -> ssv-spec-pkgs -> local-ssv-pkgs

Makefile Outdated
Comment on lines 165 to 167
.PHONY: format
format:
# both format commands must ignore generated files which are named *mock* or enr_fork_id_encoding.go
# the argument to gopls format can be a list of files
find . -name "*.go" ! -path '*mock*' ! -name 'enr_fork_id_encoding.go' -type f -print0 | xargs -0 -P 1 sh -c 'gopls -v format -w $0'
# the argument to gopls imports must be a single file so this entire command takes a few mintues to run
find . -name "*.go" ! -path '*mock*' ! -name 'enr_fork_id_encoding.go' -type f -print0 | xargs -0 -P 10 -I{} sh -c 'gopls -v imports -w "{}"'
gci write -s standard -s default -s blank -s dot -s localmodule -s "prefix(github.com/ssvlabs/ssv-spec)" $$(find . -type f -name '*.go' -not -path "*mock*" -not -name "*.gen.go")
Copy link
Contributor Author

@iurii-ssv iurii-ssv Feb 24, 2025

Choose a reason for hiding this comment

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

Note, this PR doesn't change enr_fork_id_encoding.go file - so I don't think we need to add it here as "an exception" (and similar holds true for SSZ-generated encodings for example),

but in the future ideally we'd want to add .gen.go suffix for every generated file so it's taken care of by this new make format automatically

Copy link
Contributor Author

@iurii-ssv iurii-ssv Feb 24, 2025

Choose a reason for hiding this comment

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

update 1: actually we don't even need .gen.go suffix(es) - we can just use --skip-generated and it's smart enough to figure out what files are generated by analysing code-comments I guess

update 2: manually-written mock(s) should be formatted (only generated once should be skipped), so now adjusted accordingly

Makefile Outdated
find . -name "*.go" ! -path '*mock*' ! -name 'enr_fork_id_encoding.go' -type f -print0 | xargs -0 -P 1 sh -c 'gopls -v format -w $0'
# the argument to gopls imports must be a single file so this entire command takes a few mintues to run
find . -name "*.go" ! -path '*mock*' ! -name 'enr_fork_id_encoding.go' -type f -print0 | xargs -0 -P 10 -I{} sh -c 'gopls -v imports -w "{}"'
gci write -s standard -s default -s blank -s dot -s localmodule -s "prefix(github.com/ssvlabs/ssv-spec)" $$(find . -type f -name '*.go' -not -path "*mock*" -not -name "*.gen.go")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need a separate block for spec? I agree it's close to the ssv node, but we also have other dependencies in ssvlabs, like eth2-key-manager (or ssv-signer in the future). I think it should belong to 3rd party dependencies

Copy link
Contributor

Choose a reason for hiding this comment

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

Does gci perform go fmt-like formatting as well?

Copy link
Contributor Author

@iurii-ssv iurii-ssv Feb 24, 2025

Choose a reason for hiding this comment

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

Why do we need a separate block for spec? I agree it's close to the ssv node, but we also have other dependencies in ssvlabs, like eth2-key-manager (or ssv-signer in the future). I think it should belong to 3rd party dependencies

hmm, maybe we don't - it's just a common dependency I thought "worth highlighting"

ssv-signer and eth2-key-manager usage is very limited while ssv-spec is common and it would be nice-to-spot right away

Does gci perform go fmt-like formatting as well?

it doesn't mention it anywhere, but as far as I can tell it does some basic code-formatting (such as removing extra spaces and so on) - I'm not sure if it's exactly matching gofmt / goimports in that sense though

I've been using it with previous projects in the past and haven't had any code-formatting issues - I guess if we discover any we can maybe add gofmt on top ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I've been using it with previous projects in the past and haven't had any code-formatting issues - I guess if we discover any we can maybe add gofmt on top ?

Sounds good

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, maybe we don't - it's just a common dependency I thought "worth highlighting"

I agree it's the most common and close dependency but as far as I know there's no common convention for another block for such repos. Also, moving it to 3rd party block should reduce the number of conflicts by a lot because it's how we are generally using it now

Copy link
Contributor Author

@iurii-ssv iurii-ssv Feb 24, 2025

Choose a reason for hiding this comment

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

I don't mind grouping it with other 3rd-party dependancies,

but lets see if @moshe-blox @y0sher @MatusKysel have preferences one way or the other (and I'll update it accordingly)

Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 47.8%. Comparing base (83e58c0) to head (041be28).

Additional details and impacted files

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nkryuchkov
Copy link
Contributor

Can we also add a linter check that all files are gci'ed?

@iurii-ssv iurii-ssv force-pushed the make-format-with-gci branch from 8c02268 to b6ad35b Compare February 24, 2025 13:34
@iurii-ssv
Copy link
Contributor Author

Can we also add a linter check that all files are gci'ed?

Yeah, that's a good idea, added now

Comment on lines 412 to +413
# Fix found issues (if it's supported by the linter)
fix: true
fix: false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe linter should be a read-only type of thing

@oleg-ssvlabs
Copy link
Contributor

I think it could be a good idea to have a CI job that checks if running make format results in any file changes. If it does, the pipeline should fail. It should run on pushes to all feature branches

@iurii-ssv
Copy link
Contributor Author

I think it could be a good idea to have a CI job that checks if running make format results in any file changes. If it does, the pipeline should fail. It should run on pushes to all feature branches

Doesn't our linter check for this already (and it runs in a github pipeline) ?

I think this is the linter setting responsible for checking against gofmt:

ssv/.golangci.yaml

Lines 173 to 175 in 041be28

gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants