Closed rhwood closed 3 years ago
How does fs.rm
with force
and/or recursive
options set to true work? I'm wondering if this is a bug in Node itself.
I don't know. I may be doing something wrong.
On macOS 11.3.1 with Node.js 16.1.0, this throws an error as expected:
import * as nodefs from 'fs'
nodefs.mkdirSync('tmp')
nodefs.writeFileSync('tmp/file', 'A file.')
nodefs.chmodSync('tmp', '500')
nodefs.rm('tmp/file', {force: true}, function(err) {
console.log('Expected error message')
if (err) {
console.error(err)
} else {
console.log('Unexpected success')
}
})
but this does not (note same as above except after // clean up
comment:
import * as nodefs from 'fs'
nodefs.mkdirSync('tmp')
nodefs.writeFileSync('tmp/file', 'A file.')
nodefs.chmodSync('tmp', '500')
nodefs.rm('tmp/file', {force: true}, function(err) {
console.log('Expected error message')
if (err) {
console.error(err)
} else {
console.log('Unexpected success')
}
})
// clean up
nodefs.chmodSync('tmp', '700')
nodefs.rm('tmp', {recursive: true, force: true}, function(err) {
if (err) {
console.log('Unexpected error cleaning up')
console.error(err)
}
})
but this does not
For clarity, what's the result of that code?
but this does not
For clarity, what's the result of that code?
Cleaning up...
Expected error message
Unexpected success
which means (I just realized) the rm
is an asynchronous call.
I just submitted nodejs/node#38683 as Node does have this bug also.
Closing as wontfix
, since this is an upstream bug in Node. Node just merged a fix for this: https://github.com/nodejs/node/pull/38684; from what I can tell, we should expect that fix to land in Node v17.
fs-extra
version:Starting with fs-extra 10.0.0 on node 14.14+, failures to rm a file that exists, but is in an unwritable directory, fail silently. fs-extra 9.1.0 throws an error, and fs-extra on node 12 throw an error.
Note that fs.rm in node 14.14+ is documented to fail silently only if the
force
parameter is true and the file to delete does not exist.