{"record":{"id":"a1fa1cf185342798","repo":"hashicorp/packer","slug":"failed-to-write-file-q-as-base64-s","errorCode":null,"errorMessage":"failed to write file %q as base64: %s","messagePattern":"failed to write file %q as base64: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/function/filebase64.go","lineNumber":37,"sourceCode":"\t\t\tName:        \"path\",\n\t\t\tDescription: \"Read a file and encode it as a base64 string\",\n\t\t\tType:        cty.String,\n\t\t},\n\t},\n\tType:         function.StaticReturnType(cty.String),\n\tRefineResult: refineNotNull,\n\tImpl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {\n\t\tpath := args[0].AsString()\n\t\tcontent, err := os.ReadFile(path)\n\t\tif err != nil {\n\t\t\treturn cty.NullVal(cty.String), fmt.Errorf(\"failed to read file %q: %s\", path, err)\n\t\t}\n\n\t\tout := &strings.Builder{}\n\t\tenc := base64.NewEncoder(base64.StdEncoding, out)\n\t\t_, err = enc.Write(content)\n\t\tif err != nil {\n\t\t\treturn cty.NullVal(cty.String), fmt.Errorf(\"failed to write file %q as base64: %s\", path, err)\n\t\t}\n\t\t_ = enc.Close()\n\n\t\treturn cty.StringVal(out.String()), nil\n\t},\n})\n","sourceCodeStart":19,"sourceCodeEnd":44,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/function/filebase64.go#L19-L44","documentation":"The `filebase64()` HCL2 function read the file successfully but the base64 encoder failed while writing the content to its output builder. Like the gzip writes, this targets a strings.Builder and is effectively unreachable defensive code — a strings.Builder Write never returns an error.","triggerScenarios":"Calling `filebase64(path)` where the internal base64.Encoder.Write returns an error — practically never since it wraps a strings.Builder.","commonSituations":"Not observed in real usage; exists for complete encoder error handling.","solutions":["Re-run the packer command","If reproducible, report to Packer — the error path should be unreachable","Check available memory if encoding a very large file"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// unreachable in practice; retry the command\npacker build template.pkr.hcl || retry=1","preventionTips":["Avoid base64-encoding extremely large files","Report upstream if reproducible","Keep Packer updated"],"tags":["hcl2","function","file","base64"],"backgroundTag":"base64-encode-failed","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}