Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
myocytebd opened this issue Feb 23, 2024 · 2 comments
Open
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@myocytebd
Copy link

myocytebd commented Feb 23, 2024

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.

@aduh95
Copy link
Contributor

aduh95 commented Feb 23, 2024

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

@kylo5aby
Copy link
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.

@avivkeller avivkeller added the fs Issues and PRs related to the fs subsystem / file system. label Apr 20, 2024
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.
Projects
None yet
Development

No branches or pull requests

4 participants