isaacs/rimraf

Callback invoked with `null` if no targets found

Zsar opened this issue · 2 comments

Zsar commented

Calling rimraf('dist', error => console.error("Could not delete 'dist' folder: '" + error + "'\n")) without dist folder will print "Could not delete 'dist' folder: 'null'".

One has to write rimraf('dist', error => error && console.error("Could not delete 'dist' folder: '" + error + "'\n")) instead.

According to documentation "If the file doesn't exist, rimraf will return successfully, since your desired outcome is already the case."

I'd expect no error in this case (not even null). - Is this a reasonable expectation? If not, should the error not be non-null?

rimraf/rimraf.js

Lines 77 to 78 in 9219c93

if (n === 0)
return cb()

It is not a reasonable expectation.

It will always return. If the error is set to some Error value, then it means a problem happened, and the file(s) could not be deleted. If the error is not set to an Error value, then it means that it's done deleting the file(s) specified.

This is the same as all Node.js-style callbacks. For example, fs.unlink('file.txt', (err) => { /* if err is an Error, it wasn't deleted. if err is null, then it was deleted, and should be gone now. */ })

Otherwise how would you know when it's done deleting? It doesn't return a Promise.

Checking to see if the file is there takes some non-zero amount of time. If it just never called the callback, then code like this:

rimraf('some-file-that-may-exist.txt', (er) => {
  if (er) {
    console.error('oh no could not delete!")
  } else {
    console.log('file has been deleted (or was never there to begin with)')
  }
})

would never resolve.