Skip to content

fs: allow cp() to replace a symlink with a dangling one - #66954

Open
yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:fs-cp-dangling-symlink
Open

yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:fs-cp-dangling-symlink

Conversation

@yohannesjoya

Copy link
Copy Markdown

fs.promises.cp() and fs.cp() failed with ENOENT when the source is a dangling
symlink and the destination is an existing symlink. onLink() called stat(src) to
decide whether the source is a directory before the subdirectory check, and stat
follows the dangling link. It now reuses srcIsDir, computed a few lines earlier with
internalModuleStat(), which follows the link the same way but does not throw.

Fixes: #66946

The new test fails on main and passes with this change. tools/test.py parallel/test-fs-cp-*
(91 tests) and make lint-js pass locally.

This touches the same line as #65411; whichever lands second needs a small rebase.

AI disclosure: I used a coding agent to help locate the cause and draft the test. I
reproduced the bug on a nightly build and on v22, reviewed the change line by line, and
ran the tests above locally.

onLink() stat()ed the source to check whether it is a directory, which
throws ENOENT when the source is a dangling symlink. Reuse srcIsDir,
which is computed from the same path without throwing.

Fixes: nodejs#66946
Signed-off-by: yohannesjoya <yohannesdestagebru10@gmail.com>
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Oct 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs.cp() fails to replace an existing symlink with a dangling symlink

2 participants