Go "go.mod file not found in current directory" - Fix in CI
A go command ran in a directory that has no go.mod in it or in any parent. Without a module root in scope, Go refuses before doing any work.
What this error means
A command stops immediately with go.mod file not found in current directory or any parent directory. In CI this almost always means the job ran from the wrong working directory, or before checkout placed the module files.
go: go.mod file not found in current directory or any parent directory;
see 'go help modules'Common causes
Command run from the wrong directory
CI executed go build from above the module root, or from an unrelated subdirectory, so no go.mod is in scope up the tree.
Module in a monorepo subfolder
The go.mod lives in a subdirectory but the job ran from the repo root, where there is no module file.
Checkout had not completed
A go command ran before actions/checkout placed the repository, so the working directory was empty.
How to fix it
Run from the module root
- Change into the directory that contains go.mod before any go command.
- For a monorepo, set working-directory on the run step.
- run: go build ./...
working-directory: serviceConfirm checkout ran first
- Place actions/checkout before any setup-go or go step.
- List the directory to confirm go.mod is present.
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- run: ls go.mod && go build ./...How to prevent it
- Set working-directory explicitly for modules that live in subfolders.
- Always run actions/checkout before any go command.
- Add a
ls go.modsmoke check at the top of the job.