From 2397ca2c69311348eec292618e25654b840ebaa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Mon, 10 Feb 2025 12:14:36 -0500 Subject: [PATCH] fs: handle UV_ENOTDIR in `fs.statSync` with `throwIfNoEntry` provided MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/56993 Signed-off-by: Juan José Arboleda --- src/node_file.cc | 11 +++++++++-- test/parallel/test-fs-stat.js | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 8e29bb39887625..3a51551aa184cd 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1088,6 +1088,10 @@ constexpr bool is_uv_error_except_no_entry(int result) { return result < 0 && result != UV_ENOENT; } +constexpr bool is_uv_error_except_no_entry_dir(int result) { + return result < 0 && !(result == UV_ENOENT || result == UV_ENOTDIR); +} + static void Stat(const FunctionCallbackInfo& args) { Realm* realm = Realm::GetCurrent(args); BindingData* binding_data = realm->GetBindingData(); @@ -1121,8 +1125,11 @@ static void Stat(const FunctionCallbackInfo& args) { FS_SYNC_TRACE_BEGIN(stat); int result; if (do_not_throw_if_no_entry) { - result = SyncCallAndThrowIf( - is_uv_error_except_no_entry, env, &req_wrap_sync, uv_fs_stat, *path); + result = SyncCallAndThrowIf(is_uv_error_except_no_entry_dir, + env, + &req_wrap_sync, + uv_fs_stat, + *path); } else { result = SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_stat, *path); } diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index b9d42b5b61bc83..cc7be77e53dadc 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -221,3 +221,8 @@ fs.lstat(__filename, undefined, common.mustCall()); }, ); } + +{ + // Test that the throwIfNoEntry option works and returns undefined + assert.ok(!(fs.statSync('./wont_exists', { throwIfNoEntry: false }))); +}