Skip to content
Open
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
23 changes: 22 additions & 1 deletion ext/node/polyfills/_process/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { nextTick as _nextTick } from "ext:deno_node/_next_tick.ts";
import { _exiting } from "ext:deno_node/_process/exiting.ts";
import * as fs from "ext:deno_fs/30_fs.js";
import {
denoErrorToNodeError,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OBJECT_DEFINE_PROPERTY,
} from "ext:deno_node/internal/errors.ts";
Expand All @@ -51,7 +52,27 @@ export function chdir(directory: string): void {
if (typeof directory !== "string") {
throw new ERR_INVALID_ARG_TYPE("directory", "string", directory);
}
fs.chdir(directory);
// Node's chdir error carries `path` (the cwd before chdir), `dest` (the
// target), and `syscall: 'chdir'`. Snapshot the cwd before attempting the
// change so the error's `path` matches Node's behaviour. If the current
// cwd has been deleted (common in tmpdir cleanup during process exit),
// `fs.cwd()` itself throws -- fall back to an empty string so the wrapper
// still has a sensible `path`, and don't surface the cwd lookup error.
let fromPath = "";
try {
fromPath = fs.cwd();
} catch {
// Ignore -- chdir() below will surface a chdir-shaped error.
}
try {
fs.chdir(directory);
} catch (err) {
throw denoErrorToNodeError(err as Error, {
syscall: "chdir",
path: fromPath,
dest: directory,
});
}
}

/** https://nodejs.org/api/process.html#process_process_cwd */
Expand Down
1 change: 1 addition & 0 deletions tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,7 @@
"parallel/test-process-beforeexit-throw-exit.js": {},
"parallel/test-process-binding-internalbinding-allowlist.js": {},
"parallel/test-process-binding.js": {},
"parallel/test-process-chdir-errormessage.js": {},
"parallel/test-process-chdir.js": {},
"parallel/test-process-config.js": {},
"parallel/test-process-constants-noatime.js": {
Expand Down
17 changes: 10 additions & 7 deletions tests/unit_node/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,19 @@ Deno.test({
Deno.test({
name: "process.chdir failure",
fn() {
assertThrows(
// process.chdir now wraps Deno errors into Node-shaped uv errors with
// syscall/path/dest, so a missing directory throws a plain Error with
// code "ENOENT" rather than Deno.errors.NotFound.
const err = assertThrows(
() => {
process.chdir("non-existent-directory-name");
},
Deno.errors.NotFound,
"file",
// On every OS Deno returns: "No such file" except for Windows, where it's:
// "The system cannot find the file specified. (os error 2)" so "file" is
// the only common string here.
);
Error,
"ENOENT",
) as Error & { code?: string; syscall?: string; dest?: string };
assertEquals(err.code, "ENOENT");
assertEquals(err.syscall, "chdir");
assertEquals(err.dest, "non-existent-directory-name");
},
});

Expand Down
Loading