golang/go · error

error reading %s: invalid module path: %q

Error message

error reading %s: invalid module path: %q

What it means

Returned by modload.ReadModFile when the module directive is present but CheckReservedModulePath rejects its path — the path uses a reserved segment the toolchain forbids for user modules (e.g. a path element that is a standard-library name or a reserved keyword).

Source

Thrown at src/cmd/go/internal/modload/modfile.go:72

				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
}

func shortPathErrorList(err error) error {
	if el, ok := errors.AsType[modfile.ErrorList](err); ok {
		for i := range el {
			el[i].Filename = base.ShortPath(el[i].Filename)
		}
	}
	return err
}

// A modFileIndex is an index of data corresponding to a modFile
// at a specific point in time.
type modFileIndex struct {
	data         []byte

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Rename the module path to avoid reserved elements: run 'go mod edit -module=<newpath>' with a valid domain-prefixed path.
  2. Check each path segment against module path rules (no 'std', 'cmd', 'internal' at the root, valid DNS-style first element).
  3. Re-run the failing 'go' command after editing.

Example fix

// before (go.mod)
module std
// error: error reading go.mod: invalid module path: "std"

// after
module example.com/mystd
$ go mod edit -module=example.com/mystd
Defensive patterns

Strategy: validation

Validate before calling

// Validate the module path before writing go.mod:
//   err := module.CheckPath("example.com/mymod")
//   if err != nil { /* choose a different path */ }
// Also avoid reserved segments: std, cmd, internal (at root), builtin.

Type guard

func isValidModulePath(path string) error {
    return module.CheckPath(path)
}

Prevention

When it happens

Trigger: Declaring a module path whose path elements collide with reserved names such as 'std', 'cmd', 'internal', 'builtin', or element names disallowed by module.CheckPath.

Common situations: Naming a module 'std', 'cmd', or containing a path component the module-path validator rejects; copying a path that includes a reserved segment.

Related errors


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