gohugoio/hugo · error

failed to list modules: %w

Error message

failed to list modules: %w

What it means

Returned when `go list -m -json all` (or for specific modules) fails while Hugo enumerates the module graph. Hugo relies on this listing to map imports to on-disk directories; failure means the module graph can't be built.

Source

Thrown at modules/client.go:530

		}
		return nil
	}

	if err := downloadModules(); err != nil {
		return nil, err
	}

	listAndDecodeModules := func(handle func(m *goModule) error, modules ...string) error {
		b := &bytes.Buffer{}
		args := []string{"list", "-m", "-json"}
		if len(modules) > 0 {
			args = append(args, modules...)
		} else {
			args = append(args, "all")
		}
		err := c.runGo(context.Background(), b, args...)
		if err != nil {
			return fmt.Errorf("failed to list modules: %w", err)
		}

		dec := json.NewDecoder(b)
		for {
			m := &goModule{}
			if err := dec.Decode(m); err != nil {
				if err == io.EOF {
					break
				}
				return fmt.Errorf("failed to decode modules list: %w", err)
			}

			if err := handle(m); err != nil {
				return err
			}
		}
		return nil
	}

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Run `hugo mod tidy` to repair go.mod/go.sum.
  2. Run `go list -m -json all` manually in the site dir to see the raw error.
  3. Set GOWORK=off or remove a stray go.work affecting the site directory.
  4. Ensure the installed Go version satisfies the go directive in go.mod.
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure a module file exists so 'go list -m' has something to list
if _, err := os.Stat(filepath.Join(workingDir, "go.mod")); errors.Is(err, os.ErrNotExist) {
    // run Init first or skip module listing
}

Try / catch

mods, err := client.List()
if err != nil {
    return fmt.Errorf("listing modules failed; verify go.mod is valid ('go mod verify'): %w", err)
}

Prevention

When it happens

Trigger: `go list -m -json [all|mods...]` exiting non-zero: inconsistent go.mod (missing requires), Go workspace/GOFLAGS interference, missing go.sum entries with GOFLAGS=-mod=readonly, or a go version that can't parse the go.mod go directive.

Common situations: Hand-edited go.mod, a go.work file in a parent directory pulling the site into an unrelated workspace, Go upgraded/downgraded between builds.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/8e7a7309c5ec6189.json. Report an issue: GitHub ↗.