Skip to content

[v18.x backport] fs: fix the file name is not included when the withFileTypes option of readdir #53969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ class Dir {
path,
result[i],
result[i + 1],
true, // Quirk to not introduce a breaking change.
),
);
}
}

readSyncRecursive(dirent) {
const ctx = { path: dirent.path };
const path = pathModule.join(dirent.path, dirent.name);
const ctx = { path };
const handle = dirBinding.opendir(
pathModule.toNamespacedPath(dirent.path),
pathModule.toNamespacedPath(path),
this[kDirOptions].encoding,
undefined,
ctx,
Expand All @@ -178,7 +178,7 @@ class Dir {
);

if (result) {
this.processReadResult(dirent.path, result);
this.processReadResult(path, result);
}

handle.close(undefined, ctx);
Expand Down
20 changes: 8 additions & 12 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ function assertEncoding(encoding) {
}

class Dirent {
constructor(name, type, path, filepath = path && join(path, name)) {
constructor(name, type, path) {
this.name = name;
this.parentPath = path;
this.path = filepath;
this.path = path;
this[kType] = type;
}

Expand Down Expand Up @@ -198,8 +198,8 @@ class Dirent {
}

class DirentFromStats extends Dirent {
constructor(name, stats, path, filepath) {
super(name, null, path, filepath);
constructor(name, stats, path) {
super(name, null, path);
this[kStats] = stats;
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ function getDirents(path, { 0: names, 1: types }, callback) {
callback(err);
return;
}
names[idx] = new DirentFromStats(name, stats, path, filepath);
names[idx] = new DirentFromStats(name, stats, path);
if (--toFinish === 0) {
callback(null, names);
}
Expand Down Expand Up @@ -305,21 +305,17 @@ function getDirent(path, name, type, callback) {
callback(err);
return;
}
callback(null, new DirentFromStats(name, stats, path, filepath));
callback(null, new DirentFromStats(name, stats, path));
});
} else {
callback(null, new Dirent(name, type, path));
}
} else if (type === UV_DIRENT_UNKNOWN) {
const filepath = join(path, name);
const stats = lazyLoadFs().lstatSync(filepath);
// callback === true: Quirk to not introduce a breaking change.
return new DirentFromStats(name, stats, path, callback === true ? filepath : path);
} else if (callback === true) {
// callback === true: Quirk to not introduce a breaking change.
return new Dirent(name, type, path);
return new DirentFromStats(name, stats, path);
} else {
return new Dirent(name, type, path, path);
return new Dirent(name, type, path);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const invalidCallbackObj = {
return { name: dirent.name, path: dirent.path, parentPath: dirent.parentPath, toString() { return dirent.name; } };
}).sort();
assert.deepStrictEqual(entries.map((d) => d.name), files);
assert.deepStrictEqual(entries.map((d) => d.path), files.map((name) => path.join(testDir, name)));
assert.deepStrictEqual(entries.map((d) => d.path), Array(entries.length).fill(testDir));
assert.deepStrictEqual(entries.map((d) => d.parentPath), Array(entries.length).fill(testDir));

// dir.read should return null when no more entries exist
Expand Down
Loading