nodejs / nodejs/node

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

オープン
#65,097 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

fs
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

node:fsを使って再現するところから始め、node:internal/fs/cp/cp-syncのスタックエントリ、特にcopyDir、onDir、getStatsを追ってください。ディレクトリのシンボリックリンクを使った再帰コピーの繰り返しのケースを確認し、その後、fs.cpSync、fs.cp、fs.promises.cpが自己サブディレクトリについて報告されている動作を変更せずに、2回目のコピーを正常に完了することを検証してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, nodejs
領域
backend, operating-systems
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
68/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。