Skip to content

fs#readdir(Sync) breaks when recursive + withFileTypes combined #48858

Description

@MattIPv4

Version

18.17.0

Platform

Darwin [snip] 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

touch a b c d e f

ls
a b c d e f

node
Welcome to Node.js v18.17.0.
Type ".help" for more information.
> const { readdir, readdirSync } = require('fs');
undefined
> readdir('./', console.log);
undefined
> null [ 'a', 'b', 'c', 'd', 'e', 'f' ]

> readdir('./', { withFileTypes: true }, console.log);
undefined
> null [
  Dirent { name: 'a', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'b', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'c', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'd', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'e', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'f', path: './', [Symbol(type)]: 1 }
]

> readdir('./', { recursive: true }, console.log);
null [ 'a', 'b', 'c', 'd', 'e', 'f' ]
undefined
> 
> readdir('./', { recursive: true, withFileTypes: true }, console.log);
null [
  Dirent { name: 'a', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'b', path: './', [Symbol(type)]: 1 }
]
undefined
> 
> readdirSync('./', { recursive: true });
[ 'a', 'b', 'c', 'd', 'e', 'f' ]
> readdirSync('./', { withFileTypes: true });
[
  Dirent { name: 'a', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'b', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'c', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'd', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'e', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'f', path: './', [Symbol(type)]: 1 }
]
> readdirSync('./', { recursive: true, withFileTypes: true });
[
  Dirent { name: 'a', path: './', [Symbol(type)]: 1 },
  Dirent { name: 'b', path: './', [Symbol(type)]: 1 }
]
>

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

Seemingly always

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

Should return all the files

What do you see instead?

See the above reproduction. Even with no nested subdirectories, readdir / readdirSync fail to report all files in the current directory when the recursive option is supplied at the same time as the withFileTypes option.

Additional information

It looks like #48640 discusses this in the comments, and #48698 fixes this, but I did not see an actual issue tracking this broken behavior itself, and that it was shipped in 18.17.0

Activity

  1. agarzola commented on Jul 25, 2023

    @agarzola

    I just ran into this on Node v20.5.0.

  2. chris-sophos commented on Jul 26, 2023

    @chris-sophos

    This is also an issue with the callback fs.readdir as noted in the original issue

  3. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jul 26, 2023
  4. changed the title [-]fs#readdirSync breaks when recursive + withFileTypes combined[/-] [+]fs#readdir(Sync) breaks when recursive + withFileTypes combined[/+] on Jul 26, 2023
  5. linked a pull request that will close this issuefs: fix readdir recursive sync & callback #48698on Aug 10, 2023
  6. agarzola commented on Aug 14, 2023

    @agarzola

    This is great news! Where can I find information about when we can expect v20.5.2 to be published (assuming this fix will be in that release)?

  7. Jieiku commented on Aug 28, 2023

    @Jieiku

    workaround:

    const fs = require('fs');
    
    const path = './public/'
    fs.readdirSync(path, { recursive: true, withFileTypes: false })
    .forEach(
      (file) => {
        // check if file is directory, if not then log the path/file
        if (!fs.lstatSync(path+file).isDirectory()) {
          console.log(file);
        };
      },
    );
  8. coffeebe4code commented on Apr 18, 2025

    @coffeebe4code

    I still have this issue with 22.14.0. Is there something I'm doing wrong, or is there a specific version I can expect it to work?

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.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions