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

Enforce consistent Go import organization using goimports #8198

Merged
merged 11 commits into from
Jan 23, 2025
19 changes: 19 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,25 @@ go_sdk_tool(
goroot_relative_path = "bin/gofmt",
)

alias(
name = "goimports",
actual = "@org_golang_x_tools//cmd/goimports",
visibility = ["//visibility:public"],
)

genrule(
name = "goimports_bin",
outs = ["goimports.sh"],
cmd = """
echo <<EOF > $@
#!/bin/bash
exec $(rootpath @org_golang_x_tools//cmd/goimports) "$$@"
EOF
chmod +x $@
""",
tools = ["@org_golang_x_tools//cmd/goimports"],
)

exports_files([
".swcrc",
"package.json",
Expand Down
3 changes: 3 additions & 0 deletions buildfix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ while IFS= read -r line; do
done < <(git ls-files '*.go')
bazel run "${BAZEL_QUIET_FLAGS[@]}" //:gofmt -- -w "${GO_SRCS[@]}"

echo "Building and running goimports..."
bazel run "${BAZEL_QUIET_FLAGS[@]}" //:goimports.sh -- -w "${GO_SRCS[@]}"

if which clang-format &>/dev/null; then
echo "Formatting .proto files..."
protos=()
Expand Down
2 changes: 2 additions & 0 deletions tools/checkstyle/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ sh_binary(
data = [
"//:go",
"//:gofmt",
"//:goimports",
"//:goimports.sh",
"//cli/cmd/bb",
"//tools/clang-format",
"@npm//prettier/bin:prettier",
Expand Down
4 changes: 4 additions & 0 deletions tools/checkstyle/checkstyle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -euo pipefail

GO_PATH="$(readlink ./go)"
GOFMT_PATH="$(readlink ./gofmt)"
GOIMPORTS_PATH="$(readlink ./goimports.sh)"
BB_PATH="$(readlink ./cli/cmd/bb/bb_/bb)"
PRETTIER_PATH="$(readlink ./external/npm/prettier/bin/prettier.sh)"
CLANG_FORMAT_PATH="$(readlink ./tools/clang-format/clang-format)"
Expand Down Expand Up @@ -53,6 +54,9 @@ run GoDeps \
run GoFormat \
"$GOFMT_PATH" -d .

run GoImports \
"$GOIMPORTS_PATH" -d .
Comment on lines 54 to +58
Copy link
Contributor

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

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 want to test it, but yes, good point!


run ProtoFormat \
env CLANG_FORMAT_PATH="$CLANG_FORMAT_PATH" \
tools/clang-format/clang-format.sh --dry-run
Expand Down
Loading