Skip to content

Commit d2cf395

Browse files
committed
fs: fix cpSync handle existing symlinks
1 parent 8d603d5 commit d2cf395

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/internal/fs/cp/cp-sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function onLink(destStat, src, dest, verbatimSymlinks) {
199199
if (!isAbsolute(resolvedDest)) {
200200
resolvedDest = resolve(dirname(dest), resolvedDest);
201201
}
202-
if (isSrcSubdir(resolvedSrc, resolvedDest)) {
202+
if (statSync(src).isDirectory() && isSrcSubdir(resolvedSrc, resolvedDest)) {
203203
throw new ERR_FS_CP_EINVAL({
204204
message: `cannot copy ${resolvedSrc} to a subdirectory of self ` +
205205
`${resolvedDest}`,

test/parallel/test-fs-cp.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
writeFileSync,
1616
} = fs;
1717
import net from 'net';
18-
import { join } from 'path';
18+
import { join, resolve } from 'path';
1919
import { pathToFileURL } from 'url';
2020
import { setTimeout } from 'timers/promises';
2121

@@ -248,6 +248,17 @@ function nextdir(dirname) {
248248
);
249249
}
250250

251+
// It allows copying when is not a directory
252+
{
253+
const src = nextdir();
254+
const dest = nextdir();
255+
mkdirSync(src, mustNotMutateObjectDeep({ recursive: true }));
256+
writeFileSync(`${src}/test.txt`, 'test');
257+
symlinkSync(resolve(`${src}/test.txt`), join(src, 'link.txt'));
258+
cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true }));
259+
cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true }));
260+
}
261+
251262
// It throws error if symlink in dest points to location in src.
252263
{
253264
const src = nextdir();

0 commit comments

Comments
 (0)