golangci/golangci-lint · error

can't load config: %w

Error message

can't load config: %w

What it means

In the `fmt` command's persistentPreRunE, config.NewFormattersLoader().Load() with deprecation checks and validation enabled failed. The formatter configuration (from .golangci.yml / flags) could not be loaded, parsed, or validated, so fmt aborts before doing anything.

Source

Thrown at pkg/commands/fmt.go:87

	setupFormattersFlagSet(c.viper, fs)

	fs.BoolVarP(&c.opts.diff, "diff", "d", false, color.GreenString("Display diffs instead of rewriting files"))
	fs.BoolVar(&c.opts.diffColored, "diff-colored", false, color.GreenString("Display diffs instead of rewriting files (with colors)"))
	fs.BoolVar(&c.opts.stdin, "stdin", false, color.GreenString("Use standard input for piping source files"))

	c.cmd = fmtCmd

	return c
}

func (c *fmtCommand) persistentPreRunE(cmd *cobra.Command, args []string) error {
	c.log.Infof("%s", c.buildInfo.String())

	loader := config.NewFormattersLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts.LoaderOptions, c.cfg, args)

	err := loader.Load(config.LoadOptions{CheckDeprecation: true, Validation: true})
	if err != nil {
		return fmt.Errorf("can't load config: %w", err)
	}

	return nil
}

func (c *fmtCommand) preRunE(_ *cobra.Command, _ []string) error {
	if c.cfg.GetConfigDir() != "" && c.cfg.Version != "2" {
		return fmt.Errorf("invalid version of the configuration: %q", c.cfg.Version)
	}

	metaFormatter, err := goformatters.NewMetaFormatter(c.log, &c.cfg.Formatters, &c.cfg.Run)
	if err != nil {
		return fmt.Errorf("failed to create meta-formatter: %w", err)
	}

	matcher := processors.NewGeneratedFileMatcher(c.cfg.Formatters.Exclusions.Generated)

	opts, err := goformat.NewRunnerOptions(c.cfg, c.opts.diff, c.opts.diffColored, c.opts.stdin)

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Read the wrapped cause: it names the exact config key or parse error; fix that entry in .golangci.yml.
  2. Run `golangci-lint config verify` to validate the config file against the JSON schema.
  3. Remove or replace deprecated options flagged by the deprecation check.
  4. Ensure the correct config file is being picked up (check --config flag and search paths).

Example fix

// before (.golangci.yml)
formatters:
  gofmt:
    enble: true   # misspelled key
// after
formatters:
  gofmt:
    enable: true
Defensive patterns

Strategy: validation

Validate before calling

// validate config before running fmt
out, err := exec.Command("golangci-lint", "config", "verify").CombinedOutput()
if err != nil {
  return fmt.Errorf("invalid golangci config: %s", out)
}

Try / catch

err := fmtCmd.Run()
if err != nil {
  var cfgErr *ConfigError // or match on the wrapped cause
  if errors.As(err, &cfgErr) {
    log.Fatalf("fix .golangci.yml: %v", errors.Unwrap(err))
  }
  return err
}

Prevention

When it happens

Trigger: Running `golangci-lint fmt` with a config file containing unknown keys, deprecated settings (CheckDeprecation:true), YAML syntax errors, or values failing validation (Validation:true).

Common situations: Malformed YAML; using linter-oriented keys that are invalid for formatters config; leftover deprecated options after upgrading golangci-lint; wrong --config path; schema violations like wrong types in `formatters:` section.

Related errors


AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02). Data as JSON: /api/errors/400374c1b1df8584. Report an issue: GitHub.