Skip to content

fs.readdirSync(recursive=true) should not follow follow symlinks by default #51858

Description

@myocytebd

Version

v21.6.2

Platform

Linux 6.5.0-15-generic #15~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC x86_64 x86_64 x86_64 GNU/Linux

Subsystem

fs

What steps will reproduce the bug?

  • Ensure a directory tree with symlink, e.g.
./
../
mylink -> ../other_dir/
  • fs.readdirSync('.', { recursive: true })

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

[ './mylink' ]

What do you see instead?

All files under ../other_dir/ are listed.

Additional information

This behavior is extremely surprising and error-prone, since common commands like find or Get-ChildItem does not follow symlink by default.
In addition, it is not documented.

Activity

  1. aduh95 commented on Feb 23, 2024

    @aduh95
    Contributor

    Would you like to send a PR adding an option to turn off/on that behavior?

  2. kylo5aby commented on Apr 16, 2024

    @kylo5aby
    Contributor

    for the case below

    dir
    - dir1 -->dir-symlink
    - file

    fs.readdirSync('./dir', {{ recursive: true, withFileTypes: true }}) and fs.readdirSync('./dir', {{ recursive: true, withFileTypes: false}} have different output.:

    fs.readdirSync('dir', {recursive: true, withFileTypes: true});
    //Output:
    // [
    //     Dirent { name: 'dir1', path: 'dir', [Symbol(type)]: 2 },
    //     Dirent { name: 'dir1-symlink', path: 'dir', [Symbol(type)]: 3 },
    //     Dirent { name: '1.txt', path: 'dir/dir1', [Symbol(type)]: 1 }
    // ]
    fs.readdirSync('dir', {recursive: true, withFileTypes: false});
    //Output:
    //[ 'dir1', 'dir1-symlink', 'dir1/1.txt', 'dir1-symlink/1.txt' ]

    I found that it due to

    node/lib/fs.js

    Line 1416 in 9ef03f1

    if (dirent.isDirectory()) {

    node/lib/fs.js

    Line 1424 in 9ef03f1

    const stat = binding.internalModuleStat(resultPath);

    have different behavior.

  3. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Apr 20, 2024
  4. Alfagun74 commented on Jan 12, 2025

    @Alfagun74

    I think it totally should follow symlinks.

  5. github-actions commented on May 23, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  6. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 23, 2026
  7. github-actions commented on Jun 22, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions