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: directory prefix . does not contain main module or its selected dependenciesCommon 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
- Change into the directory that holds go.mod before running go commands.
- run: go build ./...
working-directory: ./serviceConfirm the module root
- Print where Go thinks the module root is to verify the path.
go env GOMODHow 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.