Skip to content

Commit 26e8f77

Browse files
aduh95richardlau
authored andcommitted
fs: introduce dirent.parentPath
The goal is to replace `dirent.path` using a name that's less likely to create confusion. `dirent.path` value has not been stable, moving it to a different property name should avoid breaking some upgrading user expectations. PR-URL: #50976 Backport-PR-URL: #51021 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent 96514a8 commit 26e8f77

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

doc/api/fs.md

+13
Original file line numberDiff line numberDiff line change
@@ -6437,6 +6437,19 @@ The file name that this {fs.Dirent} object refers to. The type of this
64376437
value is determined by the `options.encoding` passed to [`fs.readdir()`][] or
64386438
[`fs.readdirSync()`][].
64396439
6440+
#### `dirent.parentPath`
6441+
6442+
<!-- YAML
6443+
added:
6444+
- REPLACEME
6445+
-->
6446+
6447+
> Stability: 1 – Experimental
6448+
6449+
* {string}
6450+
6451+
The path to the parent directory of the file this {fs.Dirent} object refers to.
6452+
64406453
#### `dirent.path`
64416454
64426455
<!-- YAML

lib/internal/fs/dir.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ class Dir {
152152
ArrayPrototypePush(
153153
this[kDirBufferedEntries],
154154
getDirent(
155-
pathModule.join(path, result[i]),
155+
path,
156156
result[i],
157157
result[i + 1],
158+
true, // Quirk to not introduce a breaking change.
158159
),
159160
);
160161
}

lib/internal/fs/utils.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ function assertEncoding(encoding) {
161161
}
162162

163163
class Dirent {
164-
constructor(name, type, path) {
164+
constructor(name, type, path, filepath = path && join(path, name)) {
165165
this.name = name;
166-
this.path = path;
166+
this.parentPath = path;
167+
this.path = filepath;
167168
this[kType] = type;
168169
}
169170

@@ -197,8 +198,8 @@ class Dirent {
197198
}
198199

199200
class DirentFromStats extends Dirent {
200-
constructor(name, stats, path) {
201-
super(name, null, path);
201+
constructor(name, stats, path, filepath) {
202+
super(name, null, path, filepath);
202203
this[kStats] = stats;
203204
}
204205
}
@@ -233,7 +234,7 @@ function join(path, name) {
233234
}
234235

235236
if (typeof path === 'string' && typeof name === 'string') {
236-
return pathModule.basename(path) === name ? path : pathModule.join(path, name);
237+
return pathModule.join(path, name);
237238
}
238239

239240
if (isUint8Array(path) && isUint8Array(name)) {
@@ -268,7 +269,7 @@ function getDirents(path, { 0: names, 1: types }, callback) {
268269
callback(err);
269270
return;
270271
}
271-
names[idx] = new DirentFromStats(name, stats, path);
272+
names[idx] = new DirentFromStats(name, stats, path, filepath);
272273
if (--toFinish === 0) {
273274
callback(null, names);
274275
}
@@ -304,17 +305,21 @@ function getDirent(path, name, type, callback) {
304305
callback(err);
305306
return;
306307
}
307-
callback(null, new DirentFromStats(name, stats, filepath));
308+
callback(null, new DirentFromStats(name, stats, path, filepath));
308309
});
309310
} else {
310311
callback(null, new Dirent(name, type, path));
311312
}
312313
} else if (type === UV_DIRENT_UNKNOWN) {
313314
const filepath = join(path, name);
314315
const stats = lazyLoadFs().lstatSync(filepath);
315-
return new DirentFromStats(name, stats, path);
316-
} else {
316+
// callback === true: Quirk to not introduce a breaking change.
317+
return new DirentFromStats(name, stats, path, callback === true ? filepath : path);
318+
} else if (callback === true) {
319+
// callback === true: Quirk to not introduce a breaking change.
317320
return new Dirent(name, type, path);
321+
} else {
322+
return new Dirent(name, type, path, path);
318323
}
319324
}
320325

test/parallel/test-fs-opendir.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ const invalidCallbackObj = {
4848
const entries = files.map(() => {
4949
const dirent = dir.readSync();
5050
assertDirent(dirent);
51-
return dirent.name;
52-
});
53-
assert.deepStrictEqual(files, entries.sort());
51+
return { name: dirent.name, path: dirent.path, parentPath: dirent.parentPath, toString() { return dirent.name; } };
52+
}).sort();
53+
assert.deepStrictEqual(entries.map((d) => d.name), files);
54+
assert.deepStrictEqual(entries.map((d) => d.path), files.map((name) => path.join(testDir, name)));
55+
assert.deepStrictEqual(entries.map((d) => d.parentPath), Array(entries.length).fill(testDir));
5456

5557
// dir.read should return null when no more entries exist
5658
assert.strictEqual(dir.readSync(), null);

0 commit comments

Comments
 (0)