golang/go · error

copying %s to %s: %v

Error message

copying %s to %s: %v

What it means

Thrown by copyFile when io.Copy fails partway through copying the build artifact to its destination (after the destination was successfully opened). The destination is removed via mayberemovefile to avoid leaving a corrupt partial binary, and the error names both source and destination.

Source

Thrown at src/cmd/go/internal/work/shell.go:232

	if err != nil && runtime.GOOS == "windows" {
		// Windows does not allow deletion of a binary file
		// while it is executing. Try to move it out of the way.
		// If the move fails, which is likely, we'll try again the
		// next time we do an install of this binary.
		if err := os.Rename(dst, dst+"~"); err == nil {
			os.Remove(dst + "~")
		}
		df, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
	}
	if err != nil {
		return fmt.Errorf("copying %s: %w", src, err) // err should already refer to dst
	}

	_, err = io.Copy(df, sf)
	df.Close()
	if err != nil {
		mayberemovefile(dst)
		return fmt.Errorf("copying %s to %s: %v", src, dst, err)
	}
	return nil
}

// mayberemovefile removes a file only if it is a regular file
// When running as a user with sufficient privileges, we may delete
// even device files, for example, which is not intended.
func mayberemovefile(s string) {
	if fi, err := os.Lstat(s); err == nil && !fi.Mode().IsRegular() {
		return
	}
	os.Remove(s)
}

// Be careful about removing/overwriting dst.
// Do not remove/overwrite if dst exists and is a directory
// or a non-empty non-object file.
func checkDstOverwrite(dst string, force bool) error {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Check free space at the destination: `df -h <dst-dir>`; free space and rebuild.
  2. If on a network fs, build to a local dir first (`go build -o /tmp/app`) and copy manually.
  3. Inspect dmesg/system logs for I/O errors on the underlying device.
  4. Retry the build; transient fs errors often clear.

Example fix

# before
// go build -o /mnt/nfs/app   (io.Copy fails)

# after
// go build -o ./app && cp ./app /mnt/nfs/app
Defensive patterns

Strategy: try-catch

Validate before calling

// Check free space at destination before a large install
var st syscall.Statfs_t
if err := syscall.Statfs(filepath.Dir(dst), &st); err == nil {
    free := st.Bavail * uint64(st.Bsize)
    if free < minFree { log.Fatalf("only %d bytes free at %s", free, filepath.Dir(dst)) }
}

Try / catch

err := sh.copyFile(src, dst)
if err != nil {
    var perr *fs.PathError
    if errors.As(err, &perr) && perr.Err == syscall.ENOSPC {
        return fmt.Errorf("disk full while writing %s; free space and retry", dst)
    }
    return err
}

Prevention

When it happens

Trigger: During `go install`/`go build -o`, io.Copy returns an error — e.g. disk fills mid-write, the source file vanishes, an I/O error occurs on the device, or (rarely) a signal interrupts the copy. The df/sf handles are opened fine, but the byte stream fails.

Common situations: Destination filesystem runs out of space mid-write; network filesystem (NFS/SMB) hiccup; hardware/disk error; build cache on a flaky volume; antivirus interfering on Windows.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/e3124ce54318a9e2. Report an issue: GitHub.