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

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 ""does not contain main module""?
CI invoked go from a parent or sibling directory that is outside the module root.
How do I fix "does not contain main module"?
Run from the module root

Related guides

References

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