Skip to content

Conversation

@MatricalDefunkt
Copy link

This pull request adds support for following symbolic links (symlinks) when performing glob operations in the filesystem module. It introduces a new followSymlinks option to relevant APIs, updates the documentation, and implements logic to safely traverse symlinks while preventing infinite loops due to cycles.

The most important changes are:

API and Documentation Updates:

  • Added a followSymlinks boolean option to the glob-related methods in the filesystem API, allowing users to specify whether symlinks to directories should be traversed. Documentation in doc/api/fs.md has been updated accordingly.

Core Implementation in lib/internal/fs/glob.js:

  • Modified the Glob class and related functions to accept and handle the followSymlinks option, including passing it to stat/lstat calls and ensuring symlinks are followed when requested.
  • Implemented logic to resolve symlinks and guard against cycles by tracking visited real paths, thus preventing infinite recursion when symlinks create directory loops.
  • Updated the glob matching logic to work with resolved entries, ensuring correct behavior when symlinks are followed, and adjusted all relevant code paths to use the resolved entry instead of the original symlink entry.

Internal Refactoring:

  • Enhanced the caching mechanism to store and check visited real paths, and to cache resolved stat results for symlink targets.

Fixes: #61033

@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 Dec 23, 2025
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.

Allow fs.glob to traverse symbolic links to directories

2 participants