From 6dfc82753a91a691250a9307a3046a45791322b0 Mon Sep 17 00:00:00 2001 From: TseIan Date: Sun, 21 Dec 2025 18:38:29 +0800 Subject: [PATCH] process: improve process.cwd() error message --- src/node_process_methods.cc | 5 +-- test/known_issues/test-cwd-enoent-file.js | 2 +- .../test-cwd-enoent-improved-message.js | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-cwd-enoent-improved-message.js diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 66c7c1cc713068..10dd48ac46b16b 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -163,9 +163,10 @@ static void Cwd(const FunctionCallbackInfo& 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 = + std::string("process.cwd failed with error ") + uv_strerror(err); + return env->ThrowUVException(err, "uv_cwd", msg.c_str()); } - Local cwd; if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, cwd_len) .ToLocal(&cwd)) { diff --git a/test/known_issues/test-cwd-enoent-file.js b/test/known_issues/test-cwd-enoent-file.js index 6d99987895baf4..aaa5e472af0ae3 100644 --- a/test/known_issues/test-cwd-enoent-file.js +++ b/test/known_issues/test-cwd-enoent-file.js @@ -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']); diff --git a/test/parallel/test-cwd-enoent-improved-message.js b/test/parallel/test-cwd-enoent-improved-message.js new file mode 100644 index 00000000000000..7e8bb24eac5bd3 --- /dev/null +++ b/test/parallel/test-cwd-enoent-improved-message.js @@ -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', + } +);