From 0c36c6e7b06998c330cda7ff644c41e68247b2c0 Mon Sep 17 00:00:00 2001 From: sonny Date: Sun, 21 Jul 2024 00:41:15 +0900 Subject: [PATCH] fs: fix missing file name with withFileTypes option of readdir The issue was caused by a modification in PR #51021, which included changes to the documentation for parentPath and modifications to display the file path. I have retained the content related to the documentation and removed the filepath part. Fixes: https://github.com/nodejs/node/issues/52441 Co-authored-by: injae-kim --- lib/internal/fs/dir.js | 1 - lib/internal/fs/utils.js | 20 ++++++++------------ test/parallel/test-fs-opendir.js | 2 +- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/internal/fs/dir.js b/lib/internal/fs/dir.js index a06106affe7d52..0a0431c6b1540f 100644 --- a/lib/internal/fs/dir.js +++ b/lib/internal/fs/dir.js @@ -155,7 +155,6 @@ class Dir { path, result[i], result[i + 1], - true, // Quirk to not introduce a breaking change. ), ); } diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 2e9054a675426b..4ebd1d80bff969 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -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; } @@ -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; } } @@ -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); } @@ -305,7 +305,7 @@ 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)); @@ -313,13 +313,9 @@ function getDirent(path, name, type, callback) { } 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); } } diff --git a/test/parallel/test-fs-opendir.js b/test/parallel/test-fs-opendir.js index 4956b219ee5d43..8b8fc161eee661 100644 --- a/test/parallel/test-fs-opendir.js +++ b/test/parallel/test-fs-opendir.js @@ -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