Skip to content
Latchkey

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
go: cannot find main module, but found .git/config in /home/runner/work/app
	to create a module there, run:
	go mod init

Common 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

  1. Set working-directory to the folder that contains go.mod.
  2. Confirm go.mod is present before any go command.
.github/workflows/ci.yml
- run: ls go.mod && go build ./...
  working-directory: service

Use a workspace when spanning modules

  1. For multi-module repos, point GOWORK at the go.work file so a main module is in scope.
.github/workflows/ci.yml
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.mod smoke check at the top of the job.

Frequently asked questions

What causes ""cannot find main module""?
The go command executed from above the module or in an unrelated folder with no go.mod up the tree.
How do I fix "cannot find 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