jprichardson / node-fs-extra

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

Copying a directory to a path that already has a directory with that name does not throw an error even when options are set correctly. #963

Closed george-thomas-hill closed 2 years ago

george-thomas-hill commented 2 years ago

Copying a directory to a path that already has a directory with that name does not throw an error even when options are set correctly.

Here is the relevant output from my terminal:

$ ls -p
destDir/  index.js  node_modules/  package.json  package-lock.json  sourceDir/

$ ls -p sourceDir/
testDir/

$ ls -p destDir/
testDir/

$ cat index.js 
const fs = require('fs-extra');

const source = "./sourceDir/testDir";

const destination = "./destDir/testDir";

const options = {
    overwrite: false,
    errorOnExist: true,
};

fs.copy(source, destination, options, err => {
  if (err) return console.error(err);
  console.log('success!');
});

$ node index.js 
success!

$ node -v
v16.11.0

$ cat package.json 
{
  "name": "fs-extra-demonstration",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fs-extra": "^10.1.0"
  }
}

As you see, fs-extra reports success even though it should have thrown an error, since "testDir" already existed at "destDir".

Please let me know if I'm missing something or if I'm doing something incorrectly!

Thank you.

RyanZim commented 2 years ago

Yep, looks like a bug. @manidlou?

manidlou commented 2 years ago

This is not a bug, it is an expected behavior. For directories, fs-extra copies everything inside the directory (not the directory itself). It is also mentioned in our docs https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md.

To clarify, you're copying the contents of sourceDir/testDir to destDir/testDir, so if a file or directory already exists inside destDir/testDir with the same name as the one inside the sourceDir/testDir, then you would get an error.

RyanZim commented 2 years ago

:man_facepalming: I feel silly; I did know that, but I somehow forgot about it when I replied.