Go "cannot find GOROOT directory" in CI - Fix the build
GOROOT is where the Go toolchain lives. If it points at a directory that does not exist, the go command cannot find its own standard library and refuses to run.
What this error means
A go command fails immediately with go: cannot find GOROOT directory: /usr/local/go. It usually means a hand-set GOROOT env var is stale, or a cached path no longer matches the installed toolchain.
go: cannot find GOROOT directory: /usr/local/goCommon causes
Stale GOROOT environment variable
A pinned GOROOT points at an install path that the current runner image does not have.
Cached path from a different setup
A cache or earlier step set GOROOT to a location the actual Go install no longer uses.
How to fix it
Stop hard-coding GOROOT
- Remove any manual GOROOT export and let setup-go configure it.
- uses: actions/setup-go@v5
with:
go-version: '1.22'
# do not set GOROOT manuallyPoint GOROOT at the real install
- If you must set it, derive it from the installed go binary.
export GOROOT="$(go env GOROOT)"How to prevent it
- Let setup-go own GOROOT instead of hard-coding it.
- Avoid caching absolute toolchain paths across images.
- Use
go env GOROOTif a value is genuinely needed.