Go "cannot find package ... in GOPATH" - Fix GO111MODULE in CI
Go is resolving imports against GOPATH instead of modules, and the package is not on disk there. GO111MODULE=off (or a build outside any module) forced legacy GOPATH mode, where dependencies must be physically present under $GOPATH/src.
What this error means
A build fails with cannot find package "github.com/org/lib" in any of: followed by GOROOT and GOPATH directories. The same code builds in module mode - the failure is GOPATH resolution looking for code that was never go get-ed into GOPATH.
main.go:5:2: cannot find package "github.com/org/lib" in any of:
/usr/local/go/src/github.com/org/lib (from $GOROOT)
/home/runner/go/src/github.com/org/lib (from $GOPATH)Common causes
GO111MODULE=off forcing GOPATH mode
With modules disabled, Go looks for every import under $GOPATH/src. A dependency managed by go.mod is not there, so it cannot be found.
Building outside a module
Running a build from a directory with no go.mod (and an environment that disables module auto-detection) drops Go into GOPATH resolution.
How to fix it
Re-enable module mode
Turn modules back on so Go resolves imports through go.mod and the module cache.
export GO111MODULE=on
go env GO111MODULE # confirm it is on/auto, not off
go build ./...Build from the module root
- Ensure a
go.modexists at the repo (or service) root. - Run the build from that directory so module mode activates.
- Check
go env GOMODpoints at your go.mod, not/dev/null.
How to prevent it
- Leave
GO111MODULEat its default; do not set itoffin CI. - Always build from the module root.
- Keep a committed go.mod so module mode is unambiguous.