golang/go · error

error loading go.mod

Error message

error loading go.mod:
%s:%d: %v

What it means

Outside workspace mode, a 'godebug' directive in a go.mod failed CheckGodebug. Reported with the go.mod path and the directive's line number; multiple bad godebug lines accumulate into errs. This is the go.mod analog of error 1066 (which is for go.work).

Solutions

  1. Edit go.mod at the reported line and fix the key=value pair (no spaces/commas, known key).
  2. Remove the godebug directive if the setting is no longer relevant.
  3. Upgrade the toolchain to a version that still recognizes the key (if it was removed).
  4. Re-run after editing; further godebug lines may be reported in subsequent batches.

Example fix

// before (go.mod line 12)
godebug unknownkey=1   // unknown godebug "unknownkey"

// after
godebug panicnil=0      // a real, known godebug key
Defensive patterns

Strategy: validation

Validate before calling

// Validate godebug directives in go.mod.
data, _ := os.ReadFile("go.mod")
f, _ := modfile.Parse("go.mod", data, nil)
for _, g := range f.Godebug {
    if err := modload.CheckGodebug("godebug", g.Key, g.Value); err != nil {
        return fmt.Errorf("go.mod godebug %s=%s: %w", g.Key, g.Value, err)
    }
}

Try / catch

out, err := exec.Command("go", "build", "./...").CombinedOutput()
if err != nil && bytes.Contains(out, []byte("error loading go.mod")) && bytes.Contains(out, []byte("godebug")) {
    return fmt.Errorf("invalid godebug in go.mod; fix at reported line: %s", out)
}
return err

Prevention

When it happens

Trigger: A module's go.mod has a 'godebug' line whose key/value is malformed or references an unknown/removed setting; ld is NOT in workspace mode so this branch (not the go.work one) runs.

Common situations: Module authored against a different Go version that knew a now-removed godebug key; typo in the directive; GODEBUG env value mistakenly committed into go.mod with the wrong format.

Related errors


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

Appendix: source

Thrown at src/cmd/go/internal/modload/init.go:1066

			continue
		}
		if ld.inWorkspaceMode() && !strings.HasPrefix(cfg.CmdName, "work ") {
			// Refuse to use workspace if its go version is too old.
			// Disable this check if we are a workspace command like work use or work sync,
			// which will fix the problem.
			mv := gover.FromGoMod(f)
			wv := gover.FromGoWork(workFile)
			if gover.Compare(mv, wv) > 0 && gover.Compare(mv, gover.GoStrictVersion) >= 0 {
				errs = append(errs, errWorkTooOld(gomod, workFile, mv))
				continue
			}
		}

		if !ld.inWorkspaceMode() {
			ok := true
			for _, g := range f.Godebug {
				if err := CheckGodebug("godebug", g.Key, g.Value); err != nil {
					errs = append(errs, fmt.Errorf("error loading go.mod:\n%s:%d: %v", base.ShortPath(gomod), g.Syntax.Start.Line, err))
					ok = false
				}
			}
			if !ok {
				continue
			}
		}

		modFiles = append(modFiles, f)
		mainModule := f.Module.Mod
		mainModules = append(mainModules, mainModule)
		indices = append(indices, indexModFile(data, f, mainModule, fixed))

		if err := module.CheckImportPath(f.Module.Mod.Path); err != nil {
			if pathErr, ok := err.(*module.InvalidPathError); ok {
				pathErr.Kind = "module"
			}
			errs = append(errs, err)

View on GitHub (pinned to b6b368adc5)