github/copilot-sdk · error

failed to close output file

Error message

failed to close output file: %w

What it means

extractFileFromTarball extracts a named file from a .tgz archive by copying it to an output file. After the copy succeeds, the function closes the file and surfaces any close error (e.g. buffered data not flushed to disk, ENOSPC) wrapped with this message. It guarantees the extracted file is fully persisted before returning success.

Solutions

  1. Check free disk space on the destination volume (df -h) and free up space
  2. Retry the bundling step; transient I/O errors may not recur
  3. Check dmesg / system logs for underlying disk I/O errors
  4. If on a network filesystem, extract to a local temp dir instead

Example fix

// before
if err := outFile.Close(); err != nil {
	return fmt.Errorf("failed to close output file: %w", err)
}
// after
if err := outFile.Close(); err != nil {
	if errors.Is(err, syscall.ENOSPC) {
		return fmt.Errorf("failed to close output file: %w (disk full — free up space and retry)", err)
	}
	return fmt.Errorf("failed to close output file: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// check writable space/permissions on destination before extracting
du, _ := statfs(destDir)
if du.Available < minRequiredBytes { return errors.New("insufficient disk space") }

Try / catch

// Go
tgz, err := extractFileFromTarball(tgzPath, target, destDir)
if err != nil && strings.Contains(err.Error(), "failed to close output file") {
	// free disk space or switch to a local filesystem, then retry
}

Prevention

When it happens

Trigger: The tar entry was found and copied via io.Copy without error, but outFile.Close() returns a non-nil error — typically because the write data could not be flushed to disk (disk full, I/O error, or network filesystem failure) on Close.

Common situations: Disk quota or free-space exhaustion on the machine running bundler; write failures on NFS/overlay filesystems where errors are reported at close rather than write; extracting large CLI binaries to a nearly-full 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/6e64edd4d64c725c. Report an issue: GitHub.

Appendix: source

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

		if err != nil {
			return fmt.Errorf("failed to read tar: %w", err)
		}

		if header.Name == targetPath {
			outPath := filepath.Join(destDir, outputName)
			outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(header.Mode))
			if err != nil {
				return fmt.Errorf("failed to create output file: %w", err)
			}

			if _, err := io.Copy(outFile, tarReader); err != nil {
				if cerr := outFile.Close(); cerr != nil {
					return fmt.Errorf("failed to extract binary (copy error: %v, close error: %v)", err, cerr)
				}
				return fmt.Errorf("failed to extract binary: %w", err)
			}
			if err := outFile.Close(); err != nil {
				return fmt.Errorf("failed to close output file: %w", err)
			}
			return nil
		}
	}

	return fmt.Errorf("file %q not found in tarball", targetPath)
}

// extractOptionalFileFromTarball extracts a single file from a .tgz into destDir
// like extractFileFromTarball, but returns (false, nil) instead of an error when
// the file is absent. Used for the runtime library, which older CLI packages do
// not ship.
func extractOptionalFileFromTarball(tarballPath, destDir, targetPath, outputName string) (bool, error) {
	err := extractFileFromTarball(tarballPath, destDir, targetPath, outputName)
	if err == nil {
		return true, nil
	}
	if strings.Contains(err.Error(), "not found in tarball") {

View on GitHub (pinned to cd8cf15dc3)