Repository navigation
fs.readdirSync(recursive=true) should not follow follow symlinks by default #51858
Description
Activity
Would you like to send a PR adding an option to turn off/on that behavior?
for the case below
dir - dir1 -->dir-symlink - filefs.readdirSync('./dir', {{ recursive: true, withFileTypes: true }})andfs.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
Line 1416 in 9ef03f1
if (dirent.isDirectory()) {
Line 1424 in 9ef03f1
const stat = binding.internalModuleStat(resultPath);
have different behavior.- addedfsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.
on Apr 20, 2024 I think it totally should follow symlinks.
github-actions commented
on May 23, 2026 on May 23, 2026 – with GitHub ActionsContributorMore actionsThis 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.- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on May 23, 2026 github-actions commented
on Jun 22, 2026 on Jun 22, 2026 – with GitHub ActionsContributorMore actionsThis 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.
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?
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
findorGet-ChildItemdoes not follow symlink by default.In addition, it is not documented.