{"record":{"id":"8512052882aa403f","repo":"hashicorp/packer","slug":"failed-to-create-output-file-q-s","errorCode":null,"errorMessage":"failed to create output file %q: %s","messagePattern":"failed to create output file %q: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/hcp-sbom/provisioner.go","lineNumber":893,"sourceCode":"\n// processSBOMForHCP validates, compresses, and prepares SBOM for HCP upload\nfunc (p *Provisioner) processSBOMForHCP(generatedData map[string]interface{}, sbomData []byte) error {\n\t// Validate SBOM format\n\tformat, err := validateSBOM(sbomData)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"SBOM validation failed: %s\", err)\n\t}\n\n\t// Get destination path from generatedData\n\tpkrDst, ok := generatedData[\"dst\"].(string)\n\tif !ok || pkrDst == \"\" {\n\t\treturn fmt.Errorf(\"packer destination path missing from configs: this is an internal error\")\n\t}\n\n\t// Write PackerSBOM to destination\n\toutFile, err := os.Create(pkrDst)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create output file %q: %s\", pkrDst, err)\n\t}\n\tdefer func() {\n\t\t_ = outFile.Close() // Cleanup, ignore error\n\t}()\n\n\terr = json.NewEncoder(outFile).Encode(PackerSBOM{\n\t\tRawSBOM: sbomData,\n\t\tFormat:  format,\n\t\tName:    p.config.SbomName,\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to write SBOM to %q: %s\", pkrDst, err)\n\t}\n\n\t// Also save to user destination if specified\n\tif p.config.Destination != \"\" {\n\t\tusrDst, err := p.getUserDestination()\n\t\tif err != nil {","sourceCodeStart":875,"sourceCodeEnd":911,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/hcp-sbom/provisioner.go#L875-L911","documentation":"This error is returned by processSBOMForHCP when os.Create fails to create/truncate the internal Packer SBOM output file at the destination path taken from generatedData[\"dst\"]. It means the provisioner could not open a writable file at that path on the machine running Packer (the host, not the build VM). The wrapped OS error (e.g. permission denied, no such file or directory) is embedded in the message.","triggerScenarios":"processSBOMForHCP is called (via provisionWithExistingSBOM or provisionWithNativeGeneration) after a valid SBOM is obtained, and os.Create(pkrDst) fails because the dst directory does not exist, the path is not writable by the Packer process, dst points to a read-only filesystem, or dst is a directory.","commonSituations":"Running Packer as a non-root user while dst resolves to a root-owned path (e.g. /packer/...); HCP artifact storage path not present on the host; dst configured via template variables that expand to an invalid or empty-but-truthy path; containerized Packer with a read-only rootfs.","solutions":["Read the wrapped OS error in the message to identify the cause (permission denied vs no such file or directory).","Ensure the parent directory of the destination path exists: mkdir -p $(dirname <pkrDst>).","Verify the user running Packer has write permission to that directory, or run Packer with sufficient privileges.","Check that dst is not a directory and is on a writable filesystem (e.g. not a read-only container rootfs).","Re-run the build; this error occurs before upload, so nothing was uploaded to HCP."],"exampleFix":"// before (shell): run packer with dst under a dir the user cannot write\n$ packer build template.pkr.hcl\n// after\n$ sudo mkdir -p /packer/sbom && sudo chown $(whoami) /packer/sbom\n$ packer build template.pkr.hcl","handlingStrategy":"validation","validationCode":"pkrDst, ok := generatedData[\"dst\"].(string)\nif !ok || pkrDst == \"\" {\n\treturn fmt.Errorf(\"destination path missing\")\n}\nif st, err := os.Stat(filepath.Dir(pkrDst)); err != nil || !st.IsDir() {\n\treturn fmt.Errorf(\"destination directory %q not usable: %w\", filepath.Dir(pkrDst), err)\n}","typeGuard":"func dstOK(generatedData map[string]interface{}) (string, bool) {\n\ts, ok := generatedData[\"dst\"].(string)\n\treturn s, ok && s != \"\"\n}","tryCatchPattern":null,"preventionTips":["Pre-create the destination directory and chown it to the user running Packer.","Verify writable disk space on the Packer host before long builds.","Test dst resolution with a small dry-run build before large production builds.","Avoid pointing dst at read-only or ephemeral filesystems in containers."],"tags":["go","filesystem","file-io","permission"],"backgroundTag":"file-create-permission-denied","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"}