diff --git a/src/node_file.cc b/src/node_file.cc index 0179ad12c9d..29de3b6c128 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1924,9 +1924,12 @@ static void RmSync(const FunctionCallbackInfo& args) { permission_denied_error, "rm", message.c_str(), path_c_str); } - std::string message = "Unknown error: " + error.message(); - return env->ThrowErrnoException( - UV_UNKNOWN, "rm", message.c_str(), path_c_str); +#ifdef _WIN32 + int errorno = uv_translate_sys_error(error.value()); +#else + int errorno = -error.value(); +#endif + return env->ThrowUVException(errorno, "rm", nullptr, path_c_str); } int MKDirpSync(uv_loop_t* loop, diff --git a/test/parallel/test-fs-rm.js b/test/parallel/test-fs-rm.js index 1d0578e3004..75ce22fdc40 100644 --- a/test/parallel/test-fs-rm.js +++ b/test/parallel/test-fs-rm.js @@ -225,6 +225,30 @@ if (isGitPresent) { message: /^ENOENT: no such file or directory, lstat/ }); + // Should preserve the errno for errors returned by std::filesystem. + // Refs: https://github.com/nodejs/node/issues/65884 + if (common.isLinux) { + const dotDir = nextDirPath('rm-dot'); + fs.mkdirSync(path.join(dotDir, 'child'), { recursive: true }); + try { + assert.throws(() => { + fs.rmSync(path.join(dotDir, '.'), common.mustNotMutateObjectDeep({ + force: true, + recursive: true, + })); + }, { + code: 'EINVAL', + errno: -22, + syscall: 'rm', + }); + } finally { + fs.rmSync(dotDir, common.mustNotMutateObjectDeep({ + force: true, + recursive: true, + })); + } + } + // Should delete a file const filePath = tmpdir.resolve('rm-file.txt'); fs.writeFileSync(filePath, '');