golangci/golangci-lint · error

[%s] YAML decode: %w

Error message

[%s] YAML decode: %w

What it means

After successfully opening the file, decodeYamlFile decodes it with yaml.NewDecoder(file).Decode(&m). If the document is not parseable YAML, the error is wrapped as "[%s] YAML decode: %w". This happens while validating the config against the JSON Schema in config verify.

Source

Thrown at pkg/commands/config_verify.go:104

	}

	for _, d := range detail.Errors {
		printValidationDetail(cmd, &d)
	}
}

func decodeYamlFile(filename string) (any, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, fmt.Errorf("[%s] file open: %w", filename, err)
	}

	defer func() { _ = file.Close() }()

	var m any
	err = yaml.NewDecoder(file).Decode(&m)
	if err != nil {
		return nil, fmt.Errorf("[%s] YAML decode: %w", filename, err)
	}

	return m, nil
}

func decodeTomlFile(filename string) (any, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, fmt.Errorf("[%s] file open: %w", filename, err)
	}

	defer func() { _ = file.Close() }()

	var m any
	err = toml.NewDecoder(file).Decode(&m)
	if err != nil {
		return nil, fmt.Errorf("[%s] TOML decode: %w", filename, err)
	}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Read the wrapped %w error — go-yaml reports the exact line/column of the syntax problem and fix that line
  2. Replace tab characters with spaces for indentation
  3. Ensure a space after each colon ('key: value') and balanced quotes
  4. Remove comments from .json configs (YAML comments are not valid JSON) or rename the file to .yml
  5. Paste the file into a YAML linter/parser locally to catch remaining syntax issues

Example fix

# before
linters:
	enable:
	  - lll

# after (spaces, not tabs)
linters:
  enable:
    - lll
Defensive patterns

Strategy: validation

Validate before calling

func yamlPrecheck(data []byte) error {
    if bytes.ContainsRune(data, '\t') {
        if idx := bytes.IndexRune(data, '\t'); idx >= 0 {
            line := bytes.Count(data[:idx], []byte("\n")) + 1
            return fmt.Errorf("tab character on line %d — use spaces", line)
        }
    }
    var v any
    if err := yaml.Unmarshal(data, &v); err != nil {
        return fmt.Errorf("invalid YAML: %w", err)
    }
    return nil
}

Type guard

func isYAMLSyntaxError(err error) bool {
    var te *yaml.TypeError
    return err != nil && !errors.As(err, &te) && strings.Contains(err.Error(), "yaml:")
}

Try / catch

if err := yaml.NewDecoder(file).Decode(&m); err != nil {
    return fmt.Errorf("fix the reported line/column in %s: %w", filename, err)
}

Prevention

When it happens

Trigger: yaml.Decode fails on a .yaml/.yml/.json config file: invalid YAML syntax, tabs for indentation, duplicate keys with strict mode, or non-mapping top-level document.

Common situations: Hand-edited YAML with tab indentation; missing space after a colon; unbalanced quotes/brackets; copy-pasted config with smart quotes; a .json file containing comments (JSON with // is invalid).

Related errors


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