Skip to content

Commit 24218bf

Browse files
committed
process: add notice message when process.cwd() failed.
1 parent 6dfc827 commit 24218bf

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/node_process_methods.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,16 @@ static void Cwd(const FunctionCallbackInfo<Value>& args) {
163163
size_t cwd_len = sizeof(buf);
164164
int err = uv_cwd(buf, &cwd_len);
165165
if (err) {
166-
std::string msg =
166+
std::string err_msg =
167167
std::string("process.cwd failed with error ") + uv_strerror(err);
168-
return env->ThrowUVException(err, "uv_cwd", msg.c_str());
168+
if (err == UV_ENOENT) {
169+
// If err == UV_ENOENT it is necessary to notice the user
170+
// that the current working dir was likely removed.
171+
err_msg = err_msg +
172+
std::string(", the current working directory was likely removed ") +
173+
std::string("without changing the working directory");
174+
}
175+
return env->ThrowUVException(err, "uv_cwd", err_msg.c_str());
169176
}
170177
Local<String> cwd;
171178
if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, cwd_len)

test/known_issues/test-cwd-enoent-file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (process.argv[2] === 'child') {
2323
process.chdir(dir);
2424
fs.rmdirSync(dir);
2525
assert.throws(process.cwd,
26-
/^Error: ENOENT: process\.cwd failed with error no such file or directory, uv_cwd$/);
26+
/^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$/);
2727

2828
const r = cp.spawnSync(process.execPath, [__filename, 'child']);
2929

test/parallel/test-cwd-enoent-improved-message.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ assert.throws(
2626
() => process.cwd(),
2727
{
2828
code: 'ENOENT',
29-
message: 'ENOENT: process.cwd failed with error no such file or directory, uv_cwd',
29+
message: 'ENOENT: process.cwd failed with error no such file or directory,' +
30+
' the current working directory was likely removed without changing the working directory, uv_cwd',
3031
}
3132
);

0 commit comments

Comments
 (0)