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
13 changes: 12 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,17 @@ export function chdir(directory: string): void {
if (typeof directory !== "string") {
throw new ERR_INVALID_ARG_TYPE("directory", "string", directory);
}
fs.chdir(directory);
try {
fs.chdir(directory);
} catch (e) {
// Match Node's error shape: code/syscall/path/dest with a libuv-style
// message ("ENOENT: no such file or directory, chdir '<cwd>' -> '<dest>'").
throw denoErrorToNodeError(e as Error, {
syscall: "chdir",
path: fs.cwd(),
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
14 changes: 7 additions & 7 deletions tests/unit_node/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ Deno.test({
Deno.test({
name: "process.chdir failure",
fn() {
assertThrows(
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