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
5 changes: 3 additions & 2 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ 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 msg =
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err == ENOENT {
        msg = msg + std::string(", the current working directory was likely removed without changing the working directory")
}

std::string("process.cwd failed with error ") + uv_strerror(err);
return env->ThrowUVException(err, "uv_cwd", 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, uv_cwd$/);

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

Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-cwd-enoent-improved-message.js
Original file line number Diff line number Diff line change
@@ -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',
}
);
Loading