You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent fs.rm behavior with "force" option on Windows vs mac/linux #45253
changed the title [-]Inconsistent fs.rm and fs.stat behavior on Windows vs mac/linux[/-][+]Inconsistent fs.rm behavior on Windows vs mac/linux[/+]on Oct 31, 2022
only tested this on a mac, but the behavior described above should be correct, as the path foo/bar does not exist. if this works on windows, I would think that it would need to be fixed for that platform, not the other way around.
@VincentBailly are you sure you used the same paths on windows and linux?
changed the title [-]Inconsistent fs.rm behavior on Windows vs mac/linux[/-][+]Inconsistent fs.rm behavior with "force" option on Windows vs mac/linux[/+]on Nov 1, 2022
@dnalborczyk thank you for the response. The issue was not explicit enough, sorry. Let me clarify.
The issue has to do with the option "force", the documentation says that the "force" option makes the function ignore exceptions when the path does not exist.
$ touch foo
$ rm -f foo/barrm: foo/bar: Not a directory
$ node -e 'fs.promises.rm("foo/bar", {force:true})'node:internal/process/promises:289 triggerUncaughtException(err, true /* fromPromise */); ^[Error: ENOTDIR: not a directory, stat 'foo/bar'] { errno: -20, code: 'ENOTDIR', syscall: 'stat', path: 'foo/bar'}Node.js v20.0.0-pre
It looks like the built-in method also throw in this case. I suggest we update the documentation, I think we should try keeping the behavior consistent with the built-in tool. We should probably fix the Windows behavior then, I don't have a Windows machine to work on that, so if there are any volunteers that's be awesome.
the documentation says that the "force" option makes the function ignore exceptions when the path does not exist.
When true, exceptions will be ignored if path does not exist.
Maybe this part requires rewording to avoid any ambiguity, but it sounds correct to me. force flag tells to ignore ENOENT on the path (i.e. target file or any directory in it), while the error we in this example is ENOTDIR.
We should probably fix the Windows behavior then, I don't have a Windows machine to work on that, so if there are any volunteers that's be awesome.
I'm not sure about direct native equivalent of rm -f in Windows, but it works like this in cygwin:
$ touch foo
$ rm foo/barrm: cannot remove 'foo/bar': Not a directory
$ node -e 'require("fs").promises.rm("foo/bar")'node:internal/process/promises:288 triggerUncaughtException(err, true /* fromPromise */); ^[Error: ENOENT: no such file or directory, stat 'C:\foo\bar'] { errno: -4058, code: 'ENOENT', syscall: 'stat', path: 'C:\\foo\\bar'}
$ rm -f foo/bar[no error]
$ node -e 'require("fs").promises.rm("foo/bar", { force: true })'[no error]
So, behaviour with force: true is consistent. With force: false it throws ENOENT instead.
When true, exceptions will be ignored if path does not exist
The NodeJS documentation says that rm is modeled on the standard POSIX rm utility, which says "if file does not exist and -f is specified, skip this file"
$ rm -rf foo
$ touch foo
$ stat foo/barstat: foo/bar: stat: Not a directory
$ node -e 'fs.statSync("foo/bar")'node:internal/fs/utils:348 throw err; ^Error: ENOTDIR: not a directory, stat 'foo/bar' at Object.statSync (node:fs:1596:3) at [eval]:1:4 at Script.runInThisContext (node:vm:129:12) at Object.runInThisContext (node:vm:313:38) at node:internal/process/execution:79:19 at [eval]-wrapper:6:22 at evalScript (node:internal/process/execution:78:60) at node:internal/main/eval_string:28:3 { errno: -20, syscall: 'stat', code: 'ENOTDIR', path: 'foo/bar'}Node.js v18.11.0
Activity
[-]Inconsistent fs.rm and fs.stat behavior on Windows vs mac/linux[/-][+]Inconsistent fs.rm behavior on Windows vs mac/linux[/+]dnalborczyk commentedon Oct 31, 2022
only tested this on a mac, but the behavior described above should be correct, as the path
foo/bardoes not exist. if this works onwindows, I would think that it would need to be fixed for that platform, not the other way around.@VincentBailly are you sure you used the same paths on
windowsandlinux?[-]Inconsistent fs.rm behavior on Windows vs mac/linux[/-][+]Inconsistent fs.rm behavior with "force" option on Windows vs mac/linux[/+]VincentBailly commentedon Nov 1, 2022
@dnalborczyk thank you for the response. The issue was not explicit enough, sorry. Let me clarify.
The issue has to do with the option "force", the documentation says that the "force" option makes the function ignore exceptions when the path does not exist.
https://nodejs.org/docs/latest-v16.x/api/fs.html#fspromisesrmpath-options
aduh95 commentedon Nov 1, 2022
On macOS:
It looks like the built-in method also throw in this case. I suggest we update the documentation, I think we should try keeping the behavior consistent with the built-in tool. We should probably fix the Windows behavior then, I don't have a Windows machine to work on that, so if there are any volunteers that's be awesome.
/cc @nodejs/fs
LiviaMedeiros commentedon Nov 1, 2022
Maybe this part requires rewording to avoid any ambiguity, but it sounds correct to me.
forceflag tells to ignoreENOENTon the path (i.e. target file or any directory in it), while the error we in this example isENOTDIR.I'm not sure about direct native equivalent of
rm -fin Windows, but it works like this in cygwin:So, behaviour with
force: trueis consistent. Withforce: falseit throwsENOENTinstead.VincentBailly commentedon Nov 1, 2022
If this can help: on Linux (Ubuntu 22.04 & bash 5.0.17 & coreutils 8.30) the behavior is inconsistent between rm and nodejs
VincentBailly commentedon Nov 1, 2022
Here is a summary of what different tools do in this scenario:
The NodeJS documentation says that rm is
modeled on the standard POSIX rm utility, which says "if file does not exist and -f is specified, skip this file"VincentBailly commentedon Nov 1, 2022
Since this is tightly related to the behavior of stat, let's also consider this:
VincentBailly commentedon Nov 1, 2022
It seems that fixing the
statcommand on Windows will be enough to makermconsistent.aduh95 commentedon Nov 1, 2022
On macOS:
Ceres6 commentedon Feb 10, 2025
Is there any work left here?
StefanStojanovic commentedon May 20, 2025
Since the last comment was 3 months ago, and there was no answer, I'll go ahead and close this. Please reopen if needed.