-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Node compat: Add path
member to fs.Dirent
#7649
Conversation
three locations in node_fs.zig are apparently where code changes are needed. Should also check to see if there are tests to add.
This is not the right place for this
I believe `path` is always the passed-in relative or absolute path in this case
To test all three of Bun's withFileTypes implementations
test/dirent.js
Outdated
// This script tests the contents of fs.Dirent for all of the methods that return it. | ||
// Compare running with node vs. running with Bun for compatibility. | ||
|
||
const fs = require('fs'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add automated tests
This is not an automated test. It won't run in CI.
Have a look at files with .test.js
or .test.ts
for an exmaple
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I now see the readdir
tests in test/js/node/fs/fs.test.ts
-- is it preferred to add these tests to that same file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that sounds like a good place for the test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some passing tests to test/js/node/fs/fs.test.ts. Ideally I would also test relative paths (i.e. readdir('../..')
) for behavior consistent with node, but I'm not sure if it's worthwhile to do so, or what a good approach would be for setting the test's working directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall looks fine
however path
is deprecated in node and they intend to replace it with parentPath
nodejs/node#50976
nodejs/node#51020
we should add both properties, where one is an alias for the other.
@paperdave Ah, didn't know about that Node.js deprecation. I made a change so that both |
/** | ||
* The base path that this `fs.Dirent` object refers to. | ||
* Deprecated in favor of `parentPath`. | ||
* @since v?????? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since ????
should be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this entire file gets deleted to resolve the merge conflict -- if I'm understanding things correctly, type definitions are now in https://github.com/DefinitelyTyped/DefinitelyTyped per #7670.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can update the comment in the code that needs to be changed. looks to go me otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should've been approved
We should use a rope string for the path
getter. The path
getter will rarely be called. As-is, this will be a significant memory usage increase to readdir
, which matters when calling it on a large directory tree
To create a rope string, we can use jsRopeString
in C++. We would need to add a binding to BunString.cpp
that creates a rope string from two different BunString
pointers and increment the reference count for the path
string.
Closing because a different implementation was merged: #11135 |
What does this PR do?
This changes the output of
fs.readdir({ withFileTypes: true })
(and all of its variants) to be consistent with node, such that theDirent.name
property is the path basename and theDirent.path
property is the path dirname. Fixes #7358. This is a "breaking" change since theDirent.name
property currently contains the full relative path for{ recursive: true }
.How did you verify your code works?
I wrote a script at
test/dirent.js
to manually compare the behavior of node and bun for a mock set of folders, subfolders, and files addressed by different paths. I'm new to this repo and to Zig so I know this isn't idiomatic, but it's the best I could come up with so far.bun-debug test test-file-name.test
)I'm coming in here with very little systems programming experience, so I did my best to follow any patterns I could find that appeared to be relevant to my changes. For example, I added
item.path.deref()
anywhere I sawitem.name.deref()
.@since
parameter on the TS type needs to be populated if/when this is scheduled to a release.None of my changes seem to affect the behavior of
bun repl
.