golangci/golangci-lint · error

failed to encode configuration: %w

Error message

failed to encode configuration: %w

What it means

getConfig converts the golangci-lint revive settings struct into a revive lint.Config by encoding it as TOML and decoding it back. If toml.NewEncoder(buf).Encode(rawRoot) fails — the settings map contains values TOML cannot represent — the encoder error is wrapped as "failed to encode configuration" and revive initialization fails.

Source

Thrown at pkg/golinters/revive/revive.go:188

// This function mimics the GetConfig function of revive.
// This allows to get default values and right types.
// https://github.com/golangci/golangci-lint/issues/1745
// https://github.com/mgechev/revive/blob/v1.13.0/config/config.go#L249
// https://github.com/mgechev/revive/blob/v1.13.0/config/config.go#L198-L204
func getConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
	conf := defaultConfig()

	// Since the Go version is dynamic, this value must be neutralized in order to compare with a "zero value" of the configuration structure.
	zero := &config.ReviveSettings{Go: cfg.Go}

	if !reflect.DeepEqual(cfg, zero) {
		rawRoot := createConfigMap(cfg)
		buf := bytes.NewBuffer(nil)

		err := toml.NewEncoder(buf).Encode(rawRoot)
		if err != nil {
			return nil, fmt.Errorf("failed to encode configuration: %w", err)
		}

		conf = &lint.Config{}
		_, err = toml.NewDecoder(buf).Decode(conf)
		if err != nil {
			return nil, fmt.Errorf("failed to decode configuration: %w", err)
		}
	}

	normalizeConfig(conf)

	for k, r := range conf.Rules {
		err := r.Initialize()
		if err != nil {
			return nil, fmt.Errorf("error in config of rule %q: %w", k, err)
		}
		conf.Rules[k] = r
	}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Simplify/fix linters-settings.revive to use documented types: rules as name→options maps with scalar values.
  2. Update golangci-lint (and its bundled revive) to the latest version — encoder incompatibilities are often fixed upstream.
  3. Reduce the revive config to a minimal set of settings and re-add entries until the failing one is identified.
  4. Report the inner %w cause upstream if a supported setting fails to encode.

Example fix

# before
linters-settings:
  revive:
    rules:
      - name: var-naming
        arguments: ["wrongPattern"]
# after (valid documented shape)
linters-settings:
  revive:
    rules:
      - name: var-naming
        arguments:
          - maxLength: 40
Defensive patterns

Strategy: validation

Validate before calling

// ensure revive settings only contain TOML-encodable values (scalars, arrays, string-keyed maps)
func tomlSafe(v reflect.Value) error {
    switch v.Kind() {
    case reflect.Map:
        if v.Type().Key().Kind() != reflect.String {
            return errors.New("maps must have string keys for TOML")
        }
    case reflect.Slice, reflect.Array, reflect.Struct, reflect.Ptr:
        for i := 0; i < v.Len(); i++ {
            if err := tomlSafe(v.Index(i)); err != nil { return err }
        }
    case reflect.String, reflect.Bool, reflect.Int, reflect.Float64, reflect.Interface:
    default:
        return fmt.Errorf("unsupported kind %s", v.Kind())
    }
    return nil
}

Try / catch

conf, err := getConfig(cfg)
if err != nil {
    var encErr error
    if errors.As(err, &encErr) { log.Printf("revive config not encodable: %v", encErr) }
    // fall back to default revive config
}

Prevention

When it happens

Trigger: createConfigMap(cfg) produces a structure containing a type not encodable by the TOML writer (e.g. unsupported map key type, cyclic/odd nested values) for a non-zero revive settings struct, inside getConfig called from newWrapper.

Common situations: Exotic values in linters-settings.revive (custom rule configurations, unusual nested maps) that don't round-trip through TOML; golangci-lint/revive version mismatches changing config shapes; hand-edited configs with wrong types (string where number/table expected).

Related errors


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