hashicorp/packer · error

failed to close gzip writer: %w

Error message

failed to close gzip writer: %w

What it means

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.

Source

Thrown at hcl2template/function/encoding.go:38

		{
			Name: "str",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		s := args[0].AsString()

		var b bytes.Buffer
		gz := gzip.NewWriter(&b)
		if _, err := gz.Write([]byte(s)); err != nil {
			return cty.UnknownVal(cty.String), fmt.Errorf("failed to write gzip raw data: %w", err)
		}
		if err := gz.Flush(); err != nil {
			return cty.UnknownVal(cty.String), fmt.Errorf("failed to flush gzip writer: %w", err)
		}
		if err := gz.Close(); err != nil {
			return cty.UnknownVal(cty.String), fmt.Errorf("failed to close gzip writer: %w", err)
		}
		return cty.StringVal(base64.StdEncoding.EncodeToString(b.Bytes())), nil
	},
})

// Base64Gzip compresses a string with gzip and then encodes the result in
// Base64 encoding.
//
// Packer uses the "standard" Base64 alphabet as defined in RFC 4648 section 4.
//
// Strings in the Packer language are sequences of unicode characters rather
// than bytes, so this function will first encode the characters from the string
// as UTF-8, then apply gzip compression, and then finally apply Base64 encoding.
func Base64Gzip(str cty.Value) (cty.Value, error) {
	return Base64GzipFunc.Call([]cty.Value{str})
}

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Re-run the packer command
  2. Inspect the wrapped underlying error
  3. Report upstream if reproducible, since it should be unreachable
Defensive patterns

Strategy: retry

Try / catch

// unreachable in practice; retry the command
packer build template.pkr.hcl || retry=1

Prevention

When it happens

Trigger: Calling `gzip(someString)` where gz.Close() returns an error — practically never in normal operation.

Common situations: Not observed in real usage; exists for complete writer error handling.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/c04b23a33478aa72. Report an issue: GitHub.