Skip to content

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

@ayame113

Description

@ayame113

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions