Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): add the missing path information when the file is not found in fs.stat and fs.statSync #26037

Merged
merged 7 commits into from
Oct 27, 2024
Merged
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
14 changes: 12 additions & 2 deletions ext/node/polyfills/_fs/_fs_stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ export function stat(

Deno.stat(path).then(
(stat) => callback(null, CFISBIS(stat, options.bigint)),
(err) => callback(denoErrorToNodeError(err, { syscall: "stat" })),
(err) =>
callback(
denoErrorToNodeError(err, { syscall: "stat", path: getPathname(path) }),
),
);
}

Expand Down Expand Up @@ -417,9 +420,16 @@ export function statSync(
return;
}
if (err instanceof Error) {
throw denoErrorToNodeError(err, { syscall: "stat" });
throw denoErrorToNodeError(err, {
syscall: "stat",
path: getPathname(path),
});
} else {
throw err;
}
}
}

function getPathname(path: string | URL) {
return typeof path === "string" ? path : path.pathname;
}
43 changes: 43 additions & 0 deletions tests/unit_node/fs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
cp,
FileHandle,
open,
stat,
writeFile,
} from "node:fs/promises";
import process from "node:process";
Expand Down Expand Up @@ -120,6 +121,48 @@ Deno.test(
},
);

Deno.test(
"[node/fs statSync] throw error with path information",
() => {
const file = "non-exist-file";
const fileUrl = new URL(file, import.meta.url);

assertThrows(() => {
statSync(file);
}, "Error: ENOENT: no such file or directory, stat 'non-exist-file'");

assertThrows(() => {
statSync(fileUrl);
}, `Error: ENOENT: no such file or directory, stat '${fileUrl.pathname}'`);
},
);

Deno.test(
"[node/fs/promises stat] throw error with path information",
async () => {
const file = "non-exist-file";
const fileUrl = new URL(file, import.meta.url);

try {
await stat(file);
} catch (error: unknown) {
assertEquals(
`${error}`,
"Error: ENOENT: no such file or directory, stat 'non-exist-file'",
);
}

try {
await stat(fileUrl);
} catch (error: unknown) {
assertEquals(
`${error}`,
`Error: ENOENT: no such file or directory, stat '${fileUrl.pathname}'`,
);
}
},
);

Deno.test(
"[node/fs/promises cp] copy file",
async () => {
Expand Down