{"record":{"id":"f91f52c566c8a8f0","repo":"hashicorp/packer","slug":"failed-to-write-gzip-raw-data-w","errorCode":null,"errorMessage":"failed to write gzip raw data: %w","messagePattern":"failed to write gzip raw data: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/function/encoding.go","lineNumber":32,"sourceCode":")\n\n// Base64GzipFunc constructs a function that compresses a string with gzip and then encodes the result in\n// Base64 encoding.\nvar Base64GzipFunc = function.New(&function.Spec{\n\tParams: []function.Parameter{\n\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","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/function/encoding.go#L14-L50","documentation":"The `gzip()` HCL2 function compresses a string and the underlying gzip.Writer failed while writing the raw input data. This is a defensive wrap: writes into a bytes.Buffer essentially never fail, so the error is near-unreachable.","triggerScenarios":"Calling `gzip(someString)` in a template where gz.Write returns an error — practically only if the internal buffer write fails (out-of-memory-type conditions).","commonSituations":"Almost never hit in practice; the input is always a string coerced via AsString so no user input directly causes it.","solutions":["Retry the packer command — transient memory pressure is the only plausible cause","Inspect the wrapped error (%w) for the root cause","Reduce template variable sizes if memory is constrained"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// error surfaces during template evaluation; wrap the packer run\ngz_b64=$(packer console <<< 'gzip(\"data\")' 2>&1) || echo \"gzip failed: $gz_b64\"","preventionTips":["Retry on transient memory pressure","Inspect the wrapped %w error for the real cause","Keep gzip inputs to reasonable-size strings"],"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"}