Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copySync error when a subdirectory is a symbol link #937

Closed
elgs opened this issue Jan 7, 2022 · 4 comments
Closed

copySync error when a subdirectory is a symbol link #937

elgs opened this issue Jan 7, 2022 · 4 comments

Comments

@elgs
Copy link

elgs commented Jan 7, 2022

  • Operating System: macOS Monterey 12.1
  • Node.js version: v17.3.0
  • fs-extra version: 10.0.0

I have the following directory structure (src/lib is a symbol link):

├── build
│   └── lib -> ../lib
├── build.js
├── lib
└── src
    └── lib -> ../lib

And build.js:

const fse = require('fs-extra');
fse.copySync('src/', 'build/');

Basically I want to copy everything inside src to build, and I only want to copy the src/lib as a symbol link to build, not the content. When I ran build.js, I got the following error:

/private/tmp/e$ node build.js
/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:148
      throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
      ^

Error: Cannot copy '../lib' to a subdirectory of itself, '../lib'.
    at onLink (/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:148:13)
    at getStats (/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:49:45)
    at startCopy (/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:38:10)
    at copyDirItem (/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:122:10)
    at /private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:115:39
    at Array.forEach (<anonymous>)
    at copyDir (/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:115:23)
    at onDir (/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:105:10)
    at getStats (/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:45:37)
    at handleFilterAndCopy (/private/tmp/e/node_modules/fs-extra/lib/copy-sync/copy-sync.js:33:10)

I am aware of the dereference: true option, but that would copy everything from the lib to the build directory, which is not what I wanted.

@elgs
Copy link
Author

elgs commented Jan 7, 2022

Now I am doing a workaround like this, and it seems to work, but ideally the library should(if it could) handle this situation:

const fse = require('fs-extra');

const filter = (src, dest) => {
   const destStats = fse.existsSync(dest) && fse.lstatSync(dest);
   return !destStats?.isSymbolicLink?.();
};

fse.copySync('src/', 'build/', { filter });

@RyanZim
Copy link
Collaborator

RyanZim commented Jan 7, 2022

The error message isn't terribly clear here, but basically build/lib already exists as a symlink, so when you try to copy to it, you're effectively copying to lib. Of course, you can't make lib itself a symlink to itself, so it errors out. This would work fine if build/lib didn't already exist.

@elgs
Copy link
Author

elgs commented Jan 7, 2022

@RyanZim yeah it works exactly as what you said. So is there a way to overwrite a symbol link in this case?

@RyanZim
Copy link
Collaborator

RyanZim commented Jan 7, 2022

Not directly; your best bet would be to remove it first.

@RyanZim RyanZim closed this as completed Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants