Skip to content
Latchkey

Go "build failed" loading test package imports - Fix in CI

Before running tests, Go loads and compiles the test package and everything it imports. A missing or wrong import there fails the load before any test runs.

What this error means

A run fails with cannot load package or [build failed] pointing at an import in a _test.go file. It means a test file imports something not provided, often a helper module not required.

go
pkg_test.go:7:2: cannot find module providing package github.com/stretchr/testify/assert;
	to add it: go get github.com/stretchr/testify/assert
FAIL	example.com/app/pkg [build failed]

Common causes

Test-only dependency not required

A test imports a package (e.g. a test helper or assertion library) that go.mod does not require.

Wrong import path in a test file

A _test.go file imports a misspelled or moved path that does not resolve.

How to fix it

Add the test dependency

  1. Run go mod tidy so test-only imports get require directives.
  2. Commit go.mod and go.sum.
Terminal
go mod tidy
go test ./...

Fix the import path

  1. Correct the import path in the test file to the real module path.
Terminal
go test -run=^$ ./...

How to prevent it

  • Run go mod tidy so test-only deps are recorded.
  • Keep test imports correct and module-qualified.
  • Compile tests in CI before running them.

Frequently asked questions

What causes "test package load failed"?
A test imports a package (e.g. a test helper or assertion library) that go.mod does not require.
How do I fix test package load failed?
Add the test dependency

Related guides

References

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