Go "cannot find main module" - Fix in CI
A go command needs a main module to operate on. When the working directory has no go.mod in it or any parent, Go has no module in scope and refuses.
What this error means
A command stops with "go: cannot find main module; see go help modules". In CI this usually means the job ran from the wrong directory, before checkout, or in a workspace where no module is active.
go: cannot find main module, but found .git/config in /home/runner/work/app
to create a module there, run:
go mod initCommon causes
Ran outside the module root
The go command executed from above the module or in an unrelated folder with no go.mod up the tree.
Checkout had not placed go.mod
A go command ran before actions/checkout, so the directory had no module files yet.
How to fix it
Run from the module root
- Set working-directory to the folder that contains go.mod.
- Confirm go.mod is present before any go command.
- run: ls go.mod && go build ./...
working-directory: serviceUse a workspace when spanning modules
- For multi-module repos, point GOWORK at the go.work file so a main module is in scope.
export GOWORK=$PWD/go.work
go build ./...How to prevent it
- Set working-directory explicitly for modules in subfolders.
- Always run actions/checkout before any go command.
- Add a
ls go.modsmoke check at the top of the job.