Skip to content

Check if path exists before returning from opendirSync - #17846

Closed
evanrittenhouse wants to merge 1 commit into
oven-sh:mainfrom
evanrittenhouse:er/opendirsync
Closed

Check if path exists before returning from opendirSync#17846
evanrittenhouse wants to merge 1 commit into
oven-sh:mainfrom
evanrittenhouse:er/opendirsync

Conversation

@evanrittenhouse

@evanrittenhouse evanrittenhouse commented Mar 2, 2025

Copy link
Copy Markdown

Closes #17581

What does this PR do?

  • Documentation or TypeScript types (it's okay to leave the rest blank in this case)
  • Code changes

How did you verify your code works?

One thing I don't quite see a pattern for yet is returning a system error (e.g. ENOENT) from JS code, but it seems like the typical way to do that is by putting the implementation into the Zig code.

I figured I'd raise the PR while I look at how to do that in case there's a way to do it from JS though

@evanrittenhouse
evanrittenhouse marked this pull request as draft March 2, 2025 05:44
@DonIsaac
DonIsaac requested a review from paperclover March 3, 2025 02:09

@paperclover paperclover left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem with this approach is it will not set error.code.

Comment thread src/js/node/fs.ts
function opendirSync(path, options) {
// TODO: validatePath
// validateString(path, "path");
if (!fs.existsSync(path)) {

This comment was marked as outdated.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accessSync won't error if it's not a directory

@paperclover paperclover Mar 3, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. then instead, this PR should expose an opendir binding in src/bun.js/node/node_fs_binding.zig to fs.ts, and have a matching implementation function in src/bun.js/node/node_fs.zig. this function would return a directory file descriptor. Then, there can be internal variants of readdir that work on this file descriptor.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointers. I figured this would involve some Zig bindings, but wanted to ensure that there wasn't anything in JS-land that I was missing. I'm starting to work on the bindings now, should hopefully have something up this week, time permitting

describe("opendirSync", () => {
it("should throw ENOENT on a nonexistent directory", () => {
const dirName = Math.random().toString(8);
expect(() => opendirSync(dirName)).toThrow("ENOENT");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toThrow({ code: "ENOENT" })

@evanrittenhouse

Copy link
Copy Markdown
Author

Sorry, work has been nuts. I've been making progress on this but haven't quite gotten the time to finish it up yet

@robobun

robobun commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Thanks for picking this up.

This was fixed on main in #31830: opendir / opendirSync / fs.promises.opendir now open the directory eagerly, and a missing path throws a proper system error (ENOENT, syscall: "opendir", with code / errno / path set) rather than returning a Dir. #17581 is closed. Verified on current main:

$ bun -e "require('fs').opendirSync('xxxx')"
ENOENT: no such file or directory, opendir 'xxxx'

Since the bug this draft targeted is fixed, closing it.

@robobun robobun closed this Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opendirSync successfully returns for a non-existent directory

4 participants