◐ Shell
clean mode source ↗

lib: avoid excluding symlinks in recursive fs.readdir with filetypes · nodejs/node@0571d55

Original file line numberDiff line numberDiff line change

@@ -1404,9 +1404,12 @@ function readdirSyncRecursive(basePath, options) {

14041404

// of the first array within the result.

14051405

const length = readdirResult[0].length;

14061406

for (let i = 0; i < length; i++) {

1407+

// Avoid excluding symlinks, as they are not directories.

1408+

// Refs: https://github.com/nodejs/node/issues/52663

1409+

const stat = binding.internalModuleStat(binding, pathModule.join(path, readdirResult[0][i]));

14071410

const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);

14081411

ArrayPrototypePush(readdirResults, dirent);

1409-

if (dirent.isDirectory()) {

1412+

if (dirent.isDirectory() || stat === 1) {

14101413

ArrayPrototypePush(pathsQueue, pathModule.join(dirent.parentPath, dirent.name));

14111414

}

14121415

}

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,36 @@

1+

'use strict';

2+
3+

// Refs: https://github.com/nodejs/node/issues/52663

4+

const common = require('../common');

5+

const assert = require('node:assert');

6+

const fs = require('node:fs');

7+

const path = require('node:path');

8+
9+

if (!common.canCreateSymLink())

10+

common.skip('insufficient privileges');

11+
12+

const tmpdir = require('../common/tmpdir');

13+

const readdirDir = tmpdir.path;

14+

// clean up the tmpdir

15+

tmpdir.refresh();

16+
17+

// a/1, a/2

18+

const a = path.join(readdirDir, 'a');

19+

fs.mkdirSync(a);

20+

fs.writeFileSync(path.join(a, '1'), 'irrelevant');

21+

fs.writeFileSync(path.join(a, '2'), 'irrelevant');

22+
23+

// b/1

24+

const b = path.join(readdirDir, 'b');

25+

fs.mkdirSync(b);

26+

fs.writeFileSync(path.join(b, '1'), 'irrelevant');

27+
28+

// b/c -> a

29+

const c = path.join(readdirDir, 'b', 'c');

30+

fs.symlinkSync(a, c, 'dir');

31+
32+

// Just check that the number of entries are the same

33+

assert.strictEqual(

34+

fs.readdirSync(b, { recursive: true, withFileTypes: true }).length,

35+

fs.readdirSync(b, { recursive: true, withFileTypes: false }).length

36+

);