Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
process: improve error message for process.cwd() when directory is de…
…leted

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: #57053
  • Loading branch information
Ankush1oo8 committed Feb 14, 2025
commit 65997120721aca0c6560024397d9fa8854ec411f
14 changes: 12 additions & 2 deletions lib/internal/bootstrap/switches/does_own_process_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Comment thread
Ankush1oo8 marked this conversation as resolved.
Outdated
// 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;
}
6 changes: 4 additions & 2 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ static void Cwd(const FunctionCallbackInfo<Value>& 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().");
Comment thread
Ankush1oo8 marked this conversation as resolved.
Outdated
}
Comment thread
Ankush1oo8 marked this conversation as resolved.

Local<String> cwd = String::NewFromUtf8(env->isolate(),
buf,
Expand Down