Skip to content
Latchkey

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.

CI log
$ 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.

Terminal
goimports -w .
git add -u && git commit -m "goimports"

Gate on goimports in CI

.github/workflows/ci.yml
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 -l gate in CI.
  • Configure editors to use goimports for Go files.

Frequently asked questions

What causes ""goimports" diff"?
goimports orders and groups imports (stdlib, then third-party). Hand-edited or merge-mangled import blocks differ from that canonical order.
How do I fix "goimports" diff?
Let goimports rewrite the import blocks across the tree, then commit.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card