Skip to content

Commit d636303

Browse files
author
Ethan Arrowood
committed
fs: fix readdir recursive sync & callback
Refs: #48640
1 parent 38dee8a commit d636303

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/fs.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,14 +1437,21 @@ function readdirSyncRecursive(basePath, options) {
14371437
);
14381438
handleErrorFromBinding(ctx);
14391439

1440-
for (let i = 0; i < readdirResult.length; i++) {
1441-
if (withFileTypes) {
1440+
if (withFileTypes) {
1441+
// Calling `readdir` with `withFileTypes=true`, the result is an array of arrays.
1442+
// The first array is the names, and the second array is the types.
1443+
// They are guaranteed to be the same length; hence, setting `length` to the length
1444+
// of the first array within the result.
1445+
const length = readdirResult[0].length;
1446+
for (let i = 0; i < length; i++) {
14421447
const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);
14431448
ArrayPrototypePush(readdirResults, dirent);
14441449
if (dirent.isDirectory()) {
14451450
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name));
14461451
}
1447-
} else {
1452+
}
1453+
} else {
1454+
for (let i = 0; i < readdirResult.length; i++) {
14481455
const resultPath = pathModule.join(path, readdirResult[i]);
14491456
const relativeResultPath = pathModule.relative(basePath, resultPath);
14501457
const stat = binding.internalModuleStat(resultPath);

test/sequential/test-fs-readdir-recursive.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ function getDirentPath(dirent) {
131131
}
132132

133133
function assertDirents(dirents) {
134+
assert.strictEqual(dirents.length, expected.length);
134135
dirents.sort((a, b) => (getDirentPath(a) < getDirentPath(b) ? -1 : 1));
135136
for (const [i, dirent] of dirents.entries()) {
136137
assert(dirent instanceof fs.Dirent);
138+
assert.notStrictEqual(dirent.name, undefined);
137139
assert.strictEqual(getDirentPath(dirent), expected[i]);
138140
}
139141
}

0 commit comments

Comments
 (0)