Skip to content

Commit c10cf51

Browse files
authored
fix(ext/node): do not throw NotFound for fs.exists (#34244)
1 parent 5ca12ed commit c10cf51

3 files changed

Lines changed: 47 additions & 10 deletions

File tree

ext/node/ops/fs.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,31 +315,41 @@ pub fn op_node_fs_exists_sync(
315315
state: &mut OpState,
316316
#[string] path: &str,
317317
) -> Result<bool, deno_permissions::PermissionCheckError> {
318-
let path = state.borrow_mut::<PermissionsContainer>().check_open(
318+
let path_or_err = state.borrow_mut::<PermissionsContainer>().check_open(
319319
Cow::Borrowed(Path::new(path)),
320320
OpenAccessKind::ReadNoFollow,
321321
Some("node:fs.existsSync()"),
322-
)?;
323-
let fs = state.borrow::<FileSystemRc>();
324-
Ok(fs.exists_sync(&path))
322+
);
323+
match path_or_err {
324+
Ok(path) => {
325+
let fs = state.borrow::<FileSystemRc>();
326+
Ok(fs.exists_sync(&path))
327+
}
328+
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
329+
Err(err) => Err(err),
330+
}
325331
}
326332

327333
#[op2(stack_trace)]
328334
pub async fn op_node_fs_exists(
329335
state: Rc<RefCell<OpState>>,
330336
#[string] path: String,
331337
) -> Result<bool, FsError> {
332-
let (fs, path) = {
338+
let (fs, path_or_err) = {
333339
let mut state = state.borrow_mut();
334-
let path = state.borrow_mut::<PermissionsContainer>().check_open(
340+
let path_or_err = state.borrow_mut::<PermissionsContainer>().check_open(
335341
Cow::Owned(PathBuf::from(path)),
336342
OpenAccessKind::ReadNoFollow,
337343
Some("node:fs.exists()"),
338-
)?;
339-
(state.borrow::<FileSystemRc>().clone(), path)
344+
);
345+
(state.borrow::<FileSystemRc>().clone(), path_or_err)
340346
};
341347

342-
Ok(fs.exists_async(path.into_owned()).await?)
348+
match path_or_err {
349+
Ok(path) => Ok(fs.exists_async(path.into_owned()).await?),
350+
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
351+
Err(err) => Err(FsError::Permission(err)),
352+
}
343353
}
344354

345355
fn get_open_options(flags: i32, mode: Option<u32>) -> OpenOptions {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"args": "run --quiet -P main.ts",
3-
"output": "true\n"
3+
"output": "true\nfalse\nfalse\ntrue\n"
44
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1+
import * as fs from "node:fs";
2+
import * as fsPromises from "node:fs/promises";
3+
14
try {
25
Deno.readTextFileSync("./deno.json");
36
console.log("loaded");
47
} catch (err) {
58
console.log(err instanceof Deno.errors.NotFound);
69
}
10+
11+
try {
12+
console.log(fs.existsSync("./deno.json"));
13+
} catch {
14+
console.log("failed");
15+
}
16+
try {
17+
console.log(
18+
await new Promise((resolve) => {
19+
fs.exists("./deno.json", resolve);
20+
}),
21+
);
22+
} catch {
23+
console.log("failed");
24+
}
25+
26+
try {
27+
await fsPromises.stat("./deno.json");
28+
} catch (err) {
29+
console.log(
30+
err instanceof Error && "code" in err &&
31+
err.code === "ENOENT",
32+
);
33+
}

0 commit comments

Comments
 (0)