Skip to content

fs: add maxDepth option to glob#64003

Open
TheAlexLichter wants to merge 1 commit into
nodejs:mainfrom
TheAlexLichter:fs-glob-max-depth
Open

fs: add maxDepth option to glob#64003
TheAlexLichter wants to merge 1 commit into
nodejs:mainfrom
TheAlexLichter:fs-glob-max-depth

Conversation

@TheAlexLichter

Copy link
Copy Markdown

This PR adds a maxDepth option to fs.glob(), fs.globSync(), and fs.promises.glob().

Motivation

Broad glob patterns such as ** currently traverse the complete matching directory tree. Results can be filtered further afterwards but that does not avoid the associated filesystem work for use cases that only need "nearby entries".
This is particularly costly for large directory trees or mounted filesystems.

Similar functionality is already available in existing 3rd party packages like tinyglobby in various ways.

Implementation

Depth is measured relative to cwd:

  • cwd has depth 0
  • Direct children have depth 1
  • Grandchildren have depth 2
  • And so on

The default is Infinity (compatible with existing default behavior). A finite limit applies to both returned matches and filesystem traversal, allowing subtrees to be skipped.

Relative patterns cannot bypass the limit using .., and paths outside cwd, including paths on another Windows volume, are outside any finite depth.

Absolute patterns can traverse ancestors as needed to reach cwd, but matches outside cwd are excluded when the limit is finite.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@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. labels Jun 19, 2026
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants