Unity-Technologies/UnityCsReference · error · IOException
Failed to Copy File / Directory from '{0}' to '{1}': destina
Error message
Failed to Copy File / Directory from '{0}' to '{1}': destination path already exists. What it means
CopyFileOrDirectory refuses to overwrite: if the destination already exists (checked via the native PathExists), it throws System.IO.IOException with the source/dest formatted in, before invoking the native copy. This is a deliberate fail-loud policy — the native copy does not merge or replace.
Source
Thrown at Editor/Mono/FileUtil.bindings.cs:41
if (path == string.Empty) throw new ArgumentException("path", "The path cannot be empty.");
return DeleteFileOrDirectoryInternal(path);
}
[FreeFunction("DeleteFileOrDirectory")]
private static extern bool DeleteFileOrDirectoryInternal(string path);
[FreeFunction("IsPathCreated")]
private static extern bool PathExists(string path);
// Copies a file or directory.
public static void CopyFileOrDirectory(string source, string dest)
{
CheckForValidSourceAndDestinationArgumentsAndRaiseAnExceptionWhenNullOrEmpty(source, dest);
if (PathExists(dest))
{
throw new System.IO.IOException(string.Format(
"Failed to Copy File / Directory from '{0}' to '{1}': destination path already exists.", source, dest));
}
if (!CopyFileOrDirectoryInternal(source, dest))
{
throw new System.IO.IOException(string.Format(
"Failed to Copy File / Directory from '{0}' to '{1}'.", source, dest));
}
}
[FreeFunction("CopyFileOrDirectory")]
private static extern bool CopyFileOrDirectoryInternal(string source, string dest);
// Copies the file or directory following symbolic links.
public static void CopyFileOrDirectoryFollowSymlinks(string source, string dest)
{
CheckForValidSourceAndDestinationArgumentsAndRaiseAnExceptionWhenNullOrEmpty(source, dest);
View on GitHub (pinned to 225b0fbdb5)
Solutions
- Delete or move the destination first if overwrite is intended: if (FileUtil.PathExists(dst)) FileUtil.DeleteFileOrDirectory(dst); then copy.
- Use CopyFileOrDirectory only into a freshly created unique directory so the destination cannot pre-exist.
- Switch to a copy primitive that allows overwrite (e.g. System.IO.File.Copy with overwrite:true) when you do not need the directory-recursion behaviour.
Example fix
// before FileUtil.CopyFileOrDirectory(src, dst); // throws if dst exists // after if (FileUtil.PathExists(dst)) FileUtil.DeleteFileOrDirectory(dst); FileUtil.CopyFileOrDirectory(src, dst);
Defensive patterns
Strategy: validation
Validate before calling
if (FileUtil.PathExists(dst)) FileUtil.DeleteFileOrDirectory(dst); FileUtil.CopyFileOrDirectory(src, dst);
Type guard
static bool DestinationIsClear(string dst) => !FileUtil.PathExists(dst);
Prevention
- FileUtil copy/move never overwrite — always pre-clear the destination.
- Copy into per-run unique directories so pre-existing targets cannot collide.
- If overwrite semantics are needed, use System.IO.File.Copy(..., overwrite:true).
When it happens
Trigger: Calling CopyFileOrDirectory(src, dst) where dst already points to an existing file or directory. Re-running an import/copy step that did not clean up its prior output. Incremental build code that copies into a persistent target.
Common situations: Re-importing assets into a destination folder left over from a previous run. Build scripts copying template files into Library/ or a streaming-assets target that persists. CI re-runs that do not wipe the output directory first.
Related errors
- Failed to Copy File / Directory from '{0}' to '{1}'.
- Failed to Move File / Directory from '{0}' to '{1}'.
- Failed to delete directory '{0}'.
- FileUtil.OpenRead: Failed to acquire UDS content for '{path}
- Value cannot be null.
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/7734dac24c5f3370.
Report an issue: GitHub.