-
Notifications
You must be signed in to change notification settings - Fork 35
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
Reorganize go imports to be more consistent #8593
Conversation
It'd be great to see something like this. I'll fix imports on files I touch sometimes, but sometimes don't if I know others are in that file too because it'll cause more merge conflicts. If we could have it automated, that would be better. I wonder if instead of using it through |
…t problems properly); made makefile target to reformat instead
@felipe-lee I used |
I tried it locally and we'd need to also add an exception for skipping .PHONY: fix_go_imports
fix_go_imports: bin/gci
find . -name '*.go' -not -path "./pkg/gen/*" -not -path "./pkg/assets/*" -not -path "*/*mocks/*" -not -path "./.gopath/*" \
-exec gci write --Section Standard --Section Default --Section "Prefix(github.com/transcom/mymove)" {} \; I specifically added It works pretty quickly though so that's awesome. I wonder if making it a script would be better so that we can run it with mymove/.pre-commit-config.yaml Lines 59 to 66 in 996a043
mymove/scripts/pre-commit-go-custom-linter Lines 1 to 11 in 996a043
|
Yes, that would be much better! I'm not sure why I wasn't thinking of that before; maybe I got my focus tied up on the |
I really like this! I wonder if it could be better to just supply a list of allowed top level directories? e.g. we only have go code in |
I just pushed a change to hopefully do this: mymove/.pre-commit-config.yaml Lines 69 to 70 in 63adf48
|
I just noticed it's failing in CI now at the |
Fwiw, I tried both versions of the pre-commit hook you set up (pointing at all go files vs being explicit about cmd and pkg) and both worked for me and updated the same files (afict anyways...it's a lot of files...) |
Closing in lieu of #8940. |
Summary
I've noticed that the order of our go imports has seemed a little random and getting more so over time. Ideally, the imports would be organized as follows (alphabetically within a section):
However, I think when a blank line is added between imports,
goimports
won't regroup the grouped imports to be with other similar groups. In this PR, I used the gci utility to do the reorganization which eliminates extra sections and groups like I think we want.Note that a couple of the commits (first, second) fix a few duplicate imports (either alias vs. no alias or "." vs no ".") that caused issues like
gci
andgoimports
fighting against each other (goimports
still runs as part of our pre-commit hooks).I tried to add
gci
to ourgolangci-lint
list of linters to run. However,gci
won't automatically fix the import order right now due to recent changes in the library (see this golangci-lint issue). It will fail if there is an issue with the import order, but the error message makes it less than obvious what's happening (see this gci issue). So turning it on may cause more confusion than it solves at this point.So for now, I've created a script that uses the
gci
utility directly (in write mode) and set uppre-commit
to run non-generated Go code through it for consistent formatting. That said, I'd rather have this work at thegolangci-lint
level like our other formatters, so maybe we can move to that at some point when the above issues have been addressed.To test it, try changing the order of imports on a file or two, do a
git add
on them, then run:pre-commit run fix-go-imports
To try it on all files (it shouldn't touch generated files),
pre-commit run -a fix-go-imports