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

[v18.x backport] fs: fix the file name is not included when the withFileTypes option of readdir #53969

Closed

Conversation

sonsurim
Copy link
Contributor

@sonsurim sonsurim commented Jul 20, 2024

This fixes the issue reported in #52441

The issue was caused by a modification in PR #51021 , which was working on including the parentPath in the documentation and modifying the path to display the filepath.

I have retained the content related to the documentation and removed the filepath part. Corresponding test code has also been updated accordingly.

An error occurred in the test/sequential/test-fs-opendir-recursive.js file after changing the code. This was resolved by referring to the code resolved in #49603 .

cc. @H4ad @daeyeon

Screenshots

AS-IS
You can see that even the file name is exposed in the path.

{
  const dir = await readdir(tmpDir, { withFileTypes: true });
  console.log(dir);
}
image

TO-BE
Now the file name has been removed from the path!

{
  const dir = await readdir(tmpDir, { withFileTypes: true });
  console.log(dir);
}
image

References

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. v18.x Issues that can be reproduced on v18.x or PRs targeting the v18.x-staging branch. labels Jul 20, 2024
@sonsurim sonsurim force-pushed the fix-readdir-withFileType branch 2 times, most recently from 3ef999e to 7e1b5de Compare July 21, 2024 02:49
@daeyeon
Copy link
Member

daeyeon commented Jul 21, 2024

Congrats on your first contribution! 🎉
Please update the commit message per the guide to fix the CI issue. Also, remove me as a co-author since I didn't directly contribute and don't qualify. :-)

@sonsurim sonsurim force-pushed the fix-readdir-withFileType branch 2 times, most recently from 9d97bac to d740bdb Compare July 21, 2024 07:23
@sonsurim
Copy link
Contributor Author

The git config was not set up properly, so I committed it again. 😅

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

This one is simply very hard to fix without introducing more breaking changes. It might be more wise to consider dirent.path irremediably broken and switch to using dirent.parentPath which is stable.

@@ -128,7 +128,7 @@ for (let i = 0; i < expected.length; i++) {
}

function getDirentPath(dirent) {
return pathModule.relative(testDir, dirent.path);
return pathModule.relative(testDir, pathModule.join(dirent.path, dirent.name));
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is not OK, we cannot make this kind of breaking change, especially on a maintenance LTS line. This needs to remain as it was on v18.19.1:

return pathModule.relative(testDir, dirent.path);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got it! I fixed in b2d61d8.
Many Thanks!

@sonsurim
Copy link
Contributor Author

@aduh95 Thanks for the review!

It might be more wise to consider dirent.path irremediably broken and switch to using dirent.parentPath which is stable.

Does the above mean modifying the readSyncRecursive function to use parentPath instead of path?

// dir.js
readSyncRecursive(dirent) {
    const ctx = { path: dirent.parentPath };
    const handle = dirBinding.opendir(
      pathModule.toNamespacedPath(dirent.parentPath),
      this[kDirOptions].encoding,
      undefined,
      ctx,
    );
    handleErrorFromBinding(ctx);
    const result = handle.read(
      this[kDirOptions].encoding,
      this[kDirOptions].bufferSize,
      undefined,
      ctx,
    );

    if (result) {
      this.processReadResult(dirent.parentPath, result);
    }

    handle.close(undefined, ctx);
    handleErrorFromBinding(ctx);
  }

Could you please help me a little more?

sonsurim added a commit to sonsurim/node that referenced this pull request Jul 21, 2024
The getDirentPath function in test/sequential/test-fs-opendir-recursive.js has been reverted for stability.

Refs: nodejs#53969 (comment)
@aduh95
Copy link
Contributor

aduh95 commented Jul 21, 2024

Does the above mean modifying the readSyncRecursive function to use parentPath instead of path?

No I meant for user land, Im unsure fixing it in Node.js is worth it

sonny and others added 3 commits July 21, 2024 21:17
The issue was caused by a modification in PR nodejs#51021,
which included changes to the documentation for parentPath
and modifications to display the file path.
I have retained the content related to the documentation
and removed the filepath part.

Fixes: nodejs#52441
Co-authored-by: injae-kim <injae-kim@users.noreply.github.com>
An error occurred in the test/sequential/test-fs-opendir-recursive.js file
after changing the code. This was resolved by referring to the code resolved
in PR nodejs#49603.

Refs: nodejs#49603
The getDirentPath function in test/sequential/test-fs-opendir-recursive.js has been reverted for stability.

Refs: nodejs#53969 (comment)
@H4ad
Copy link
Member

H4ad commented Jul 21, 2024

This one is simply very hard to fix without introducing more breaking changes. It might be more wise to consider dirent.path irremediably broken and switch to using dirent.parentPath which is stable.

Doesn't make sense this statement since on the sync version we have the correct behavior, unless you want to change the sync version.

@aduh95
Copy link
Contributor

aduh95 commented Jul 21, 2024

unless you want to change the sync version.

No that would be equally unwise IMO. What I'm saying is that dirent.path is inconsistent, and I don't see a way to fix the inconsistency without risking breaking more stuff. dirent.parentPath was introduced because dirent.path is inconsistent and unfortunately the ecosystem was/is relying on some of these inconsistencies.
If there's a way to bring back the behavior of v18.19.1 without breaking dirent.parentPath, I think we should consider doing so; I couldn't figure a way for doing so, I'm not saying it's impossible, but given the fact that v18.19.1 behavior is also inconsistent, I'm not sure it's worth doing.

@sonsurim
Copy link
Contributor Author

I will wait for the direction of the work to be decided. If you decide, please let me know!

@sonsurim sonsurim closed this Aug 19, 2024
@sonsurim sonsurim deleted the fix-readdir-withFileType branch August 19, 2024 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. v18.x Issues that can be reproduced on v18.x or PRs targeting the v18.x-staging branch.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants