-
Notifications
You must be signed in to change notification settings - Fork 95
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
Enforce consistent Go import organization using goimports
#8198
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - would be good to get some reviews from other folks as well before merging
btw, for vscode, I had to add this to my settings.json to get goimports-on-save to work properly:
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
},
we could maybe add this to our vscode setup guide in the internal site (https://github.com/buildbuddy-io/buildbuddy-internal/blob/master/website/docs/development-environment.md)
BUILD
Outdated
alias( | ||
name = "goimports", | ||
actual = "@org_golang_x_tools//cmd/goimports", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
genrule( | ||
name = "goimports_bin", | ||
outs = ["goimports.sh"], | ||
cmd = """ | ||
echo '#!/bin/bash' > $@ | ||
echo 'exec $(location @org_golang_x_tools//cmd/goimports) "$$@"' >> $@ | ||
chmod +x $@ | ||
""", | ||
tools = ["@org_golang_x_tools//cmd/goimports"], | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit - instead of using $(location)
does it work if we run @org_golang_x_tools//cmd/goimports
directly, similar to how we run @npm//prettier/bin:prettier
which is also an external dep?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That absolutely works! Thanks. Lol, goimports.sh
was failing to find goimports
when run in CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch that. What pushed me to writing the goimports.sh
rule in the first place is that running bazel run //:goimports
would execute outside the current working directory. I don't know how to get the absolute path of goimports
without calling $(location ...).
…pendency of checkstyle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mostly just care about not having to think about stuff like this too much, so this seems like a good change in that regard. That said, we have historically grouped inputs into three groups: library-level ones (like "fmt"), unaliased imports, and then aliased imports, with each group separated by an empty line, like this:
Lines 4 to 25 in 3567984
"bytes" | |
"context" | |
"flag" | |
"os" | |
"strings" | |
"time" | |
"github.com/buildbuddy-io/buildbuddy/server/remote_cache/cachetools" | |
"github.com/buildbuddy-io/buildbuddy/server/remote_cache/digest" | |
"github.com/buildbuddy-io/buildbuddy/server/util/grpc_client" | |
"github.com/buildbuddy-io/buildbuddy/server/util/log" | |
"github.com/buildbuddy-io/buildbuddy/server/util/proto" | |
"github.com/buildbuddy-io/buildbuddy/server/util/status" | |
"github.com/mattn/go-isatty" | |
"google.golang.org/grpc/metadata" | |
"google.golang.org/protobuf/encoding/protojson" | |
bbspb "github.com/buildbuddy-io/buildbuddy/proto/buildbuddy_service" | |
capb "github.com/buildbuddy-io/buildbuddy/proto/cache" | |
repb "github.com/buildbuddy-io/buildbuddy/proto/remote_execution" | |
rspb "github.com/buildbuddy-io/buildbuddy/proto/resource" | |
bspb "google.golang.org/genproto/googleapis/bytestream" |
Do you know if there's a way to get goimports to do that automatically?
I'm cool with this if that's the diff! 2 minor questions/nits I have are:
import (
builtins
custom packages
alias ... aliased packages
) I don't think goimports will break this, but I have seen some editors group aliases and non-aliased stuff together which is 🤮 This change won't break anything here, right? |
As mentioned in your other PR, I would manually alias the |
That's what the Google Style Guide recommends: https://google.github.io/styleguide/go/decisions.html#imports so seems reasonable to me that we just adopt that convention. |
run GoFormat \ | ||
"$GOFMT_PATH" -d . | ||
|
||
run GoImports \ | ||
"$GOIMPORTS_PATH" -d . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: https://pkg.go.dev/golang.org/x/tools/cmd/goimports
In addition to fixing imports, goimports also formats your code in the same style as gofmt so it can be used as a replacement for your editor's gofmt-on-save hook.
So we can replace GoFormat with GoImports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to test it, but yes, good point!
Co-authored-by: Fabian Meumertzheim <[email protected]>
Does seem wrong. This blog post mentions packages named after major versions, saying that you should just use the real package name. Not sure why If I explicitly alias to
|
@iain-macdonald (VSCode) and I (Zed) noticed our editors making seemingly-superfluous changes to Go imports on save. Turns out this is due to
gopls
running import organization logic.It would be nice to consistently organize imports across the codebase the same way we consistently format Go code using
gofmt
. Luckily there's a tool that can do just that!goimports
This change adds
goimports
tobuildfix.sh
and tocheckstyle.sh
. Runningbuildfix.sh
on a recent checkout touches 4 files. (Diff at the bottom). I can of course add these changes to this pull request if we want to move forward.The downside of introducing this change is that people's editors may not adhere to
goimports
organization or may actively fight it. I don't want to cause headaches!