coreybutler/nvm-windows · error

failed to create destination file: %w

Error message

failed to create destination file: %w

What it means

In copyFile(), after the destination directory exists, os.Create(new) opens the destination file for writing. This error means creation failed: a file or directory already exists at that exact name with the wrong type/permissions, the volume is full, or the path is invalid. Note os.Create truncates an existing writable file, so a plain duplicate file does NOT trigger this — denial, not existence, is the usual cause.

Source

Thrown at src/utility/rename.go:64

// copyFile copies a single file from source (old) to destination (new).
func copyFile(old, new string) error {
	srcFile, err := os.Open(old)
	if err != nil {
		return fmt.Errorf("failed to open source file: %w", err)
	}
	defer srcFile.Close()

	// Ensure destination directory exists
	destDir := filepath.Dir(new)
	err = os.MkdirAll(destDir, os.ModePerm)
	if err != nil {
		return fmt.Errorf("failed to create destination directory: %w", err)
	}

	destFile, err := os.Create(new)
	if err != nil {
		return fmt.Errorf("failed to create destination file: %w", err)
	}
	defer destFile.Close()

	_, err = io.Copy(destFile, srcFile)
	if err != nil {
		return fmt.Errorf("failed to copy data: %w", err)
	}

	// Copy file permissions
	info, err := srcFile.Stat()
	if err != nil {
		return fmt.Errorf("failed to get source file info: %w", err)
	}
	err = os.Chmod(new, info.Mode())
	if err != nil {
		return fmt.Errorf("failed to set permissions on destination file: %w", err)
	}

View on GitHub (pinned to 5b18223ca1)

Solutions

  1. Clear the read-only attribute on the destination file: attrib -r <dest>, or delete the stale destination and retry.
  2. Free space on the destination volume if the wrapped error says insufficient disk space.
  3. Run elevated for 'Access is denied'.
  4. Remove any directory occupying the destination filename (rmdir) before retrying.
Defensive patterns

Strategy: validation

Validate before calling

if info, err := os.Stat(new); err == nil && info.IsDir() {
    return fmt.Errorf("destination %s is a directory, expected file", new)
}
// clear read-only bit on stale destination
os.Chmod(new, 0o666)

Try / catch

if err := copyAcrossDrives(old, new); err != nil && strings.Contains(err.Error(), "create destination file") {
    attrib := exec.Command("attrib", "-r", new); attrib.Run()
    err = copyAcrossDrives(old, new)
}

Prevention

When it happens

Trigger: copyFile where the destination name is occupied by a read-only file or a directory, the destination volume is out of space, or the user lacks write permission on the destination folder.

Common situations: Previous nvm install left a read-only node.exe at the destination; disk full on the target drive; copying into a protected directory without elevation; leftover directory where a file is expected in the versions tree.

Related errors


AI-assisted analysis of coreybutler/nvm-windows@5b18223ca1 (2026-08-15). Data as JSON: /api/errors/07e496f7557ebec6. Report an issue: GitHub.