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

Check if path exists before returning from opendirSync #17846

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

evanrittenhouse
Copy link

@evanrittenhouse evanrittenhouse commented Mar 2, 2025

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
Copy link
Member

@paperclover paperclover left a comment

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.

@@ -1021,6 +1021,10 @@ function _toUnixTimestamp(time: any, name = "time") {
function opendirSync(path, options) {
// TODO: validatePath
// validateString(path, "path");
if (!fs.existsSync(path)) {

This comment was marked as outdated.

Copy link
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

Copy link
Member

@paperclover paperclover Mar 3, 2025

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.

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

Choose a reason for hiding this comment

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

.toThrow({ code: "ENOENT" })

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
3 participants