Skip to content

Commit c314b2d

Browse files
authored
fix(ext/node): add path to fs.stat and fs.statSync error (denoland#26037)
1 parent 05868cc commit c314b2d

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

ext/node/polyfills/_fs/_fs_stat.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ export function stat(
383383

384384
Deno.stat(path).then(
385385
(stat) => callback(null, CFISBIS(stat, options.bigint)),
386-
(err) => callback(denoErrorToNodeError(err, { syscall: "stat" })),
386+
(err) =>
387+
callback(
388+
denoErrorToNodeError(err, { syscall: "stat", path: getPathname(path) }),
389+
),
387390
);
388391
}
389392

@@ -417,9 +420,16 @@ export function statSync(
417420
return;
418421
}
419422
if (err instanceof Error) {
420-
throw denoErrorToNodeError(err, { syscall: "stat" });
423+
throw denoErrorToNodeError(err, {
424+
syscall: "stat",
425+
path: getPathname(path),
426+
});
421427
} else {
422428
throw err;
423429
}
424430
}
425431
}
432+
433+
function getPathname(path: string | URL) {
434+
return typeof path === "string" ? path : path.pathname;
435+
}

tests/unit_node/fs_test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
cp,
2727
FileHandle,
2828
open,
29+
stat,
2930
writeFile,
3031
} from "node:fs/promises";
3132
import process from "node:process";
@@ -123,6 +124,48 @@ Deno.test(
123124
},
124125
);
125126

127+
Deno.test(
128+
"[node/fs statSync] throw error with path information",
129+
() => {
130+
const file = "non-exist-file";
131+
const fileUrl = new URL(file, import.meta.url);
132+
133+
assertThrows(() => {
134+
statSync(file);
135+
}, "Error: ENOENT: no such file or directory, stat 'non-exist-file'");
136+
137+
assertThrows(() => {
138+
statSync(fileUrl);
139+
}, `Error: ENOENT: no such file or directory, stat '${fileUrl.pathname}'`);
140+
},
141+
);
142+
143+
Deno.test(
144+
"[node/fs/promises stat] throw error with path information",
145+
async () => {
146+
const file = "non-exist-file";
147+
const fileUrl = new URL(file, import.meta.url);
148+
149+
try {
150+
await stat(file);
151+
} catch (error: unknown) {
152+
assertEquals(
153+
`${error}`,
154+
"Error: ENOENT: no such file or directory, stat 'non-exist-file'",
155+
);
156+
}
157+
158+
try {
159+
await stat(fileUrl);
160+
} catch (error: unknown) {
161+
assertEquals(
162+
`${error}`,
163+
`Error: ENOENT: no such file or directory, stat '${fileUrl.pathname}'`,
164+
);
165+
}
166+
},
167+
);
168+
126169
Deno.test(
127170
"[node/fs/promises cp] copy file",
128171
async () => {

0 commit comments

Comments
 (0)