Go "errors parsing go.mod" - Fix a Malformed go.mod in CI
Go could not parse go.mod at all. An unknown directive, leftover merge-conflict markers, or invalid syntax makes the file unreadable, so every command fails before any build work begins.
What this error means
A go command fails immediately with errors parsing go.mod: and a line number describing the problem - an unexpected token, an unknown directive, or a conflict marker. Nothing builds because the module file itself is broken.
go: errors parsing go.mod:
/app/go.mod:7: unknown directive: requrie
/app/go.mod:12: unexpected input: <<<<<<< HEADCommon causes
Leftover merge-conflict markers
A bad merge left <<<<<<<, =======, or >>>>>>> markers in go.mod, which the parser cannot read.
An unknown or misspelled directive
A hand-edited typo (requrie for require, a stray keyword) is not a directive Go recognizes, so parsing stops.
Invalid syntax in a block
A malformed require/replace block - a missing version, an unclosed parenthesis - makes the line unparseable.
How to fix it
Resolve merge conflicts in go.mod
- Open go.mod at the reported line and remove every conflict marker.
- Keep the correct require/replace lines from both sides.
- Run
go mod tidyso the file is reconciled and valid.
Let go mod edit rewrite the file cleanly
Using go mod edit writes directives in the exact format Go expects, fixing typos and formatting.
go mod edit -fmt
go mod tidyValidate go.mod in CI
go mod edit -json > /dev/null # nonzero exit if go.mod is unparseableHow to prevent it
- Resolve merge conflicts in go.mod carefully and re-tidy afterward.
- Edit go.mod with
go mod edit, not by hand. - Add a parse check (
go mod edit -json) to CI.