nodejs / nodejs/node

fs.cp and fs.cpSync fail to repeatedly copy directory with symlinks

未关闭
#65,097 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

fs
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v24.16.0

Platform
Linux machine 7.1.3-cachyos-lto #1-NixOS SMP PREEMPT_DYNAMIC Tue Jan  1 00:00:00 UTC 1980 x86_64 GNU/Linux
Subsystem

fs

What steps will reproduce the bug?

This script reproduces the issue:

root="$(mktemp -d)"
trap 'rm -rf "$root"' EXIT

mkdir "$root/dir" "$root/foo"
ln -s "$root/dir" "$root/foo/link"

ROOT="$root" node <<'NODE'
const fs = require("node:fs");
const root = process.env.ROOT;

fs.cpSync(`${root}/foo`, `${root}/bar`, { recursive: true }); // succeeds
fs.cpSync(`${root}/foo`, `${root}/bar`, { recursive: true }); // throws
NODE
How often does it reproduce? Is there a required condition?

Requires a copying repeatedly a dir containing a symlink to another dir.

What is the expected behavior? Why is that the expected behavior?

The second copy should succeed, replacing bar/link or leaving it unchanged.

What do you see instead?
node:internal/fs/cp/cp-sync:145
    return fsBinding.cpSyncCopyDir(src, dest,
                     ^

Error: Cannot copy /tmp/tmp.61ap1tNPej/dir to a subdirectory of self /tmp/tmp.61ap1tNPej/dir
    at copyDir (node:internal/fs/cp/cp-sync:145:22)
    at onDir (node:internal/fs/cp/cp-sync:137:10)
    at getStats (node:internal/fs/cp/cp-sync:68:12)
    at cpSyncFn (node:internal/fs/cp/cp-sync:58:10)
    at Object.cpSync (node:fs:3158:3)
    at [stdin]:5:4
    at runScriptInThisContext (node:internal/vm:219:10)
    at node:internal/process/execution:451:12
    at [stdin]-wrapper:6:24
    at runScriptInContext (node:internal/process/execution:449:60) {
  code: 'ERR_FS_CP_EINVAL'
}

Node.js v24.16.0
Additional information

Also recreated in:

  • Node.js v22.23.1
  • Node.js v26.7.0
  • Node.js v27.0.0-nightly20260806de333e856d

The result is the same with force: true, force: false, or errorOnExist. fs.cp() and fs.promises.cp() are also affected.

Modifying bar/link, or pointing it to a different directory, makes the second copy succeed.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先使用 node:fs 进行复现,并跟踪 node:internal/fs/cp/cp-sync 中的堆栈条目,尤其是 copyDir、onDir 和 getStats。检查带有目录符号链接的重复递归复制情况,然后验证 fs.cpSync、fs.cp 和 fs.promises.cp 在不改变已报告的自身子目录行为的情况下成功完成第二次复制。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, nodejs
领域
backend, operating-systems
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
68/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。