hashicorp/packer · error

Failed to compress %s: %s

Error message

Failed to compress %s: %s

What it means

The io.Copy from the artifact's source file into the compression writer failed during Packer's compress post-processor. Reading the source or writing/compressing into the output stream returned an error after the file was successfully opened, so the build aborted mid-compression.

Source

Thrown at post-processor/compress/post-processor.go:243

		// into our compressor.
		if len(artifact.Files()) != 1 {
			return nil, false, false, fmt.Errorf(
				"Can only have 1 input file when not using tar/zip. Found %d "+
					"files: %v", len(artifact.Files()), artifact.Files())
		}
		archiveFile := artifact.Files()[0]
		ui.Say(fmt.Sprintf("Archiving %s with %s", archiveFile, compression))

		source, err := os.Open(archiveFile)
		if err != nil {
			return nil, false, false, fmt.Errorf(
				"Failed to open source file %s for reading: %s",
				archiveFile, err)
		}
		defer source.Close()

		if _, err = io.Copy(output, source); err != nil {
			return nil, false, false, fmt.Errorf("Failed to compress %s: %s",
				archiveFile, err)
		}
	}

	ui.Say(fmt.Sprintf("Archive %s completed", target))

	return newArtifact, false, false, nil
}

func (config *Config) detectFromFilename() {
	var result [][]string

	extensions := map[string]string{
		"tar":   "tar",
		"zip":   "zip",
		"gz":    "pgzip",
		"lz4":   "lz4",
		"bgzf":  "bgzf",

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Check free disk space at the output path (df -h) and clean up
  2. Look at the wrapped inner error to distinguish read vs write failure
  3. Move output_directory to larger/local storage and retry
  4. Retry the build; if persistent, test the source file integrity (e.g. fsck or re-download)
Defensive patterns

Strategy: retry

Validate before calling

// ensure adequate disk space before compressing
if fi, err := os.Stat(outputDir); err == nil {
    _ = fi
}
// check at least 2x the source size is free on the output filesystem
if st, err := os.Stat(sourcePath); err == nil {
    if free, _ := diskFree(outputDir); free < 2*st.Size() {
        return fmt.Errorf("insufficient disk space to compress %s", sourcePath)
    }
}

Try / catch

// Go: retry once on transient copy failure
artifact, _, _, err := pp.PostProcess(ctx, ui, artifact)
if err != nil && strings.Contains(err.Error(), "Failed to compress") {
    time.Sleep(2 * time.Second)
    artifact, _, _, err = pp.PostProcess(ctx, ui, artifact)
}
return err

Prevention

When it happens

Trigger: The default (single-file, non tar/zip) branch runs io.Copy(output, source) and it returns an error — typically ENOSPC writing the output archive, an I/O error on the output file, or a read failure on the source mid-stream.

Common situations: Disk filling up while compressing a large disk image; output filesystem hitting a quota; source file truncated/changed concurrently; failing disk or network storage holding the output path.

Related errors


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