hashicorp/packer · error
failed to write SBOM to %q: %s
Error message
failed to write SBOM to %q: %s
What it means
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.
Source
Thrown at provisioner/hcp-sbom/provisioner.go:905
return fmt.Errorf("packer destination path missing from configs: this is an internal error")
}
// Write PackerSBOM to destination
outFile, err := os.Create(pkrDst)
if err != nil {
return fmt.Errorf("failed to create output file %q: %s", pkrDst, err)
}
defer func() {
_ = outFile.Close() // Cleanup, ignore error
}()
err = json.NewEncoder(outFile).Encode(PackerSBOM{
RawSBOM: sbomData,
Format: format,
Name: p.config.SbomName,
})
if err != nil {
return fmt.Errorf("failed to write SBOM to %q: %s", pkrDst, err)
}
// Also save to user destination if specified
if p.config.Destination != "" {
usrDst, err := p.getUserDestination()
if err != nil {
return fmt.Errorf("failed to compute destination path %q: %s", p.config.Destination, err)
}
if err := os.WriteFile(usrDst, sbomData, 0644); err != nil {
return fmt.Errorf("failed to write SBOM to destination %q: %s", usrDst, err)
}
}
return nil
}
// Communicator returns the communicator for elevated execution
func (p *Provisioner) Communicator() packersdk.Communicator {View on GitHub (pinned to eb36e3c3e4)
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.
Example fix
// before: destination volume too small for large SBOM "destination": "/mnt/small-tmp/sbom.json" // after: use a volume with enough free space "destination": "/var/packer/sbom.json"
Defensive patterns
Strategy: try-catch
Validate before calling
if st, err := os.Stat(filepath.Dir(pkrDst)); err == nil {
if usable := st.IsDir(); !usable {
return fmt.Errorf("%q is not a directory", st.Name())
}
}
// check free space via syscall.Statfs on Linux before writing large SBOMs Try / catch
outFile, err := os.Create(pkrDst)
if err != nil {
return fmt.Errorf("failed to create output file %q: %w", pkrDst, err)
}
defer outFile.Close()
if err := json.NewEncoder(outFile).Encode(payload); err != nil {
if errors.Is(err, syscall.ENOSPC) {
// free disk space or redirect to another volume, then retry
}
return fmt.Errorf("failed to write SBOM to %q: %w", pkrDst, err)
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- failed to close temp file: %w
- marshal artifact identity: %w
- failed to get source: %w
- could not create plugin folder %q: %w
- could not create final plugin binary file: %w
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/ae8e5aab929f9bf4.
Report an issue: GitHub.