Skip to content

Commit 62d36d9

Browse files
Ethan Arrowoodrluvaton
Ethan Arrowood
authored andcommitted
fs: fix readdir recursive sync & callback
Refs: nodejs#48640 PR-URL: nodejs#48698 Fixes: nodejs#48858 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 841faf3 commit 62d36d9

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/fs.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1443,14 +1443,21 @@ function readdirSyncRecursive(basePath, options) {
14431443
);
14441444
handleErrorFromBinding(ctx);
14451445

1446-
for (let i = 0; i < readdirResult.length; i++) {
1447-
if (withFileTypes) {
1446+
if (withFileTypes) {
1447+
// Calling `readdir` with `withFileTypes=true`, the result is an array of arrays.
1448+
// The first array is the names, and the second array is the types.
1449+
// They are guaranteed to be the same length; hence, setting `length` to the length
1450+
// of the first array within the result.
1451+
const length = readdirResult[0].length;
1452+
for (let i = 0; i < length; i++) {
14481453
const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);
14491454
ArrayPrototypePush(readdirResults, dirent);
14501455
if (dirent.isDirectory()) {
14511456
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name));
14521457
}
1453-
} else {
1458+
}
1459+
} else {
1460+
for (let i = 0; i < readdirResult.length; i++) {
14541461
const resultPath = pathModule.join(path, readdirResult[i]);
14551462
const relativeResultPath = pathModule.relative(basePath, resultPath);
14561463
const stat = binding.internalModuleStat(resultPath);

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

+2
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)