golang/go · error

error loading go.work: %s:%d: %w

Error message

error loading go.work:
%s:%d: %w

What it means

A 'godebug' directive in go.work failed CheckGodebug validation (key/value format). The error is wrapped with file path and line of the offending godebug line so the user can locate it. Distinct from the same check applied to go.mod (error 1070).

Source

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

		return nil, nil, err
	}
	seen := map[string]bool{}
	for _, d := range wf.Use {
		modRoot := d.Path
		if !filepath.IsAbs(modRoot) {
			modRoot = filepath.Join(workDir, modRoot)
		}

		if seen[modRoot] {
			return nil, nil, fmt.Errorf("error loading go.work:\n%s:%d: path %s appears multiple times in workspace", base.ShortPath(path), d.Syntax.Start.Line, modRoot)
		}
		seen[modRoot] = true
		modRoots = append(modRoots, modRoot)
	}

	for _, g := range wf.Godebug {
		if err := CheckGodebug("godebug", g.Key, g.Value); err != nil {
			return nil, nil, fmt.Errorf("error loading go.work:\n%s:%d: %w", base.ShortPath(path), g.Syntax.Start.Line, err)
		}
	}

	return wf, modRoots, nil
}

// ReadWorkFile reads and parses the go.work file at the given path.
func ReadWorkFile(path string) (*modfile.WorkFile, error) {
	path = base.ShortPath(path) // use short path in any errors
	workData, err := fsys.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("reading go.work: %w", err)
	}

	f, err := modfile.ParseWork(path, workData, nil)
	if err != nil {
		return nil, fmt.Errorf("errors parsing go.work:\n%w", err)
	}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Open go.work at the reported line and correct the key/value (one key=value per line, no spaces or commas).
  2. Cross-check the key against 'go doc runtime.GODEBUG' / the official godebug list; remove unknown keys.
  3. For default=, ensure the value is 'go<VERSION>' not exceeding the toolchain version.
  4. Run 'go work sync' or 'go work edit' to re-emit a cleaned go.work if hand-editing is error-prone.

Example fix

// before (go.work line 5)
godebug my setting=value     // key has a space
// error loading go.work: ...: key contains space

// after
godebug mysetting=value
Defensive patterns

Strategy: validation

Validate before calling

// Validate godebug entries in go.work before building.
data, _ := os.ReadFile("go.work")
wf, _ := modfile.ParseWork("go.work", data, nil)
for _, g := range wf.Godebug {
    if err := modload.CheckGodebug("godebug", g.Key, g.Value); err != nil {
        return fmt.Errorf("go.work 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.work")) && bytes.Contains(out, []byte("godebug")) {
    // point the user at the offending line; do not retry blindly
    return fmt.Errorf("invalid godebug in go.work; fix line then rebuild: %s", out)
}
return err

Prevention

When it happens

Trigger: go.work has a 'godebug' line whose key or value violates CheckGodebug (whitespace, comma, unknown key, malformed default=goVERSION, etc.). ReadWorkFile/WorkDir flow calls CheckGodebug per entry and reports at g.Syntax.Start.Line.

Common situations: Typo in a godebug key; copy-pasting a GODEBUG env var verbatim into a godebug directive (which uses '=' not '='); referencing a godebug setting removed in the current toolchain.

Related errors


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