Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,18 @@ static void Cwd(const FunctionCallbackInfo<Value>& args) {
size_t cwd_len = sizeof(buf);
int err = uv_cwd(buf, &cwd_len);
if (err) {
return env->ThrowUVException(err, "uv_cwd");
std::string err_msg =
std::string("process.cwd failed with error ") + uv_strerror(err);
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<String> cwd;
if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, cwd_len)
.ToLocal(&cwd)) {
Expand Down
2 changes: 1 addition & 1 deletion test/known_issues/test-cwd-enoent-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, the current working directory was likely removed without changing the working directory, uv_cwd$/);

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

Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-cwd-enoent-improved-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'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,' +
' the current working directory was likely removed without changing the working directory, uv_cwd',
}
);