{"record":{"id":"bea3f3d7dac2b642","repo":"hashicorp/packer","slug":"failed-to-write-sbom-to-destination-q-s","errorCode":null,"errorMessage":"failed to write SBOM to destination %q: %s","messagePattern":"failed to write SBOM to destination %q: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/hcp-sbom/provisioner.go","lineNumber":915,"sourceCode":"\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 {\n\t\t\treturn fmt.Errorf(\"failed to compute destination path %q: %s\", p.config.Destination, err)\n\t\t}\n\t\tif err := os.WriteFile(usrDst, sbomData, 0644); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to write SBOM to destination %q: %s\", usrDst, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// Communicator returns the communicator for elevated execution\nfunc (p *Provisioner) Communicator() packersdk.Communicator {\n\treturn p.communicator\n}\n\n// ElevatedUser returns the elevated user for Windows execution\nfunc (p *Provisioner) ElevatedUser() string {\n\treturn p.config.ElevatedUser\n}\n\n// ElevatedPassword returns the elevated password for Windows execution\nfunc (p *Provisioner) ElevatedPassword() string {","sourceCodeStart":897,"sourceCodeEnd":933,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/hcp-sbom/provisioner.go#L897-L933","documentation":"This error is returned when os.WriteFile fails to write the raw SBOM bytes to the computed user destination path (0644 permissions). The path was computed successfully, but creating or writing the file failed on the host filesystem — most commonly a missing parent directory or insufficient permissions.","triggerScenarios":"p.config.Destination is set, getUserDestination() succeeds, and os.WriteFile(usrDst, sbomData, 0644) returns an error because the parent directory does not exist, the path is not writable, usrDst is a directory, or the disk is full.","commonSituations":"Setting destination to a nested path whose directories were never created (e.g. ./artifacts/sbom/report.json with no artifacts/sbom/ dir); writing to a protected location like /usr or C:\\Program Files without elevation; read-only mounted output directory.","solutions":["Read the wrapped OS error: 'no such file or directory' means create the parent directories first.","Pre-create the destination directory: mkdir -p <parent of destination>.","Check write permissions for the user running Packer on the destination directory.","Ensure destination is a file path, not an existing directory.","Free disk space if the error indicates the device is full."],"exampleFix":"// before (shell)\ndestination = \"./out/sboms/report.json\"   // ./out/sboms does not exist\n// after (pre-create dirs)\n$ mkdir -p out/sboms\n$ packer build template.pkr.hcl","handlingStrategy":"validation","validationCode":"usrDst, err := getUserDestination()\nif err == nil {\n\tif st, err := os.Stat(filepath.Dir(usrDst)); err != nil || !st.IsDir() {\n\t\tos.MkdirAll(filepath.Dir(usrDst), 0o755) // ensure parent exists\n\t}\n}","typeGuard":null,"tryCatchPattern":"if err := os.WriteFile(usrDst, sbomData, 0644); err != nil {\n\tvar perr *fs.PathError\n\tif errors.As(err, &perr) && errors.Is(err, syscall.ENOENT) {\n\t\tos.MkdirAll(filepath.Dir(usrDst), 0o755)\n\t\terr = os.WriteFile(usrDst, sbomData, 0644)\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to write SBOM to destination %q: %w\", usrDst, err)\n\t}\n}","preventionTips":["Always mkdir -p the destination directory in your build pipeline before packer build.","Point destination at a user-writable directory, not system paths like /usr or C:\\Program Files.","Confirm destination is a file path, not a directory.","Watch for disk-full conditions in CI runners with small ephemeral disks."],"tags":["go","filesystem","file-io","permission"],"backgroundTag":"file-write-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"}