{"record":{"id":"e24952ac0ff038db","repo":"denoland/deno","slug":"a-file-exists-at-the-destination-deststr","errorCode":null,"errorMessage":"A file exists at the destination: ${destStr}","messagePattern":"A file exists at the destination: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_fs/_fs_copy.ts","lineNumber":95,"sourceCode":"export const copyFilePromise = promisify(copyFile) as (\n  src: string | Buffer | URL,\n  dest: string | Buffer | URL,\n  mode?: number,\n) => Promise<void>;\n\nexport function copyFileSync(\n  src: string | Buffer | URL,\n  dest: string | Buffer | URL,\n  mode?: number,\n) {\n  const srcStr = getValidatedPathToString(src, \"src\");\n  const destStr = getValidatedPathToString(dest, \"dest\");\n  const modeNum = getValidMode(mode, \"copyFile\");\n\n  if ((modeNum & fs.COPYFILE_EXCL) === fs.COPYFILE_EXCL) {\n    try {\n      Deno.lstatSync(destStr);\n      throw new Error(`A file exists at the destination: ${destStr}`);\n    } catch (e) {\n      if (ObjectPrototypeIsPrototypeOf(Deno.errors.NotFound.prototype, e)) {\n        Deno.copyFileSync(srcStr, destStr);\n      } else {\n        throw e;\n      }\n    }\n  } else {\n    Deno.copyFileSync(srcStr, destStr);\n  }\n}\n","sourceCodeStart":77,"sourceCodeEnd":107,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/_fs/_fs_copy.ts#L77-L107","documentation":"fs.copyFileSync with fs.constants.COPYFILE_EXCL performs a no-clobber copy: it lstats the destination and, if anything exists there (file, directory, symlink), throws 'A file exists at the destination: ...' - the polyfill's equivalent of Node's EEXIST. Without the flag the destination is silently overwritten.","triggerScenarios":"fs.copyFileSync(src, dst, fs.constants.COPYFILE_EXCL) where dst already exists - commonly from a previous run of the same script that partially completed.","commonSituations":"Idempotent install/bootstrap scripts re-run after partial completion; backup routines protecting existing archives; concurrent processes racing to create the same destination file.","solutions":["If overwriting is acceptable, drop the COPYFILE_EXCL flag","Treat the error as 'already done': catch it and skip/continue","Pre-check with fs.existsSync(dst) or fs.lstatSync when you need custom handling per case"],"exampleFix":"// before\nfs.copyFileSync(src, dst, fs.constants.COPYFILE_EXCL); // throws if dst exists\n\n// after\ntry {\n  fs.copyFileSync(src, dst, fs.constants.COPYFILE_EXCL);\n} catch (e) {\n  if (\n    e instanceof Error &&\n    e.message.startsWith(\"A file exists at the destination\")\n  ) {\n    // previously copied - treat as success\n  } else {\n    throw e;\n  }\n}","handlingStrategy":"try-catch","validationCode":"import * as fs from \"node:fs\";\n\nfunction copyNoClobber(src: string, dst: string): \"copied\" | \"existed\" {\n  if (fs.existsSync(dst)) return \"existed\"; // lstat-based pre-check\n  try {\n    fs.copyFileSync(src, dst, fs.constants.COPYFILE_EXCL);\n    return \"copied\";\n  } catch {\n    return \"existed\"; // lost the race - treat as already present\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  fs.copyFileSync(src, dst, fs.constants.COPYFILE_EXCL);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith(\"A file exists at the destination\")) {\n    // Deno polyfill: plain Error (Node sets code EEXIST) - treat as idempotent success\n  } else {\n    throw e;\n  }\n}","preventionTips":["Decide explicitly whether each copy may overwrite; only pass COPYFILE_EXCL when it may not","Make install/bootstrap scripts idempotent by treating 'destination exists' as success","Match the error by message prefix under Deno - the polyfill throws a plain Error without code"],"tags":["fs","node-compat","copyfile","eexist","validation"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}