{"record":{"id":"c04b23a33478aa72","repo":"hashicorp/packer","slug":"failed-to-close-gzip-writer-w","errorCode":null,"errorMessage":"failed to close gzip writer: %w","messagePattern":"failed to close gzip writer: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/function/encoding.go","lineNumber":38,"sourceCode":"\t\t{\n\t\t\tName: \"str\",\n\t\t\tType: cty.String,\n\t\t},\n\t},\n\tType: function.StaticReturnType(cty.String),\n\tImpl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {\n\t\ts := args[0].AsString()\n\n\t\tvar b bytes.Buffer\n\t\tgz := gzip.NewWriter(&b)\n\t\tif _, err := gz.Write([]byte(s)); err != nil {\n\t\t\treturn cty.UnknownVal(cty.String), fmt.Errorf(\"failed to write gzip raw data: %w\", err)\n\t\t}\n\t\tif err := gz.Flush(); err != nil {\n\t\t\treturn cty.UnknownVal(cty.String), fmt.Errorf(\"failed to flush gzip writer: %w\", err)\n\t\t}\n\t\tif err := gz.Close(); err != nil {\n\t\t\treturn cty.UnknownVal(cty.String), fmt.Errorf(\"failed to close gzip writer: %w\", err)\n\t\t}\n\t\treturn cty.StringVal(base64.StdEncoding.EncodeToString(b.Bytes())), nil\n\t},\n})\n\n// Base64Gzip compresses a string with gzip and then encodes the result in\n// Base64 encoding.\n//\n// Packer uses the \"standard\" Base64 alphabet as defined in RFC 4648 section 4.\n//\n// Strings in the Packer language are sequences of unicode characters rather\n// than bytes, so this function will first encode the characters from the string\n// as UTF-8, then apply gzip compression, and then finally apply Base64 encoding.\nfunc Base64Gzip(str cty.Value) (cty.Value, error) {\n\treturn Base64GzipFunc.Call([]cty.Value{str})\n}\n","sourceCodeStart":20,"sourceCodeEnd":55,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/function/encoding.go#L20-L55","documentation":"The `gzip()` HCL2 function failed to close the gzip.Writer, which finalizes the gzip footer/checksum. A defensive wrap: Close over a bytes.Buffer cannot realistically fail, so this is near-unreachable defensive code.","triggerScenarios":"Calling `gzip(someString)` where gz.Close() returns an error — practically never in normal operation.","commonSituations":"Not observed in real usage; exists for complete writer error handling.","solutions":["Re-run the packer command","Inspect the wrapped underlying error","Report upstream if reproducible, since it should be unreachable"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// unreachable in practice; retry the command\npacker build template.pkr.hcl || retry=1","preventionTips":["Report upstream if reproducible","Keep Packer updated","No user-side input can trigger this"],"tags":["hcl2","function","gzip","encoding"],"backgroundTag":"gzip-compression-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"}