{"record":{"id":"7734dac24c5f3370","repo":"Unity-Technologies/UnityCsReference","slug":"failed-to-copy-file-directory-from-0-to-1","errorCode":null,"errorMessage":"Failed to Copy File / Directory from '{0}' to '{1}': destination path already exists.","messagePattern":"Failed to Copy File / Directory from '(.+?)' to '(.+?)': destination path already exists\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/FileUtil.bindings.cs","lineNumber":41,"sourceCode":"            if (path == string.Empty) throw new ArgumentException(\"path\", \"The path cannot be empty.\");\n\n            return DeleteFileOrDirectoryInternal(path);\n        }\n\n        [FreeFunction(\"DeleteFileOrDirectory\")]\n        private static extern bool DeleteFileOrDirectoryInternal(string path);\n\n        [FreeFunction(\"IsPathCreated\")]\n        private static extern bool PathExists(string path);\n\n        // Copies a file or directory.\n        public static void CopyFileOrDirectory(string source, string dest)\n        {\n            CheckForValidSourceAndDestinationArgumentsAndRaiseAnExceptionWhenNullOrEmpty(source, dest);\n\n            if (PathExists(dest))\n            {\n                throw new System.IO.IOException(string.Format(\n                    \"Failed to Copy File / Directory from '{0}' to '{1}': destination path already exists.\", source, dest));\n            }\n\n            if (!CopyFileOrDirectoryInternal(source, dest))\n            {\n                throw new System.IO.IOException(string.Format(\n                    \"Failed to Copy File / Directory from '{0}' to '{1}'.\", source, dest));\n            }\n        }\n\n        [FreeFunction(\"CopyFileOrDirectory\")]\n        private static extern bool CopyFileOrDirectoryInternal(string source, string dest);\n\n        // Copies the file or directory following symbolic links.\n        public static void CopyFileOrDirectoryFollowSymlinks(string source, string dest)\n        {\n            CheckForValidSourceAndDestinationArgumentsAndRaiseAnExceptionWhenNullOrEmpty(source, dest);\n","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/FileUtil.bindings.cs#L23-L59","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nFileUtil.CopyFileOrDirectory(src, dst); // throws if dst exists\n// after\nif (FileUtil.PathExists(dst)) FileUtil.DeleteFileOrDirectory(dst);\nFileUtil.CopyFileOrDirectory(src, dst);","handlingStrategy":"validation","validationCode":"if (FileUtil.PathExists(dst)) FileUtil.DeleteFileOrDirectory(dst);\nFileUtil.CopyFileOrDirectory(src, dst);","typeGuard":"static bool DestinationIsClear(string dst) => !FileUtil.PathExists(dst);","tryCatchPattern":null,"preventionTips":["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)."],"tags":["filesystem","copy","ioexception","exists","unity-editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}