{"record":{"id":"552a2314fe346db5","repo":"golangci/golangci-lint","slug":"yaml-decode-file-s-w","errorCode":null,"errorMessage":"YAML decode file %s: %w","messagePattern":"YAML decode file (.+?): %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/internal/migrate/parser/parser.go","lineNumber":30,"sourceCode":"\t\"github.com/pelletier/go-toml/v2\"\n\t\"go.yaml.in/yaml/v3\"\n)\n\ntype File interface {\n\tio.ReadWriter\n\tName() string\n}\n\n// Decode decodes a file into data.\n// The choice of the decoder is based on the file extension.\nfunc Decode(file File, data any) error {\n\text := filepath.Ext(file.Name())\n\n\tswitch strings.ToLower(ext) {\n\tcase \".yaml\", \".yml\", \".json\":\n\t\terr := yaml.NewDecoder(file).Decode(data)\n\t\tif err != nil && !errors.Is(err, io.EOF) {\n\t\t\treturn fmt.Errorf(\"YAML decode file %s: %w\", file.Name(), err)\n\t\t}\n\n\tcase \".toml\":\n\t\terr := toml.NewDecoder(file).Decode(&data)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"TOML decode file %s: %w\", file.Name(), err)\n\t\t}\n\n\tdefault:\n\t\treturn fmt.Errorf(\"unsupported file type: %s\", ext)\n\t}\n\n\treturn nil\n}\n\n// Encode encodes data into a file.\n// The choice of the encoder is based on the file extension.\nfunc Encode(data any, dstFile File) error {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/commands/internal/migrate/parser/parser.go#L12-L48","documentation":"parser.Decode wraps errors from yaml.NewDecoder(file).Decode(data) when a .yaml/.yml/.json configuration file cannot be parsed into the data map. An io.EOF (empty file) is intentionally tolerated. This means the config file is syntactically invalid YAML or JSON.","triggerScenarios":"Calling Decode on a file whose extension is .yaml/.yml/.json and whose content fails YAML/JSON parsing — bad indentation, tabs in YAML, unclosed quotes/brackets, duplicate keys, or invalid JSON syntax.","commonSituations":"Hand-edited .golangci.yml with tab characters or inconsistent indentation, merge-conflict markers (<<<<<<<) left in the file, or a truncated file from a failed write.","solutions":["Open the file at the reported line/column and fix the YAML/JSON syntax error (replace tabs with spaces, fix indentation).","Validate the file with `yamllint <file>` or paste it into a YAML validator.","Remove any Git merge-conflict markers left in the config.","Restore the file from version control if it is truncated or corrupted."],"exampleFix":"// before (.golangci.yml — tab indentation)\nlinters:\n\tenable:\n\t  - gocritic\n// after\nlinters:\n  enable:\n    - gocritic","handlingStrategy":"validation","validationCode":"data, err := os.ReadFile(cfgPath)\nif err != nil { return err }\nif bytes.ContainsRune(data, '\\t') { return errors.New(\"tabs are invalid YAML indentation\") }\nif bytes.Contains(data, []byte(\"<<<<<<<\")) { return errors.New(\"merge conflict markers present\") }","typeGuard":null,"tryCatchPattern":"if err := parser.Decode(f, &data); err != nil {\n    var yerr *yaml.TypeError\n    if errors.As(err, &yerr) { /* show line/type info */ }\n    if errors.Is(err, io.EOF) { /* treat as empty config */ }\n    return err\n}","preventionTips":["Configure the editor to use spaces, never tabs, in YAML.","Validate YAML/JSON with yamllint or a linter before committing.","Resolve merge conflicts in config files completely.","Keep config files under version control to restore corrupted copies."],"tags":["go","yaml","json","config","syntax"],"backgroundTag":"yaml-parse-error","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}