From bbdd237fca6fd219e47c460ca2e5f9c71bf382e3 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 25 Jun 2018 18:13:37 +0200 Subject: [PATCH] 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 https://github.com/nodejs/help/issues/1355. --- src/node_process.cc | 25 ++++++++++++------- .../test-process-chdir-errormessage.js | 5 +++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/node_process.cc b/src/node_process.cc index af19ef2d14653f..020f132f825e00 100644 --- a/src/node_process.cc +++ b/src/node_process.cc @@ -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& args) { Abort(); } @@ -59,8 +66,14 @@ void Chdir(const FunctionCallbackInfo& 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, @@ -93,13 +106,7 @@ void CPUUsage(const FunctionCallbackInfo& args) { void Cwd(const FunctionCallbackInfo& 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) diff --git a/test/parallel/test-process-chdir-errormessage.js b/test/parallel/test-process-chdir-errormessage.js index 0475d7940ce16a..3dca040a2f716c 100644 --- a/test/parallel/test-process-chdir-errormessage.js +++ b/test/parallel/test-process-chdir-errormessage.js @@ -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' } );