process: improve process.cwd() error message

PR-URL: https://github.com/nodejs/node/pull/61164
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: theanarkh <theratliter@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
TseIan authored and RafaelGSS committed 2026-01-14 17:21:25 -03:00
1 parent 9b136db814
commit ca5c4c9752
3 files changed
+43 -3

No files matched your search

+10 -2
View File
@@ -163,9 +163,17 @@ 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 +
", the current working directory was likely removed " +
"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)) {
+1 -1
View File
@@ -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']);
@@ -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',
}
);