Skip to content

Commit cf37945

Browse files
committed
src: include cwd in chdir error message
Include the current working directory in the error message for a failing `process.chdir()` since that is usually information relevant for debugging. This is semver-major because it moves properties of the error message object. Inspired by nodejs/help#1355. PR-URL: #21526 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 4107bb4 commit cf37945

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/node_process.cc

+16-9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ using v8::Value;
4747
// used in Hrtime() below
4848
#define NANOS_PER_SEC 1000000000
4949

50+
#ifdef _WIN32
51+
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
52+
#define CHDIR_BUFSIZE (MAX_PATH * 4)
53+
#else
54+
#define CHDIR_BUFSIZE (PATH_MAX)
55+
#endif
56+
5057
void Abort(const FunctionCallbackInfo<Value>& args) {
5158
Abort();
5259
}
@@ -59,8 +66,14 @@ void Chdir(const FunctionCallbackInfo<Value>& args) {
5966
CHECK(args[0]->IsString());
6067
Utf8Value path(env->isolate(), args[0]);
6168
int err = uv_chdir(*path);
62-
if (err)
63-
return env->ThrowUVException(err, "chdir", nullptr, *path, nullptr);
69+
if (err) {
70+
// Also include the original working directory, since that will usually
71+
// be helpful information when debugging a `chdir()` failure.
72+
char buf[CHDIR_BUFSIZE];
73+
size_t cwd_len = sizeof(buf);
74+
uv_cwd(buf, &cwd_len);
75+
return env->ThrowUVException(err, "chdir", nullptr, buf, *path);
76+
}
6477
}
6578

6679
// CPUUsage use libuv's uv_getrusage() this-process resource usage accessor,
@@ -93,13 +106,7 @@ void CPUUsage(const FunctionCallbackInfo<Value>& args) {
93106

94107
void Cwd(const FunctionCallbackInfo<Value>& args) {
95108
Environment* env = Environment::GetCurrent(args);
96-
#ifdef _WIN32
97-
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
98-
char buf[MAX_PATH * 4];
99-
#else
100-
char buf[PATH_MAX];
101-
#endif
102-
109+
char buf[CHDIR_BUFSIZE];
103110
size_t cwd_len = sizeof(buf);
104111
int err = uv_cwd(buf, &cwd_len);
105112
if (err)

test/parallel/test-process-chdir-errormessage.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ common.expectsError(
1111
{
1212
type: Error,
1313
code: 'ENOENT',
14-
message: "ENOENT: no such file or directory, chdir 'does-not-exist'",
14+
message: /ENOENT: no such file or directory, chdir .+ -> 'does-not-exist'/,
15+
path: process.cwd(),
16+
syscall: 'chdir',
17+
dest: 'does-not-exist'
1518
}
1619
);

0 commit comments

Comments
 (0)