Skip to content

Commit c6dca02

Browse files
committed
fs: fix readdir recursive sync & callback
Refs: nodejs/node#48640 PR-URL: nodejs/node#48698 Fixes: nodejs/node#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 bca04ed commit c6dca02

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

graal-nodejs/lib/fs.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1430,14 +1430,21 @@ function readdirSyncRecursive(basePath, options) {
14301430
);
14311431
handleErrorFromBinding(ctx);
14321432

1433-
for (let i = 0; i < readdirResult.length; i++) {
1434-
if (withFileTypes) {
1433+
if (withFileTypes) {
1434+
// Calling `readdir` with `withFileTypes=true`, the result is an array of arrays.
1435+
// The first array is the names, and the second array is the types.
1436+
// They are guaranteed to be the same length; hence, setting `length` to the length
1437+
// of the first array within the result.
1438+
const length = readdirResult[0].length;
1439+
for (let i = 0; i < length; i++) {
14351440
const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);
14361441
ArrayPrototypePush(readdirResults, dirent);
14371442
if (dirent.isDirectory()) {
14381443
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name));
14391444
}
1440-
} else {
1445+
}
1446+
} else {
1447+
for (let i = 0; i < readdirResult.length; i++) {
14411448
const resultPath = pathModule.join(path, readdirResult[i]);
14421449
const relativeResultPath = pathModule.relative(basePath, resultPath);
14431450
const stat = binding.internalModuleStat(resultPath);

graal-nodejs/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)