Go "cannot find package ... in GOPATH" - Fix GO111MODULE in CI
By Daniel Zoghalchali·Latchkey
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.
go build output
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)
Diagnose it: module path, proxy, or checksum?
Go module errors name the module but rarely the layer that failed. Separate the three: the module path does not resolve, the proxy cannot serve it, or the checksum database disagrees with what was downloaded.
Terminal
# what Go resolves and from where
go env GOPROXY GOSUMDB GOPRIVATE GOFLAGS
# does the module resolve at all, bypassing the build?
go list -m -versions github.com/org/module
# verify the module cache against go.sum
go mod verify
# private modules must be excluded from proxy and sumdb
go env -w GOPRIVATE=github.com/yourorg/*
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.
Terminal
export GO111MODULE=on
go env GO111MODULE # confirm it is on/auto, not off
go build ./...
Build from the module root
Ensure a go.mod exists at the repo (or service) root.
Run the build from that directory so module mode activates.
Check go env GOMOD points at your go.mod, not /dev/null.
How to prevent it
Leave GO111MODULE at its default; do not set it off in CI.
Always build from the module root.
Keep a committed go.mod so module mode is unambiguous.
Frequently asked questions
What causes Go "cannot find package ... in GOPATH"?
There are 2 common causes: go111module=off forcing gopath mode and building outside a module. With modules disabled, Go looks for every import under $GOPATH/src.
How do I fix Go "cannot find package ... in GOPATH"?
There are 2 fixes depending on which cause you have: re-enable module mode and build from the module root. Work through them in order, since the first is the most common.
What does Go "cannot find package ... in GOPATH" actually mean?
A build fails with cannot find package "github.com/org/lib" in any of: followed by GOROOT and GOPATH directories.
How do I stop Go "cannot find package ... in GOPATH" happening again?
Leave GO111MODULE at its default; do not set it off in CI. The prevention section lists 3 changes that keep it from recurring.