jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.43k stars 775 forks source link

copySync fails when there is a symlink in the source directory and the operation has already been done once. #1043

Closed a-lcardos closed 2 months ago

a-lcardos commented 2 months ago

Situation: when I have a directory "/tmp/a" that has a symlink and I try to copy this directory to "/tmp/b" which has the same symlink copySync fails.

✗ ls -lah /tmp/a
total 0
drwxr-xr-x   3 l  wheel    96B  1 Jul 09:22 .
drwxrwxrwt  17 root     wheel   544B  1 Jul 09:23 ..
lrwxr-xr-x   1 l  wheel    14B  1 Jul 09:19 symlink-to-real-file -> /tmp/real-file
✗ ls -lah /tmp/b
total 0
drwxr-xr-x   3 l  wheel    96B  1 Jul 09:25 .
drwxrwxrwt  17 root     wheel   544B  1 Jul 09:23 ..
lrwxr-xr-x   1 l  wheel    14B  1 Jul 09:20 symlink-to-real-file -> /tmp/real-file

The code:

copySync('/tmp/a', '/tmp/b');

Throws:

/Volumes/workplace/TestBug/node_modules/fs-extra/lib/copy/copy-sync.js:143
      throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
      ^

Error: Cannot copy '/tmp/real-file' to a subdirectory of itself, '/tmp/real-file'.

The same call succeeds if we have a file instead of a symlink.

RyanZim commented 2 months ago

This is a known bug; previously reported in https://github.com/jprichardson/node-fs-extra/issues/1019.