Skip to content

fs: add maxDepth option to glob - #64003

Open
TheAlexLichter wants to merge 2 commits into
nodejs:mainfrom
TheAlexLichter:fs-glob-max-depth
Open

fs: add maxDepth option to glob#64003
TheAlexLichter wants to merge 2 commits 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

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@TheAlexLichter

Copy link
Copy Markdown
Author

(Added the "Signed off" trailer)

Comment thread doc/api/fs.md
Signed-off-by: Alexander Lichter <github@lichter.io>
@trivikr trivikr added author ready PRs that have at least one approval, no outstanding review comments, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Aug 24, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 24, 2026
@nodejs-github-bot

This comment was marked as outdated.

@trivikr

trivikr commented Aug 24, 2026

Copy link
Copy Markdown
Member

@TheAlexLichter The lint errors need to be fixed

/home/runner/work/node/node/test/parallel/test-fs-glob.mjs
Error:   20:1  error  'node:child_process' import is duplicated  no-duplicate-imports

@trivikr trivikr removed the author ready PRs that have at least one approval, no outstanding review comments, and a CI started. label Aug 24, 2026
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.87855% with 74 lines in your changes missing coverage. Please review.
βœ… Project coverage is 90.14%. Comparing base (e6a8d06) to head (e9f17df).
⚠️ Report is 957 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/fs/glob.js 80.87% 72 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64003      +/-   ##
==========================================
- Coverage   92.01%   90.14%   -1.87%     
==========================================
  Files         379      751     +372     
  Lines      166972   253943   +86971     
  Branches    25554    47887   +22333     
==========================================
+ Hits       153639   228925   +75286     
- Misses      13041    16278    +3237     
- Partials      292     8740    +8448     
Files with missing lines Coverage Ξ”
lib/internal/fs/glob.js 89.27% <80.87%> (+1.14%) ⬆️

... and 605 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@TheAlexLichter

Copy link
Copy Markdown
Author

@trivikr just resolved them πŸ‘πŸ»

@trivikr trivikr added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no outstanding review comments, and a CI started. labels Aug 25, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 25, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no outstanding review comments, and a CI started. 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.

5 participants