hashicorp/packer · error
failed to write file %q as base64: %s
Error message
failed to write file %q as base64: %s
What it means
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.
Source
Thrown at hcl2template/function/filebase64.go:37
Name: "path",
Description: "Read a file and encode it as a base64 string",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
RefineResult: refineNotNull,
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
path := args[0].AsString()
content, err := os.ReadFile(path)
if err != nil {
return cty.NullVal(cty.String), fmt.Errorf("failed to read file %q: %s", path, err)
}
out := &strings.Builder{}
enc := base64.NewEncoder(base64.StdEncoding, out)
_, err = enc.Write(content)
if err != nil {
return cty.NullVal(cty.String), fmt.Errorf("failed to write file %q as base64: %s", path, err)
}
_ = enc.Close()
return cty.StringVal(out.String()), nil
},
})
View on GitHub (pinned to eb36e3c3e4)
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
Defensive patterns
Strategy: retry
Try / catch
// unreachable in practice; retry the command packer build template.pkr.hcl || retry=1
Prevention
- Avoid base64-encoding extremely large files
- Report upstream if reproducible
- Keep Packer updated
When it happens
Trigger: Calling `filebase64(path)` where the internal base64.Encoder.Write returns an error — practically never since it wraps a strings.Builder.
Common situations: Not observed in real usage; exists for complete encoder error handling.
Related errors
- failed to read file %q: %s
- too many values, 1 needed: %v
- failed to write gzip raw data: %w
- failed to flush gzip writer: %w
- failed to close gzip writer: %w
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/a1fa1cf185342798.
Report an issue: GitHub.