golang/go · error
errors parsing %s: %w
Error message
errors parsing %s: %w
What it means
Returned by modload.ReadModFile when golang.org/x/mod/modfile.Parse fails on the main module's go.mod. The error list is reformatted with short paths and wrapped. A TooNewError is returned separately only if the lax parse succeeds, so this error specifically means the go.mod is syntactically/semantically invalid even under lax parsing rules — i.e. genuinely malformed.
Source
Thrown at src/cmd/go/internal/modload/modfile.go:59
if err != nil {
return nil, nil, err
}
f, err = modfile.Parse(gomod, data, fix)
if err != nil {
f, laxErr := modfile.ParseLax(gomod, data, fix)
if laxErr == nil {
if f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 {
toolchain := ""
if f.Toolchain != nil {
toolchain = f.Toolchain.Name
}
return nil, nil, &gover.TooNewError{What: base.ShortPath(gomod), GoVersion: f.Go.Version, Toolchain: toolchain}
}
}
// Errors returned by modfile.Parse begin with file:line.
return nil, nil, fmt.Errorf("errors parsing %s:\n%w", base.ShortPath(gomod), shortPathErrorList(err))
}
if f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 {
toolchain := ""
if f.Toolchain != nil {
toolchain = f.Toolchain.Name
}
return nil, nil, &gover.TooNewError{What: base.ShortPath(gomod), GoVersion: f.Go.Version, Toolchain: toolchain}
}
if f.Module == nil {
// No module declaration. Must add module path.
return nil, nil, fmt.Errorf("error reading %s: missing module declaration. To specify the module path:\n\tgo mod edit -module=example.com/mod", base.ShortPath(gomod))
} else if err := CheckReservedModulePath(f.Module.Mod.Path); err != nil {
return nil, nil, fmt.Errorf("error reading %s: invalid module path: %q", base.ShortPath(gomod), f.Module.Mod.Path)
}
return data, f, err
}
View on GitHub (pinned to b6b368adc5)
Solutions
- Read the file:line prefix in the wrapped error list to find the exact offending line in go.mod.
- Run 'go mod edit -print' or 'go mod tidy' to auto-fix trivial formatting issues, or 'go mod edit -fmt' to normalize.
- Correct or remove the malformed directive by hand, then re-run the command.
- If caused by a merge conflict, resolve the conflict markers in go.mod first.
Example fix
// before (go.mod contains)
require (
example.com/pkg v1.2.0
// missing closing paren
// error: errors parsing go.mod:
// go.mod:5: ...
// after
require (
example.com/pkg v1.2.0
) Defensive patterns
Strategy: validation
Validate before calling
// Validate go.mod parses before running build steps: // go mod edit -print >/dev/null // Non-zero exit means go.mod is malformed; parse errors are printed with file:line.
Prevention
- Run 'go mod edit -fmt' to normalize go.mod formatting.
- Add a pre-commit hook that runs 'go mod edit -print >/dev/null'.
- Never leave merge-conflict markers in go.mod; configure your VCS to flag them.
When it happens
Trigger: Any 'go' subcommand that reads the main go.mod when that file contains a directive modfile.Parse rejects: unknown directives, malformed require/replace/exclude syntax, duplicate blocks, invalid quoted strings, or an unparseable go version.
Common situations: Hand-editing go.mod and leaving a syntax error; a merge conflict left in go.mod; a tool wrote a non-conformant directive; line endings or stray characters introduced by an editor.
Related errors
- parsing %s: %v
- %s/%s and ...%s/go.mod both have ...%s module paths at revis
- %s/%s is missing module path at revision %s
- %s/%s has non-...%s module path %q at revision %s
- %s is missing module path%s at revision %s
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/dc5c10d1b955cc3e.
Report an issue: GitHub.