Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that uv_cwd here returns something different from the original cwd? (I am thinking about Windows since libuv calls SetCurrentDirectoryW after SetCurrentDirectoryW and SetEnvironmentVariableW might fail even after SetCurrentDirectoryW succeeds)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung I guess so? It’s not really clear to me how SetEnvironmentVariableW could fail with a runtime error…

I don’t think we really need to worry about it? It’s going to be pretty rare, I think

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'
}
);