Skip to content

Commit

Permalink
src: include cwd in chdir error message
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax committed Jul 17, 2018
1 parent 4107bb4 commit cf37945
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/node_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ using v8::Value;
// used in Hrtime() below
#define NANOS_PER_SEC 1000000000

#ifdef _WIN32
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
#define CHDIR_BUFSIZE (MAX_PATH * 4)
#else
#define CHDIR_BUFSIZE (PATH_MAX)
#endif

void Abort(const FunctionCallbackInfo<Value>& args) {
Abort();
}
Expand All @@ -59,8 +66,14 @@ void Chdir(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString());
Utf8Value path(env->isolate(), args[0]);
int err = uv_chdir(*path);
if (err)
return env->ThrowUVException(err, "chdir", nullptr, *path, nullptr);
if (err) {
// Also include the original working directory, since that will usually
// be helpful information when debugging a `chdir()` failure.
char buf[CHDIR_BUFSIZE];
size_t cwd_len = sizeof(buf);
uv_cwd(buf, &cwd_len);
return env->ThrowUVException(err, "chdir", nullptr, buf, *path);
}
}

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

void Cwd(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
#ifdef _WIN32
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
char buf[MAX_PATH * 4];
#else
char buf[PATH_MAX];
#endif

char buf[CHDIR_BUFSIZE];
size_t cwd_len = sizeof(buf);
int err = uv_cwd(buf, &cwd_len);
if (err)
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-process-chdir-errormessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ common.expectsError(
{
type: Error,
code: 'ENOENT',
message: "ENOENT: no such file or directory, chdir 'does-not-exist'",
message: /ENOENT: no such file or directory, chdir .+ -> 'does-not-exist'/,
path: process.cwd(),
syscall: 'chdir',
dest: 'does-not-exist'
}
);

0 comments on commit cf37945

Please sign in to comment.