From 6dfc82753a91a691250a9307a3046a45791322b0 Mon Sep 17 00:00:00 2001 From: TseIan Date: Sun, 21 Dec 2025 18:38:29 +0800 Subject: [PATCH 1/4] process: improve process.cwd() error message --- src/node_process_methods.cc | 5 +-- test/known_issues/test-cwd-enoent-file.js | 2 +- .../test-cwd-enoent-improved-message.js | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-cwd-enoent-improved-message.js diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 66c7c1cc713068..10dd48ac46b16b 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -163,9 +163,10 @@ static void Cwd(const FunctionCallbackInfo& args) { size_t cwd_len = sizeof(buf); int err = uv_cwd(buf, &cwd_len); if (err) { - return env->ThrowUVException(err, "uv_cwd"); + std::string msg = + std::string("process.cwd failed with error ") + uv_strerror(err); + return env->ThrowUVException(err, "uv_cwd", msg.c_str()); } - Local cwd; if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, cwd_len) .ToLocal(&cwd)) { diff --git a/test/known_issues/test-cwd-enoent-file.js b/test/known_issues/test-cwd-enoent-file.js index 6d99987895baf4..aaa5e472af0ae3 100644 --- a/test/known_issues/test-cwd-enoent-file.js +++ b/test/known_issues/test-cwd-enoent-file.js @@ -23,7 +23,7 @@ if (process.argv[2] === 'child') { process.chdir(dir); fs.rmdirSync(dir); assert.throws(process.cwd, - /^Error: ENOENT: no such file or directory, uv_cwd$/); + /^Error: ENOENT: process\.cwd failed with error no such file or directory, uv_cwd$/); const r = cp.spawnSync(process.execPath, [__filename, 'child']); diff --git a/test/parallel/test-cwd-enoent-improved-message.js b/test/parallel/test-cwd-enoent-improved-message.js new file mode 100644 index 00000000000000..7e8bb24eac5bd3 --- /dev/null +++ b/test/parallel/test-cwd-enoent-improved-message.js @@ -0,0 +1,31 @@ +'use strict'; + +const common = require('../common'); +const { isMainThread } = require('worker_threads'); +const assert = require('assert'); +const fs = require('fs'); + +if (!isMainThread) { + common.skip('process.chdir is not available in Workers'); +} + +// Fails with EINVAL on SmartOS, EBUSY on Windows, EBUSY on AIX. +if (common.isSunOS || common.isWindows || common.isAIX || common.isIBMi) { + common.skip('cannot rmdir current working directory'); +} + +const tmpdir = require('../common/tmpdir'); +const dirname = `${tmpdir.path}/cwd-does-not-exist-${process.pid}`; + +tmpdir.refresh(); +fs.mkdirSync(dirname); +process.chdir(dirname); +fs.rmdirSync(dirname); + +assert.throws( + () => process.cwd(), + { + code: 'ENOENT', + message: 'ENOENT: process.cwd failed with error no such file or directory, uv_cwd', + } +); From 24218bf98aed59fd4b0b9d5bf7046109b05bde47 Mon Sep 17 00:00:00 2001 From: TseIan Date: Fri, 26 Dec 2025 08:02:11 +0800 Subject: [PATCH 2/4] process: add notice message when process.cwd() failed. --- src/node_process_methods.cc | 11 +++++++++-- test/known_issues/test-cwd-enoent-file.js | 2 +- test/parallel/test-cwd-enoent-improved-message.js | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 10dd48ac46b16b..43eed4c4e8eafd 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -163,9 +163,16 @@ static void Cwd(const FunctionCallbackInfo& args) { size_t cwd_len = sizeof(buf); int err = uv_cwd(buf, &cwd_len); if (err) { - std::string msg = + std::string err_msg = std::string("process.cwd failed with error ") + uv_strerror(err); - return env->ThrowUVException(err, "uv_cwd", msg.c_str()); + if (err == UV_ENOENT) { + // If err == UV_ENOENT it is necessary to notice the user + // that the current working dir was likely removed. + err_msg = err_msg + + std::string(", the current working directory was likely removed ") + + std::string("without changing the working directory"); + } + return env->ThrowUVException(err, "uv_cwd", err_msg.c_str()); } Local cwd; if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, cwd_len) diff --git a/test/known_issues/test-cwd-enoent-file.js b/test/known_issues/test-cwd-enoent-file.js index aaa5e472af0ae3..9545ad6873f268 100644 --- a/test/known_issues/test-cwd-enoent-file.js +++ b/test/known_issues/test-cwd-enoent-file.js @@ -23,7 +23,7 @@ if (process.argv[2] === 'child') { process.chdir(dir); fs.rmdirSync(dir); assert.throws(process.cwd, - /^Error: ENOENT: process\.cwd failed with error no such file or directory, uv_cwd$/); + /^Error: ENOENT: process\.cwd failed with error no such file or directory, the current working directory was likely removed without changing the working directory, uv_cwd$/); const r = cp.spawnSync(process.execPath, [__filename, 'child']); diff --git a/test/parallel/test-cwd-enoent-improved-message.js b/test/parallel/test-cwd-enoent-improved-message.js index 7e8bb24eac5bd3..e481627bbadfe3 100644 --- a/test/parallel/test-cwd-enoent-improved-message.js +++ b/test/parallel/test-cwd-enoent-improved-message.js @@ -26,6 +26,7 @@ assert.throws( () => process.cwd(), { code: 'ENOENT', - message: 'ENOENT: process.cwd failed with error no such file or directory, uv_cwd', + message: 'ENOENT: process.cwd failed with error no such file or directory,' + + ' the current working directory was likely removed without changing the working directory, uv_cwd', } ); From 95968df7080a5176b09b273bf426f54e127f1e8f Mon Sep 17 00:00:00 2001 From: TseIan Date: Sat, 27 Dec 2025 08:34:31 +0800 Subject: [PATCH 3/4] process: fix cpp format issue --- src/node_process_methods.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 43eed4c4e8eafd..42a39e8d180a39 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -165,7 +165,7 @@ static void Cwd(const FunctionCallbackInfo& args) { if (err) { std::string err_msg = std::string("process.cwd failed with error ") + uv_strerror(err); - if (err == UV_ENOENT) { + if (err == UV_ENOENT) { // If err == UV_ENOENT it is necessary to notice the user // that the current working dir was likely removed. err_msg = err_msg + From 06cc69775926cfc00b3bbbfbeaff2eeedda72f03 Mon Sep 17 00:00:00 2001 From: TseIan Date: Sat, 27 Dec 2025 17:16:37 +0800 Subject: [PATCH 4/4] process: fix cpp format issue --- src/node_process_methods.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 42a39e8d180a39..a50be07cdb7a0e 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -168,7 +168,8 @@ static void Cwd(const FunctionCallbackInfo& args) { if (err == UV_ENOENT) { // If err == UV_ENOENT it is necessary to notice the user // that the current working dir was likely removed. - err_msg = err_msg + + err_msg = + err_msg + std::string(", the current working directory was likely removed ") + std::string("without changing the working directory"); }