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

  1. 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.
  2. Verify the filesystem backing pkrDst is healthy and writable during the whole encode (NFS timeouts, quota limits).
  3. Confirm the raw SBOM data was fully fetched (truncated data can also cause encode issues downstream).
  4. 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

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


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/ae8e5aab929f9bf4. Report an issue: GitHub.