Skip to content
Latchkey

Go "directory prefix does not contain main module" - Fix in CI

A package pattern like ./... only matches paths under the main module. When the working directory is above or outside the module root, the pattern matches a directory the module does not own.

What this error means

A command fails with directory prefix . does not contain main module or its selected dependencies. CI ran go from a directory that is not inside the module.

go
go: directory prefix . does not contain main module or its selected dependencies

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

Command run from the wrong directory

CI invoked go from a parent or sibling directory that is outside the module root.

go.mod in a subdirectory

The module lives in a subfolder but the workflow ran go from the repo root where there is no go.mod.

How to fix it

Run from the module root

  1. Change into the directory that holds go.mod before running go commands.
.github/workflows/ci.yml
- run: go build ./...
  working-directory: ./service

Confirm the module root

  1. Print where Go thinks the module root is to verify the path.
shell
go env GOMOD

How to prevent it

  • Set working-directory to the module root in CI steps.
  • Keep one clear module root per build job.
  • Use go env GOMOD to confirm the module before building.

Frequently asked questions

What causes Go "directory prefix does not contain main module"?
There are 2 common causes: command run from the wrong directory and go.mod in a subdirectory. CI invoked go from a parent or sibling directory that is outside the module root.
How do I fix Go "directory prefix does not contain main module"?
There are 2 fixes depending on which cause you have: run from the module root and confirm the module root. Work through them in order, since the first is the most common.
What does Go "directory prefix does not contain main module" actually mean?
A command fails with directory prefix .
How do I stop Go "directory prefix does not contain main module" happening again?
Set working-directory to the module root in CI steps. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card