{"record":{"id":"78c0a5eef052a807","repo":"mikefarah/yq","slug":"failed-to-encode-hcl-w","errorCode":null,"errorMessage":"failed to encode HCL: %w","messagePattern":"failed to encode HCL: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/encoder_hcl.go","lineNumber":58,"sourceCode":"func (he *hclEncoder) PrintLeadingContent(_ io.Writer, _ string) error {\n\treturn nil\n}\n\nfunc (he *hclEncoder) Encode(writer io.Writer, node *CandidateNode) error {\n\tlog.Debugf(\"I need to encode %v\", NodeToString(node))\n\tif node.Kind == ScalarNode {\n\t\treturn writeString(writer, node.Value+\"\\n\")\n\t}\n\n\tf := hclwrite.NewEmptyFile()\n\tbody := f.Body()\n\n\t// Collect comments as we encode\n\tcommentMap := make(map[string]string)\n\the.collectComments(node, \"\", commentMap)\n\n\tif err := he.encodeNode(body, node); err != nil {\n\t\treturn fmt.Errorf(\"failed to encode HCL: %w\", err)\n\t}\n\n\t// Get the formatted output and remove extra spacing before '='\n\toutput := f.Bytes()\n\tcompactOutput := he.compactSpacing(output)\n\n\t// Inject comments back into the output\n\tfinalOutput := he.injectComments(compactOutput, commentMap)\n\n\tif he.prefs.ColorsEnabled {\n\t\tcolourized := he.colorizeHcl(finalOutput)\n\t\t_, err := writer.Write(colourized)\n\t\treturn err\n\t}\n\n\t_, err := writer.Write(finalOutput)\n\treturn err\n}","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/encoder_hcl.go#L40-L76","documentation":"Wrapper error for the HCL encoder: any failure while converting the CandidateNode tree into HCL body content is wrapped with 'failed to encode HCL'. The underlying cause (unsupported node shape, bad raw expression, non-mapping root, etc.) is chained via %w.","triggerScenarios":"Running `yq -o hcl` on data the HCL encoder cannot represent — e.g. a root that is not a mapping, a node with an alias, or an attribute value whose raw expression contains unsupported characters.","commonSituations":"Converting YAML/JSON config to HCL/Terraform where the data contains constructs HCL cannot express (aliases, deeply nested non-block shapes); inspect the chained cause with `%+v` or stderr detail.","solutions":["Read the wrapped cause below this message to identify the real failure","Reshape the input so the root is a mapping of HCL-representable values","Remove aliases (or resolve them) before encoding: `yq '... alias-resolving expr ...' -o hcl`","Validate the data structure against HCL block expectations before encoding"],"exampleFix":"// before: yq -o hcl '.' malformed.yaml\n// after:  yq -o hcl '( ... | del(.alias_field) )' input.yaml  — fix the underlying cause first","handlingStrategy":"try-catch","validationCode":"// pre-validate with yq before encoding to HCL:\n// yq 'type == \"!!map\" and ([.. | tag] | any(\"!!alias\" | not))' input.yaml","typeGuard":null,"tryCatchPattern":"if err := he.Encode(writer, node); err != nil {\n    var wrapped = fmt.Errorf(\"hcl encode failed: %w\", err)\n    log.Printf(\"%v\", wrapped) // inspect chained cause for the real error\n    return fallbackFormat(node) // e.g. emit JSON instead\n}","preventionTips":["Always read the %w-wrapped cause, not just the wrapper message","Ensure root is a mapping and no aliases exist before `-o hcl`","Validate raw-expression strings contain only supported characters"],"tags":["hcl","encoding","wrapped-error","yq"],"backgroundTag":"hcl-encode-failure","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"}