Node compat: Add path member to fs.Dirent#7649
Node compat: Add path member to fs.Dirent#7649NReilingh wants to merge 16 commits intooven-sh:mainfrom
path member to fs.Dirent#7649Conversation
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
| // 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.
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.
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.
that sounds like a good place for the test
There was a problem hiding this comment.
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.
paperclover
left a comment
There was a problem hiding this comment.
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.
since ???? should be removed
There was a problem hiding this comment.
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.
paperclover
left a comment
There was a problem hiding this comment.
i can update the comment in the code that needs to be changed. looks to go me otherwise
Jarred-Sumner
left a comment
There was a problem hiding this comment.
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.nameproperty is the path basename and theDirent.pathproperty is the path dirname. Fixes #7358. This is a "breaking" change since theDirent.nameproperty currently contains the full relative path for{ recursive: true }.How did you verify your code works?
I wrote a script at
test/dirent.jsto 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().@sinceparameter 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.