diff --git a/lib/fs.js b/lib/fs.js index 6a7d2cde463597..0fd917ba96acd6 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1401,9 +1401,12 @@ function readdirSyncRecursive(basePath, options) { // of the first array within the result. const length = readdirResult[0].length; for (let i = 0; i < length; i++) { + // Avoid excluding symlinks, as they are not directories. + // Refs: https://github.com/nodejs/node/issues/52663 + const stat = binding.internalModuleStat(binding, pathModule.join(path, readdirResult[0][i])); const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]); ArrayPrototypePush(readdirResults, dirent); - if (dirent.isDirectory()) { + if (dirent.isDirectory() || stat === 1) { ArrayPrototypePush(pathsQueue, pathModule.join(dirent.parentPath, dirent.name)); } } diff --git a/test/parallel/test-fs-readdir-types-symlinks.js b/test/parallel/test-fs-readdir-types-symlinks.js new file mode 100644 index 00000000000000..0ee27c7b60330a --- /dev/null +++ b/test/parallel/test-fs-readdir-types-symlinks.js @@ -0,0 +1,31 @@ +'use strict'; + +// Refs: https://github.com/nodejs/node/issues/52663 +const common = require('../common'); +const assert = require('node:assert'); +const fs = require('node:fs'); +const path = require('node:path'); + +if (!common.canCreateSymLink()) + common.skip('insufficient privileges'); + +const tmpdir = require('../common/tmpdir'); +const readdirDir = tmpdir.path; +// clean up the tmpdir +tmpdir.refresh(); + +// a/file +const a = path.join(readdirDir, 'a'); +fs.mkdirSync(a); +fs.writeFileSync(path.join(a, 'file'), 'irrelevant'); + +// a/b +// b -> a +const b = path.join(readdirDir, 'a', 'b'); +fs.symlinkSync(a, b, 'dir'); + +// Just check that the number of entries are the same +assert.strictEqual( + fs.readdirSync(a, { withFileTypes: true }).length, + fs.readdirSync(a, { withFileTypes: false }).length +);