{"record":{"id":"ae8e5aab929f9bf4","repo":"hashicorp/packer","slug":"failed-to-write-sbom-to-q-s","errorCode":null,"errorMessage":"failed to write SBOM to %q: %s","messagePattern":"failed to write SBOM to %q: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/hcp-sbom/provisioner.go","lineNumber":905,"sourceCode":"\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 {\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 {","sourceCodeStart":887,"sourceCodeEnd":923,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/hcp-sbom/provisioner.go#L887-L923","documentation":"This error is returned when json.NewEncoder(outFile).Encode fails while writing the wrapped PackerSBOM JSON document to the already-created output file. os.Create succeeded, so this is a write-time failure: disk full, I/O error, or (rarely) marshaling failure inside the encoder. The wrapped encoder error is included in the message.","triggerScenarios":"processSBOMForHCP calls json.NewEncoder(outFile).Encode(PackerSBOM{...}) after successfully creating the file, and the Encode call returns a non-nil error — typically ENOSPC (disk full), EIO (hardware/ NFS failure), or an unserializable value in the PackerSBOM struct.","commonSituations":"Disk on the Packer host fills up while writing a large SBOM; writing to a network mount that drops; a failure marshaling the PackerSBOM wrapper (rare, since fields are plain strings/bytes).","solutions":["Check the wrapped error: if it is 'no space left on device', free disk space on the Packer host or redirect output to a larger volume.","Verify the filesystem backing pkrDst is healthy and writable during the whole encode (NFS timeouts, quota limits).","Confirm the raw SBOM data was fully fetched (truncated data can also cause encode issues downstream).","Re-run the build after fixing storage; the partially written file will be recreated on retry."],"exampleFix":"// before: destination volume too small for large SBOM\n\"destination\": \"/mnt/small-tmp/sbom.json\"\n// after: use a volume with enough free space\n\"destination\": \"/var/packer/sbom.json\"","handlingStrategy":"try-catch","validationCode":"if st, err := os.Stat(filepath.Dir(pkrDst)); err == nil {\n\tif usable := st.IsDir(); !usable {\n\t\treturn fmt.Errorf(\"%q is not a directory\", st.Name())\n\t}\n}\n// check free space via syscall.Statfs on Linux before writing large SBOMs","typeGuard":null,"tryCatchPattern":"outFile, err := os.Create(pkrDst)\nif err != nil {\n\treturn fmt.Errorf(\"failed to create output file %q: %w\", pkrDst, err)\n}\ndefer outFile.Close()\nif err := json.NewEncoder(outFile).Encode(payload); err != nil {\n\tif errors.Is(err, syscall.ENOSPC) {\n\t\t// free disk space or redirect to another volume, then retry\n\t}\n\treturn fmt.Errorf(\"failed to write SBOM to %q: %w\", pkrDst, err)\n}","preventionTips":["Monitor disk space on the Packer host (SBOMs for large images can be tens of MB).","Prefer local SSD paths over network mounts for intermediate SBOM output.","Keep json.Encoder error handling in place and wrap with %w so root causes like ENOSPC surface.","Clean up stale SBOM output files between builds."],"tags":["go","filesystem","json","disk-full"],"backgroundTag":"disk-full-write-failure","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"}