From 0856f42c116b10fd47f09ce7a292a2acd893c136 Mon Sep 17 00:00:00 2001 From: Shaurya Saria Date: Sat, 12 Sep 2026 16:00:43 +0530 Subject: [PATCH] fs: preserve rmSync errno for filesystem errors Signed-off-by: Shaurya Saria --- src/node_file.cc | 9 ++++++--- test/parallel/test-fs-rm.js | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 0179ad12c9d8..29de3b6c1286 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 1d0578e3004d..75ce22fdc40c 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, '');