denoland/deno · error · NodeSystemError

ERR_FS_CP_EINVAL

ERR_FS_CP_EINVAL

Error message

Invalid src or dest

What it means

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.

Source

Thrown at ext/node/polyfills/_fs/cp/cp.ts:60

  IsSrcFile: 1n << 34n,
  IsSrcCharDevice: 1n << 35n,
  IsSrcBlockDevice: 1n << 36n,
  IsSrcSymlink: 1n << 37n,
  IsSrcSocket: 1n << 38n,
  IsSrcFifo: 1n << 39n,
};

const {
  Number,
  PromiseResolve,
  SymbolAsyncIterator,
} = primordials;

function throwCpError(err) {
  const { EEXIST, EINVAL, EISDIR, ENOTDIR } = lazyConstants();
  switch (err.kind) {
    case "EINVAL":
      throw new ERR_FS_CP_EINVAL({
        message: err.message,
        path: err.path,
        syscall: "cp",
        errno: EINVAL,
        code: "EINVAL",
      });
    case "DIR_TO_NON_DIR":
      throw new ERR_FS_CP_DIR_TO_NON_DIR({
        message: err.message,
        path: err.path,
        syscall: "cp",
        errno: EISDIR,
        code: "EISDIR",
      });
    case "NON_DIR_TO_DIR":
      throw new ERR_FS_CP_NON_DIR_TO_DIR({
        message: err.message,
        path: err.path,

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Verify src and dest resolve to different paths (fs.realpathSync both) before calling cp
  2. Fix the destination computation that left it equal to src
  3. If a no-op copy is legitimate, detect equality first and skip the call

Example fix

// before
await fsp.cp(sourceDir, path.join(outDir, rel), { recursive: true }); // rel === '' makes dest === src

// after
const dest = path.join(outDir, rel);
if (path.resolve(sourceDir) === path.resolve(dest)) return; // nothing to do
await fsp.cp(sourceDir, dest, { recursive: true });
Defensive patterns

Strategy: try-catch

Validate before calling

const samePath = (a, b) => {
  try { return fs.realpathSync(a) === fs.realpathSync(b); } catch { return path.resolve(a) === path.resolve(b); }
};
if (samePath(src, dest)) return; // no-op, avoid ERR_FS_CP_EINVAL

Try / catch

try {
  await fsp.cp(src, dest, opts);
} catch (err) {
  if (err.code === 'ERR_FS_CP_EINVAL') { /* src/dest identical or invalid — fix paths */ }
  else throw err;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/fd07a4027482aece. Report an issue: GitHub.