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.

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Anna Henningsen 2018-06-25 18:13:37 +02:00
parent 4107bb463e
commit cf37945b12
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 20 additions and 10 deletions

View File

@ -47,6 +47,13 @@ using v8::Value;
// used in Hrtime() below // used in Hrtime() below
#define NANOS_PER_SEC 1000000000 #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) { void Abort(const FunctionCallbackInfo<Value>& args) {
Abort(); Abort();
} }
@ -59,8 +66,14 @@ void Chdir(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString()); CHECK(args[0]->IsString());
Utf8Value path(env->isolate(), args[0]); Utf8Value path(env->isolate(), args[0]);
int err = uv_chdir(*path); int err = uv_chdir(*path);
if (err) if (err) {
return env->ThrowUVException(err, "chdir", nullptr, *path, nullptr); // 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, // CPUUsage use libuv's uv_getrusage() this-process resource usage accessor,
@ -93,13 +106,7 @@ void CPUUsage(const FunctionCallbackInfo<Value>& args) {
void Cwd(const FunctionCallbackInfo<Value>& args) { void Cwd(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
#ifdef _WIN32 char buf[CHDIR_BUFSIZE];
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
char buf[MAX_PATH * 4];
#else
char buf[PATH_MAX];
#endif
size_t cwd_len = sizeof(buf); size_t cwd_len = sizeof(buf);
int err = uv_cwd(buf, &cwd_len); int err = uv_cwd(buf, &cwd_len);
if (err) if (err)

View File

@ -11,6 +11,9 @@ common.expectsError(
{ {
type: Error, type: Error,
code: 'ENOENT', 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'
} }
); );