github/copilot-sdk · error

failed to write output binary

Error message

failed to write output binary: %w

What it means

buildBundle compresses the extracted CLI binary with Zstandard into the final output artifact via compressZstdFile. If compression or writing the .zst file fails, the error is wrapped with this message and the bundle is aborted. It covers both read failures on the source binary and write failures on the destination file.

Solutions

  1. Free disk space or point --output at a volume with enough room for the compressed artifact (input binary size is a good lower bound).
  2. Ensure the output directory is writable by the current user; pre-create it and chown if needed.
  3. Check for filesystem quotas (quota -s) or tmpfs size limits if the output mount is /tmp or similar.
  4. Re-run the build; transient I/O errors while writing large files can resolve on retry.

Example fix

// before
bundler --output /mnt/small-tmpfs/dist
// no space left on device

// after
bundler --output /var/lib/build/dist   # larger persistent volume
Defensive patterns

Strategy: try-catch

Validate before calling

if err := os.Chmod(outputPath, 0o644); err != nil {
	return fmt.Errorf("output location %q not writable: %w", outputPath, err)
}
probe := outputPath + ".probe"
if err := os.WriteFile(probe, []byte("x"), 0o644); err != nil {
	return fmt.Errorf("cannot write to output volume: %w", err)
}
os.Remove(probe)
if stat, err := os.Stat(filepath.Dir(outputPath)); err == nil {
	if stat, _ := stat.Sys().(*syscall.Statfs_t); stat != nil && stat.Bavail*uint64(stat.Bsize) < 2<<30 {
		return fmt.Errorf("output volume has <2GB free")
	}
}

Type guard

func canWriteTo(dir string) bool {
	f, err := os.CreateTemp(dir, ".probe*")
	if err != nil {
		return false
	}
	f.Close()
	os.Remove(f.Name())
	return true
}

Try / catch

if err := buildBundle(...); err != nil {
	if strings.Contains(err.Error(), "failed to write output binary") {
		var pe *fs.PathError
		if errors.As(err, &pe) && errors.Is(err, syscall.ENOSPC) {
			fmt.Fprintln(os.Stderr, "disk full on output volume; free space or choose another --output")
			os.Exit(1)
		}
	}
	return err
}

Prevention

When it happens

Trigger: compressZstdFile(binaryPath, outputPath) errors: outputPath cannot be created/written (permissions, read-only fs), disk is full, or the source binary becomes unreadable mid-compression.

Common situations: Output directory on a full disk (large multi-hundred-MB binaries), running in a container with a read-only or small tmpfs output mount, or filesystem quota exceeded on the output volume.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/43417ad2bbdad665. Report an issue: GitHub.

Appendix: source

Thrown at go/cmd/bundler/main.go:461

	}

	if outputDir != "." {
		if err := os.MkdirAll(outputDir, 0755); err != nil {
			return bundleArtifacts{}, fmt.Errorf("failed to create output directory: %w", err)
		}
	}
	if includeLicense {
		if err := extractCLILicense(tarballPath, outputPath); err != nil {
			return bundleArtifacts{}, fmt.Errorf("failed to extract CLI license: %w", err)
		}
	}

	binaryHash, err := sha256File(binaryPath)
	if err != nil {
		return bundleArtifacts{}, fmt.Errorf("failed to hash output binary: %w", err)
	}
	if err := compressZstdFile(binaryPath, outputPath); err != nil {
		return bundleArtifacts{}, fmt.Errorf("failed to write output binary: %w", err)
	}

	rawLibPath := filepath.Join(tempDir, "runtime.node")
	if err := extractFileFromTarball(
		tarballPath,
		tempDir,
		"package/prebuilds/"+info.runtimePlatform+"/runtime.node",
		"runtime.node",
	); err != nil {
		return bundleArtifacts{}, fmt.Errorf("runtime package is missing prebuilds/%s/runtime.node: %w", info.runtimePlatform, err)
	}
	runtimeHash, err := sha256File(rawLibPath)
	if err != nil {
		return bundleArtifacts{}, fmt.Errorf("failed to hash runtime.node: %w", err)
	}
	if err := compressZstdFile(rawLibPath, runtimeArtifactPath); err != nil {
		return bundleArtifacts{}, fmt.Errorf("failed to write runtime.node: %w", err)
	}

View on GitHub (pinned to cd8cf15dc3)