Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1924,9 +1924,12 @@ static void RmSync(const FunctionCallbackInfo<Value>& 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,
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
Expand Down