{"record":{"id":"fd07a4027482aece","repo":"denoland/deno","slug":"err-fs-cp-einval","errorCode":"ERR_FS_CP_EINVAL","errorMessage":"Invalid src or dest","messagePattern":"Invalid src or dest","errorType":"exception","errorClass":"NodeSystemError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_fs/cp/cp.ts","lineNumber":60,"sourceCode":"  IsSrcFile: 1n << 34n,\n  IsSrcCharDevice: 1n << 35n,\n  IsSrcBlockDevice: 1n << 36n,\n  IsSrcSymlink: 1n << 37n,\n  IsSrcSocket: 1n << 38n,\n  IsSrcFifo: 1n << 39n,\n};\n\nconst {\n  Number,\n  PromiseResolve,\n  SymbolAsyncIterator,\n} = primordials;\n\nfunction throwCpError(err) {\n  const { EEXIST, EINVAL, EISDIR, ENOTDIR } = lazyConstants();\n  switch (err.kind) {\n    case \"EINVAL\":\n      throw new ERR_FS_CP_EINVAL({\n        message: err.message,\n        path: err.path,\n        syscall: \"cp\",\n        errno: EINVAL,\n        code: \"EINVAL\",\n      });\n    case \"DIR_TO_NON_DIR\":\n      throw new ERR_FS_CP_DIR_TO_NON_DIR({\n        message: err.message,\n        path: err.path,\n        syscall: \"cp\",\n        errno: EISDIR,\n        code: \"EISDIR\",\n      });\n    case \"NON_DIR_TO_DIR\":\n      throw new ERR_FS_CP_NON_DIR_TO_DIR({\n        message: err.message,\n        path: err.path,","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/_fs/cp/cp.ts#L42-L78","documentation":"throwCpError (ext/node/polyfills/_fs/cp/cp.ts:60) maps a native error with kind 'EINVAL' (produced by op_node_cp_validate_and_prepare during fs.cp/fsp.cp) to ERR_FS_CP_EINVAL. The class's default message is 'Invalid src or dest'; the native path can override it (e.g. src and dest resolving to the same file). It signals the copy request itself is malformed before any data moves.","triggerScenarios":"fs.cp('/a/file.txt', '/a/file.txt') — copying a path onto itself; fs.cpSync with src or dest that the native validation op rejects as invalid (e.g. same resolved identity, invalid combination with dereference).","commonSituations":"Backup/restore code that computes src and dest from the same variable; templating code where a destination pattern failed to substitute and collapsed onto src; copying with dereference:true between hardlinked/identical paths.","solutions":["Verify src and dest resolve to different paths (fs.realpathSync both) before calling cp","Fix the destination computation that left it equal to src","If a no-op copy is legitimate, detect equality first and skip the call"],"exampleFix":"// before\nawait fsp.cp(sourceDir, path.join(outDir, rel), { recursive: true }); // rel === '' makes dest === src\n\n// after\nconst dest = path.join(outDir, rel);\nif (path.resolve(sourceDir) === path.resolve(dest)) return; // nothing to do\nawait fsp.cp(sourceDir, dest, { recursive: true });","handlingStrategy":"try-catch","validationCode":"const samePath = (a, b) => {\n  try { return fs.realpathSync(a) === fs.realpathSync(b); } catch { return path.resolve(a) === path.resolve(b); }\n};\nif (samePath(src, dest)) return; // no-op, avoid ERR_FS_CP_EINVAL","typeGuard":null,"tryCatchPattern":"try {\n  await fsp.cp(src, dest, opts);\n} catch (err) {\n  if (err.code === 'ERR_FS_CP_EINVAL') { /* src/dest identical or invalid — fix paths */ }\n  else throw err;\n}","preventionTips":["Compute dest independently from src variables","path.resolve both paths and compare before copying","Beware dest patterns that collapse to '' via path.join(out, '')"],"tags":["node-compat","fs","cp","einval","copy"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}