Skip to content
Latchkey

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.

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)

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

  1. Ensure a go.mod exists at the repo (or service) root.
  2. Run the build from that directory so module mode activates.
  3. 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 ""cannot find package ... in GOPATH""?
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.
How do I fix "cannot find package ... in GOPATH"?
Turn modules back on so Go resolves imports through go.mod and the module cache.

Related guides

References

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