Commit 85eb0c704b6 for nodejs
commit 85eb0c704b69a0e2a85c29bedde718a2c00e80bf
Author: Yohannes Desta <108905193+yohannesjoya@users.noreply.github.com>
Date: Tue Nov 24 03:46:39 2026 +0300
fs: stat src through symlinks in cpSync() when dereference is set
CpSyncCheckPaths() used symlink_status() for src when dereference was
true and status() when it was false, the reverse of what the option
means. #59681 fixed the same swap for dest but left src as it was.
A symlink to a file passed as src with dereference: true was treated
as a directory, so cpSync() threw ERR_FS_EISDIR unless recursive was
set, while fs.cp() copied the file.
Swap the two calls and drop the check that counted a symlink as a
directory, which status() can no longer return.
Refs: https://github.com/nodejs/node/issues/59168
Refs: https://github.com/nodejs/node/pull/60945
Co-authored-by: Nicholas Paun <npaun@cloudflare.com>
Signed-off-by: yohannesjoya <yohannesdestagebru10@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67340
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
diff --git a/src/node_file.cc b/src/node_file.cc
index 26be951240a..aee9882ccd6 100644
--- a/src/node_file.cc
+++ b/src/node_file.cc
@@ -4567,8 +4567,8 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
std::error_code error_code;
auto src_status = dereference
- ? std::filesystem::symlink_status(src_path, error_code)
- : std::filesystem::status(src_path, error_code);
+ ? std::filesystem::status(src_path, error_code)
+ : std::filesystem::symlink_status(src_path, error_code);
if (error_code) {
#ifdef _WIN32
@@ -4586,9 +4586,7 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
bool dest_exists = !error_code && dest_status.type() !=
std::filesystem::file_type::not_found;
- bool src_is_dir =
- (src_status.type() == std::filesystem::file_type::directory) ||
- (dereference && src_status.type() == std::filesystem::file_type::symlink);
+ bool src_is_dir = src_status.type() == std::filesystem::file_type::directory;
auto src_path_str = ConvertPathToUTF8(src_path);
auto dest_path_str = ConvertPathToUTF8(dest_path);
diff --git a/test/parallel/test-fs-cp-dereference-symlink-to-file.mjs b/test/parallel/test-fs-cp-dereference-symlink-to-file.mjs
new file mode 100644
index 00000000000..8c921630366
--- /dev/null
+++ b/test/parallel/test-fs-cp-dereference-symlink-to-file.mjs
@@ -0,0 +1,42 @@
+// This tests that cp() and cpSync() copy the target file of a symlink passed
+// as src when dereference is true and recursive is not set.
+
+import { mustCall, mustNotMutateObjectDeep } from '../common/index.mjs';
+import { nextdir } from '../common/fs.js';
+import assert from 'node:assert';
+import {
+ cp,
+ cpSync,
+ lstatSync,
+ mkdirSync,
+ readFileSync,
+ symlinkSync,
+ writeFileSync,
+} from 'node:fs';
+import { join } from 'node:path';
+import tmpdir from '../common/tmpdir.js';
+
+tmpdir.refresh();
+
+const src = nextdir();
+mkdirSync(src);
+writeFileSync(join(src, 'foo.txt'), 'foo');
+symlinkSync(join(src, 'foo.txt'), join(src, 'link.txt'));
+
+const opts = mustNotMutateObjectDeep({ dereference: true });
+
+{
+ const dest = join(nextdir(), 'foo.txt');
+ cpSync(join(src, 'link.txt'), dest, opts);
+ assert(lstatSync(dest).isFile());
+ assert.strictEqual(readFileSync(dest, 'utf8'), 'foo');
+}
+
+{
+ const dest = join(nextdir(), 'foo.txt');
+ cp(join(src, 'link.txt'), dest, opts, mustCall((err) => {
+ assert.ifError(err);
+ assert(lstatSync(dest).isFile());
+ assert.strictEqual(readFileSync(dest, 'utf8'), 'foo');
+ }));
+}