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

The parentPath returned by fsPromises.glob is incorrect in some cases. #55060

Open
ayame113 opened this issue Sep 22, 2024 · 3 comments · May be fixed by #55071
Open

The parentPath returned by fsPromises.glob is incorrect in some cases. #55060

ayame113 opened this issue Sep 22, 2024 · 3 comments · May be fixed by #55071
Labels
confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system.

Comments

@ayame113
Copy link

ayame113 commented Sep 22, 2024

Version

v22.8.0

Platform

Darwin xxxMBP.lan 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

fsPromises.glob has inconsistent parentPath behavior when passed withFileTypes option.

According to the documentation, dirent.parentPath should return path to the parent directory, but in some cases it doesn't.

How often does it reproduce? Is there a required condition?

This occurs when a path is passed that does not contain a wildcard(*) in the filename portion.
For example, passing ./path/*/file.txt will result in different behavior than passing ./path/to/*.txt.

import { glob } from "fs/promises";
import path from "path";

// If the file name is a wildcard: OK
for await (const dirent of glob("./path/to/*.txt", { withFileTypes: true })) {
  console.log(dirent.parentPath);
  //=> /Users/me/work/test/path/to

  console.log(path.join(dirent.parentPath, dirent.name));
  //=> /Users/me/work/test/path/to/file.txt ✅
}

// If the file name is not a wildcard: 💣
for await (const dirent of glob("./path/*/file.txt", { withFileTypes: true })) {
  console.log(dirent.parentPath);
  //=> /Users/me/work/test/path/to/file.txt

  console.log(path.join(dirent.parentPath, dirent.name));
  //=> /Users/me/work/test/path/to/file.txt/file.txt 💣
}

What is the expected behavior? Why is that the expected behavior?

parentPath contains the path to a file, not the path to a directory.

What do you see instead?

The path to the file, not the path to the directory

Additional information

As far as I can see, something similar is happening with fs.globSync.

@RedYetiDev RedYetiDev added the fs Issues and PRs related to the fs subsystem / file system. label Sep 22, 2024
@RedYetiDev
Copy link
Member

@nodejs/fs

@RedYetiDev
Copy link
Member

Similar issue: #51955

@RedYetiDev
Copy link
Member

RedYetiDev commented Sep 22, 2024

I'm looking into this. A notable difference is that the incorrect dirent is created from DirentFromStats, while the correct dirent is created from Dirent.

See #55071

@RedYetiDev RedYetiDev added the confirmed-bug Issues with confirmed bugs. label Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants