Skip to content

Commit

Permalink
fs: fix missing file name with withFileTypes option of readdir
Browse files Browse the repository at this point in the history
The issue was caused by a modification in PR nodejs#51021, which included changes
to the documentation for parentPath and modifications to display the filepath.
I have retained the content related to the documentation and removed the
filepath part.

Fixes: nodejs#52441
Co-authored-by: injae-kim <injae-kim@users.noreply.github.com>
  • Loading branch information
2 people authored and sonsurim committed Jul 21, 2024
1 parent 0092358 commit 88dbf16
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
1 change: 0 additions & 1 deletion lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ class Dir {
path,
result[i],
result[i + 1],
true, // Quirk to not introduce a breaking change.
),
);
}
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

0 comments on commit 88dbf16

Please sign in to comment.