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

  1. Read the file:line prefix in the wrapped error list to find the exact offending line in go.mod.
  2. Run 'go mod edit -print' or 'go mod tidy' to auto-fix trivial formatting issues, or 'go mod edit -fmt' to normalize.
  3. Correct or remove the malformed directive by hand, then re-run the command.
  4. 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

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


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/dc5c10d1b955cc3e. Report an issue: GitHub.