{"record":{"id":"00df09c09b1d4ba0","repo":"mikefarah/yq","slug":"configure-yaml-encoding-w","errorCode":null,"errorMessage":"configure YAML encoding: %w","messagePattern":"configure YAML encoding: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/encoder_yaml.go","lineNumber":67,"sourceCode":"\tif ye.prefs.ColorsEnabled {\n\t\tdestination = tempBuffer\n\t}\n\n\tindent := ye.prefs.Indent\n\tif indent < 2 {\n\t\tindent = 2\n\t} else if indent > 9 {\n\t\tindent = 9\n\t}\n\n\tdumper, err := yaml.NewDumper(destination,\n\t\tyaml.WithV3Defaults(),\n\t\tyaml.WithIndent(indent),\n\t\tyaml.WithCompactSeqIndent(ye.prefs.CompactSequenceIndent),\n\t\tyaml.WithLineWidth(-1),\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"configure YAML encoding: %w\", err)\n\t}\n\n\ttarget, err := node.MarshalYAML()\n\tif err != nil {\n\t\t_ = dumper.Close()\n\t\treturn err\n\t}\n\n\ttrailingContent := target.FootComment\n\ttarget.FootComment = \"\"\n\n\terr = dumper.Dump(target)\n\tif closeErr := dumper.Close(); err == nil {\n\t\terr = closeErr\n\t}\n\tif err != nil {\n\t\treturn err\n\t}","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/encoder_yaml.go#L49-L85","documentation":"Returned by the YAML encoder when the underlying go-yaml dumper cannot be configured (e.g. invalid indent, unsupported options). It wraps the low-level yaml.WriteOption error with the 'configure YAML encoding' prefix so callers know the failure happened before any node was serialized. Nothing was written yet when this fires.","triggerScenarios":"Calling yq with an encoding setup whose yaml encoder options are invalid — e.g. a non-positive or oversized --indent value passed to NewYamlEncoder preferences causing yaml.WithIndent(indent) to fail inside Encode.","commonSituations":"Users invoking `yq -o yaml --indent 0` or a harness passing indent from config/env where the value is 0 or negative; embedding yqlib with custom encoder preferences that the bundled go-yaml build rejects.","solutions":["Use a supported indent value (typically 2 or 4, must be > 0) via --indent or the encoder preferences","Inspect the wrapped %w cause to identify which With* option was rejected","If embedding yqlib, validate preferences (Indent, CompactSequenceIndent) before calling NewYamlEncoder/Encode"],"exampleFix":"// before\nyq -o yaml --indent 0 '.' file.yml\n// after\nyq -o yaml --indent 2 '.' file.yml","handlingStrategy":"validation","validationCode":"indent := 2 // must be > 0; validate before invoking yq/yqlib\nif indent <= 0 {\n    return fmt.Errorf(\"invalid indent %d for YAML encoder\", indent)\n}","typeGuard":"func validIndent(n int) bool { return n > 0 }","tryCatchPattern":"out, err := enc.Encode(node)\nif err != nil {\n    var cfgErr interface{ Unwrap() error }\n    if errors.As(err, &wrapped) && strings.Contains(err.Error(), \"configure YAML encoding\") {\n        // fix encoder preferences and retry\n    }\n    return err\n}","preventionTips":["Always pass a positive indent (2 or 4) to YAML encoder preferences","Validate user-supplied --indent values in wrapper scripts before calling yq","When embedding yqlib, test encoder construction with your chosen preferences in unit tests"],"tags":["yaml","encoding","configuration"],"backgroundTag":"yaml-encoder-config-invalid","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}