{"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/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/_fs/_fs_copy.ts#L77-L107","documentation":"Node-compat fs.copyFileSync with fs.constants.COPYFILE_EXCL first lstats the destination; if it exists it throws this Error, emulating Node's EEXIST semantics. Note that the Deno polyfill throws a plain Error whose message embeds the destination path - unlike Node it carries no code property, so detection is by catch or by pre-checking the destination yourself.","triggerScenarios":"fs.copyFileSync(src, dest, fs.constants.COPYFILE_EXCL) with dest already present; passing a mode number whose low bit includes EXCL (COPYFILE_EXCL is 1); re-running an idempotent setup script that copies the same template twice.","commonSituations":"Deploy/bootstrap scripts copying default configs that must not clobber user edits; backup routines that intentionally refuse overwrite; CI steps re-executed against a dirty workspace.","solutions":["If overwrite is acceptable, drop COPYFILE_EXCL - the default overwrites silently","Pre-check fs.existsSync(dest) (or lstatSync) and skip or rename per your semantics","Wrap in try-catch and treat this message as the already-copied signal, since the polyfill error has no code field"],"exampleFix":"// before\nimport fs from 'node:fs';\nfs.copyFileSync(src, dest, fs.constants.COPYFILE_EXCL); // throws on rerun\n\n// after\nif (!fs.existsSync(dest)) {\n  fs.copyFileSync(src, dest, fs.constants.COPYFILE_EXCL);\n}","handlingStrategy":"validation","validationCode":"import fs from 'node:fs';\nfunction copyExclusive(src: string, dest: string): void {\n  if (fs.existsSync(dest)) return; // or rename/abort per your semantics\n  fs.copyFileSync(src, dest, fs.constants.COPYFILE_EXCL);\n}","typeGuard":"function destinationIsFree(dest: string): boolean {\n  return !fs.existsSync(dest);\n}","tryCatchPattern":"try {\n  fs.copyFileSync(src, dest, fs.constants.COPYFILE_EXCL);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('A file exists at the destination')) {\n    // expected on re-run: idempotent success or explicit skip message\n    return;\n  }\n  throw e;\n}","preventionTips":["Remember the Deno polyfill throws a plain Error without a code field - match on the message","Check the destination with existsSync/lstatSync before using COPYFILE_EXCL in rerunnable scripts","Make scripts idempotent: copy only when the destination is absent, by design rather than by exception"],"tags":["deno","node-compat","fs","copyfile"],"backgroundTag":"file-already-exists","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}