hashicorp/packer · error

failed to flush gzip writer: %w

Error message

failed to flush gzip writer: %w

What it means

The `gzip()` HCL2 function failed while flushing the gzip.Writer after writing the input. A defensive wrap: Flush on a writer over a bytes.Buffer cannot realistically fail, so this is near-unreachable defensive code.

Source

Thrown at hcl2template/function/encoding.go:35

// Base64 encoding.
var Base64GzipFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			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 for the root cause
  3. Report to Packer 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.Flush() returns an error — practically never, since the underlying writer is an in-memory bytes.Buffer.

Common situations: Not observed in real usage; exists to satisfy complete error handling of the writer API.

Related errors


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