{"record":{"id":"7b6827ede8a6dd56","repo":"hashicorp/packer","slug":"failed-to-read-file-q-s","errorCode":null,"errorMessage":"failed to read file %q: %s","messagePattern":"failed to read file %q: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/function/filebase64.go","lineNumber":30,"sourceCode":"\t\"github.com/zclconf/go-cty/cty\"\n\t\"github.com/zclconf/go-cty/cty/function\"\n)\n\nvar Filebase64 = function.New(&function.Spec{\n\tParams: []function.Parameter{\n\t\tfunction.Parameter{\n\t\t\tName:        \"path\",\n\t\t\tDescription: \"Read a file and encode it as a base64 string\",\n\t\t\tType:        cty.String,\n\t\t},\n\t},\n\tType:         function.StaticReturnType(cty.String),\n\tRefineResult: refineNotNull,\n\tImpl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {\n\t\tpath := args[0].AsString()\n\t\tcontent, err := os.ReadFile(path)\n\t\tif err != nil {\n\t\t\treturn cty.NullVal(cty.String), fmt.Errorf(\"failed to read file %q: %s\", path, err)\n\t\t}\n\n\t\tout := &strings.Builder{}\n\t\tenc := base64.NewEncoder(base64.StdEncoding, out)\n\t\t_, err = enc.Write(content)\n\t\tif err != nil {\n\t\t\treturn cty.NullVal(cty.String), fmt.Errorf(\"failed to write file %q as base64: %s\", path, err)\n\t\t}\n\t\t_ = enc.Close()\n\n\t\treturn cty.StringVal(out.String()), nil\n\t},\n})\n","sourceCodeStart":12,"sourceCodeEnd":44,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/function/filebase64.go#L12-L44","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","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"],"exampleFix":"// before\ncontent_b64 = filebase64(\"./certs/tls.crt\") // fails if missing\n// after\ncontent_b64 = fileexists(\"./certs/tls.crt\") ? filebase64(\"./certs/tls.crt\") : \"\"","handlingStrategy":"validation","validationCode":"// guard before calling filebase64\nlocals {\n  cert_path = \"./certs/tls.crt\"\n  cert_b64  = fileexists(local.cert_path) ? filebase64(local.cert_path) : \"\"\n}","typeGuard":null,"tryCatchPattern":"// CLI: check readability before the build\ntest -r \"$CERT_PATH\" || { echo \"cannot read $CERT_PATH\" >&2; exit 1; }\npacker build template.pkr.hcl","preventionTips":["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"],"tags":["hcl2","function","file","filesystem"],"backgroundTag":"file-not-found","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"}