From 65997120721aca0c6560024397d9fa8854ec411f Mon Sep 17 00:00:00 2001 From: Ankush1oo8 <154556772+Ankush1oo8@users.noreply.github.com> Date: Sat, 15 Feb 2025 04:51:28 +0530 Subject: [PATCH 1/4] process: improve error message for process.cwd() when directory is deleted If the current working directory is deleted, process.cwd() throws a generic 'uv_cwd' error, which can be confusing. This commit enhances the error message by modifying the original error instead of throwing a new one. PR-URL: https://github.com/nodejs/node/pull/57053 --- .../bootstrap/switches/does_own_process_state.js | 14 ++++++++++++-- src/node_process_methods.cc | 6 ++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index 370da66a825f65..f5c2e09e6b96d3 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -138,7 +138,17 @@ function wrappedUmask(mask) { } function wrappedCwd() { - if (cachedCwd === '') - cachedCwd = rawMethods.cwd(); + if (cachedCwd === '') { + try { + cachedCwd = rawMethods.cwd(); + } catch (e) { + if (e.code === 'ENOENT' && e.syscall === 'uv_cwd') { + // Enhance the error message + e.message = 'Current working directory does not exist - this can happen if the directory ' + + 'was deleted while the process was still inside it. Original error: ' + e.message; + } + throw e; + } + } return cachedCwd; } diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index c67d1ac00a972b..2337d06200135f 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -136,8 +136,10 @@ static void Cwd(const FunctionCallbackInfo& args) { char buf[PATH_MAX_BYTES]; size_t cwd_len = sizeof(buf); int err = uv_cwd(buf, &cwd_len); - if (err) - return env->ThrowUVException(err, "uv_cwd"); + if (err) { + return env->ThrowError("process.cwd() failed: The current working directory no longer exists. " + "Use process.chdir() to change to a valid directory before calling process.cwd()."); + } Local cwd = String::NewFromUtf8(env->isolate(), buf, From b7569228314e8b8e0024dbbd24b899508a91973e Mon Sep 17 00:00:00 2001 From: Ankush1oo8 <154556772+Ankush1oo8@users.noreply.github.com> Date: Fri, 21 Feb 2025 03:32:21 +0530 Subject: [PATCH 2/4] process: move process.cwd() error handling to native side PR-URL: https://github.com/nodejs/node/pull/57053 --- .../bootstrap/switches/does_own_process_state.js | 11 +---------- src/node_errors.h | 5 +++++ src/node_process_methods.cc | 6 +++--- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index f5c2e09e6b96d3..5528df3338e3de 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -139,16 +139,7 @@ function wrappedUmask(mask) { function wrappedCwd() { if (cachedCwd === '') { - try { - cachedCwd = rawMethods.cwd(); - } catch (e) { - if (e.code === 'ENOENT' && e.syscall === 'uv_cwd') { - // Enhance the error message - e.message = 'Current working directory does not exist - this can happen if the directory ' + - 'was deleted while the process was still inside it. Original error: ' + e.message; - } - throw e; - } + cachedCwd = rawMethods.cwd(); } return cachedCwd; } diff --git a/src/node_errors.h b/src/node_errors.h index 801b19fc91810b..6f246e16cd1857 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -37,6 +37,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details); #define ERRORS_WITH_CODE(V) \ V(ERR_ACCESS_DENIED, Error) \ + V(ERR_CWD_DELETED, Error) \ V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error) \ V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \ V(ERR_BUFFER_TOO_LARGE, Error) \ @@ -160,6 +161,10 @@ ERRORS_WITH_CODE(V) V(ERR_ACCESS_DENIED, "Access to this API has been restricted") \ V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \ "Buffer is not available for the current Context") \ + V(ERR_CWD_DELETED, \ + "Current working directory does not exist. " \ + "It may have been deleted while the process was still " \ + "inside it. Use process.chdir() to change to a valid directory.") \ V(ERR_CLOSED_MESSAGE_PORT, "Cannot send data on closed MessagePort") \ V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 2337d06200135f..29af3225e443b5 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -136,9 +136,9 @@ static void Cwd(const FunctionCallbackInfo& args) { char buf[PATH_MAX_BYTES]; size_t cwd_len = sizeof(buf); int err = uv_cwd(buf, &cwd_len); - if (err) { - return env->ThrowError("process.cwd() failed: The current working directory no longer exists. " - "Use process.chdir() to change to a valid directory before calling process.cwd()."); + if (err == UV_ENOENT) { + THROW_ERR_CWD_DELETED(env); + return; } Local cwd = String::NewFromUtf8(env->isolate(), From a49675fc4959e233bf640bc0daf21a5ded936243 Mon Sep 17 00:00:00 2001 From: Ankush1oo8 <154556772+Ankush1oo8@users.noreply.github.com> Date: Sat, 22 Feb 2025 02:26:00 +0530 Subject: [PATCH 3/4] process: improve error message for process.cwd() when directory is deleted also keeping the old code intact --- src/node_process_methods.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 29af3225e443b5..090e3b61070ebd 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -140,6 +140,8 @@ static void Cwd(const FunctionCallbackInfo& args) { THROW_ERR_CWD_DELETED(env); return; } + if (err) + return env->ThrowUVException(err, "uv_cwd"); Local cwd = String::NewFromUtf8(env->isolate(), buf, From 843f44ab0426f929ba2861412ecd059582b308dd Mon Sep 17 00:00:00 2001 From: Ankush1oo8 <154556772+Ankush1oo8@users.noreply.github.com> Date: Sat, 22 Feb 2025 03:32:11 +0530 Subject: [PATCH 4/4] process: improve error message for process.cwd() when directory is deleted with docs change --- doc/api/errors.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/api/errors.md b/doc/api/errors.md index f721ed221eab21..b5f728210a0a7c 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1116,6 +1116,14 @@ added: An attempt to invoke an unsupported crypto operation was made. + + +### `ERR_CWD_DELETED` + +The current working directory has been deleted while the process was still inside it. + +To resolve this, use `process.chdir()` to switch to a valid directory. + ### `ERR_DEBUGGER_ERROR`