Go "goimports" CI Diff Failures - Fix Import Formatting
A CI gate runs goimports and finds it would rewrite a file - imports are unsorted, an unused import lingers, or a needed import is missing. goimports is a superset of gofmt that also manages the import block, so its diff fails the build.
What this error means
A goimports -l (or -d) step prints files or a diff and exits nonzero. The failure is purely about the import block - ordering, grouping, or unused/missing imports - that goimports would fix.
$ goimports -l .
internal/api/client.go
$ goimports -d internal/api/client.go
- "fmt"
"net/http"
+
+ "github.com/org/app/internal/log"Common causes
Imports not sorted or grouped
goimports orders and groups imports (stdlib, then third-party). Hand-edited or merge-mangled import blocks differ from that canonical order.
An unused or missing import
goimports removes unused imports and can add ones the code references, so leftover or absent import lines produce a diff.
How to fix it
Run goimports and commit
Let goimports rewrite the import blocks across the tree, then commit.
goimports -w .
git add -u && git commit -m "goimports"Gate on goimports in CI
test -z "$(goimports -l .)" || { goimports -d .; exit 1; }How to prevent it
- Run
goimports -w .(or format-on-save with goimports) before committing. - Keep a
goimports -lgate in CI. - Configure editors to use goimports for Go files.