golangci/golangci-lint · error

can't load config: %w

Error message

can't load config: %w

What it means

The `formatters` command's preRunE loads configuration via config.NewFormattersLoader().Load() with validation enabled. Failure is wrapped as "can't load config". It means the config file/flags for the formatters command are unparseable or fail validation, so the command can't build its lintersdb manager.

Source

Thrown at pkg/commands/formatters.go:77

	fs.SortFlags = false // sort them as they are defined here

	setupConfigFileFlagSet(fs, &c.opts.LoaderOptions)

	setupFormattersFlagSet(c.viper, fs)

	fs.BoolVar(&c.opts.JSON, "json", false, color.GreenString("Display as JSON"))

	c.cmd = formattersCmd

	return c
}

func (c *formattersCommand) preRunE(cmd *cobra.Command, args []string) error {
	loader := config.NewFormattersLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts.LoaderOptions, c.cfg, args)

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

	dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
		lintersdb.NewLinterBuilder(), lintersdb.NewPluginModuleBuilder(c.log), lintersdb.NewPluginGoBuilder(c.log))
	if err != nil {
		return err
	}

	c.dbManager = dbManager

	return nil
}

func (c *formattersCommand) execute(_ *cobra.Command, _ []string) error {
	enabledLintersMap, err := c.dbManager.GetEnabledLintersMap()
	if err != nil {
		return fmt.Errorf("can't get enabled formatters: %w", err)
	}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Read the wrapped cause for the exact offending key or line and fix the config file.
  2. Run `golangci-lint config verify` to schema-check the config.
  3. Remove unknown or deprecated options.
  4. Confirm the intended config file is being loaded (check --config and current directory).

Example fix

// before (.golangci.yml)
formatters:
  gofumpt:
    extra-rules: "yes"  # wrong type
// after
formatters:
  gofumpt:
    extra-rules: true
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

if err := formattersCmd.Run(); err != nil {
  if strings.Contains(err.Error(), "can't load config") {
    log.Fatalf("fix config file: %v", errors.Unwrap(err))
  }
  return err
}

Prevention

When it happens

Trigger: Running `golangci-lint formatters` with a config file containing YAML errors, unknown/invalid keys, or values that fail validation.

Common situations: Malformed .golangci.yml; invalid entries in the formatters section; wrong --config path; deprecated/renamed keys after a version upgrade.

Related errors


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