{"record":{"id":"174475bb2e4d2706","repo":"denoland/deno","slug":"the-source-and-destination-are-the-same-file","errorCode":null,"errorMessage":"the source and destination are the same file","messagePattern":"the source and destination are the same file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ext/fs/std_fs.rs","lineNumber":918,"sourceCode":"    {\n      return cp_(\n        source_meta,\n        from,\n        &to.join(from.file_name().ok_or_else(|| {\n          io::Error::new(\n            io::ErrorKind::InvalidInput,\n            \"the source path is not a valid file\",\n          )\n        })?),\n      );\n    }\n  }\n\n  if let Ok(m) = fs::symlink_metadata(to)\n    && is_identical(&source_meta, &m)\n  {\n    return Err(\n      io::Error::new(\n        io::ErrorKind::InvalidInput,\n        \"the source and destination are the same file\",\n      )\n      .into(),\n    );\n  }\n\n  cp_(source_meta, from, to)\n}\n\n#[cfg(not(windows))]\nfn stat(path: &Path) -> FsResult<FsStat> {\n  let metadata = fs::metadata(path)?;\n  Ok(FsStat::from_std(metadata))\n}\n\n#[cfg(windows)]\nfn stat(path: &Path) -> FsResult<FsStat> {","sourceCodeStart":900,"sourceCodeEnd":936,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fs/std_fs.rs#L900-L936","documentation":"Before dispatching a copy, Deno lstats both paths and compares identity with is_identical(): inode equality on Unix; on Windows creation_time + last_write_time equality (a heuristic, since a stable file_index() is not exposed by std). On a match it refuses with io::ErrorKind::InvalidInput 'the source and destination are the same file' (ext/fs/std_fs.rs:918).","triggerScenarios":"Destination is a hard link or symlink to the source (same inode on Unix); equivalent path spellings; on Windows, the timestamp heuristic can misfire on two genuinely different files that share creation and modification times — e.g. files extracted from the same archive or emitted by one code-generator run.","commonSituations":"Copy scripts where TO is inside FROM through links; CI artifacts with mass-identical timestamps; false positives reported on Windows after zip extraction preserves mtimes.","solutions":["Skip when the paths truly are the same file: compare Deno.realPathSync(from) === Deno.realPathSync(to)","On Windows false positives: remove the stale destination first (Deno.removeSync(to)) so the metadata comparison no longer matches","Compare size/content hash yourself when timestamps are suspect","If distinct files keep being flagged, report it — the Windows check is time-based by design"],"exampleFix":"// before\nawait Deno.copy(src, dst); // false positive: same timestamps on Windows\n\n// after\nif (Deno.build.os === 'windows') {\n  try { Deno.removeSync(dst); } catch { /* not present */ }\n}\nawait Deno.copy(src, dst);","handlingStrategy":"validation","validationCode":"function sameRealPath(from: string, to: string): boolean {\n  try {\n    return Deno.realPathSync(from) === Deno.realPathSync(to);\n  } catch {\n    return false;\n  }\n}\n\nif (!sameRealPath(src, dst)) {\n  if (Deno.build.os === 'windows') {\n    try { Deno.removeSync(dst); } catch { /* absent */ } // defeat timestamp heuristic\n  }\n  await Deno.copy(src, dst);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await Deno.copy(src, dst);\n} catch (e) {\n  if (e instanceof Error && /the same file/.test(e.message)) {\n    // distinguish real duplicates (same realPath) from Windows timestamp false positives\n    if (Deno.realPathSync(src) === Deno.realPathSync(dst)) return;\n    Deno.removeSync(dst);\n    await Deno.copy(src, dst);\n    return;\n  }\n  throw e;\n}","preventionTips":["Compare realPathSync of source and destination before copying","On Windows, remove the stale destination first — the same-file check compares timestamps","Watch for hard links/symlinks making distinct-looking paths the same inode"],"tags":["filesystem","copy","same-file","windows","timestamps","invalid-input"],"backgroundTag":"copy-same-file","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}