hashicorp/packer · error
failed to read file %q: %s
Error message
failed to read file %q: %s
What it means
The `filebase64()` HCL2 function could not read the file at the given path with os.ReadFile. It wraps the OS error (ENOENT, EACCES, EISDIR, etc.) with the path so you can see which file failed while evaluating the data source/expression.
Source
Thrown at hcl2template/function/filebase64.go:30
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
var Filebase64 = function.New(&function.Spec{
Params: []function.Parameter{
function.Parameter{
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
- Verify the path exists and is readable: `ls -l /path/to/file`
- Use a path relative to the template file or an absolute path (paths resolve from cwd, not the template dir)
- Ensure any build step that creates the file runs before packer
- If the file should be optional, guard with `fileexists(path)` before calling filebase64
Example fix
// before
content_b64 = filebase64("./certs/tls.crt") // fails if missing
// after
content_b64 = fileexists("./certs/tls.crt") ? filebase64("./certs/tls.crt") : "" Defensive patterns
Strategy: validation
Validate before calling
// guard before calling filebase64
locals {
cert_path = "./certs/tls.crt"
cert_b64 = fileexists(local.cert_path) ? filebase64(local.cert_path) : ""
} Try / catch
// CLI: check readability before the build
test -r "$CERT_PATH" || { echo "cannot read $CERT_PATH" >&2; exit 1; }
packer build template.pkr.hcl Prevention
- Verify paths with `fileexists()` in HCL or `test -f` in CI
- Run packer from the directory you expect; use absolute paths when unsure
- Ensure prerequisite files exist before the build step
- Don't pass directories to filebase64
When it happens
Trigger: `filebase64("/path/to/file")` in an HCL2 template where the path does not exist, is a directory, lacks read permission, or the working directory differs from the one assumed.
Common situations: Typo in path; running packer from a different directory than expected; file generated by a previous build step that did not run; reading a directory instead of a file; permission issues in CI.
Understand the failure class
Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.
Related errors
- failed to write file %q as base64: %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/7b6827ede8a6dd56.
Report an issue: GitHub.