Skip to content

Enhance path.matchesGlob to support exclusion patterns and options (parity with fs.glob) #59015

@mizulu

Description

@mizulu

What is the problem this feature will solve?


Currently, the fs.glob function in Node.js allows specifying advanced options such as exclude patterns, which not only exclude the specified path but also all its descendants from matching results. In contrast, path.matchesGlob only matches a glob pattern to an input string without supporting exclude patterns or additional options.

There is no way to replicate the full matching logic of fs.glob (including exclude semantics) using path.matchesGlob alone on a list of paths, such as when working with a virtual filesystem.

Example:

  • fs.glob({ include: '**/*', exclude: ['folder_a/folder_b'] }) will exclude folder_a/folder_b and all its contents.
  • However, using only path.matchesGlob, one cannot easily achieve the same exclusion behavior because the exclude pattern only matches the exact path, not its descendants.

What is the feature you are proposing to solve the problem?


  • Allow path.matchesGlob to accept an options argument (e.g., { exclude: [...] }, and/or other options supported by fs.glob).
  • Ensure that exclusion patterns work the same way as in fs.glob, i.e., they exclude the matched path and all its descendants.
  • This will enable users to apply consistent glob-matching logic both for real file systems and for lists of paths (e.g., in virtual filesystems or in-memory path lists).

Example API:

const include = '**/*';
const exclude = ['folder_a/folder_b'];
const path = 'folder_a/folder_b/file.js';
path.matchesGlob(path, include, { exclude }); // returns false

What alternatives have you considered?


  • Manually replicating exclusion logic by writing custom code to check if a path is a descendant of any excluded folder. This approach is error-prone and leads to code duplication.

The proposed enhancement would provide parity and consistency between fs.glob and path.matchesGlob, improving developer experience and flexibility.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions