diff --git a/doc/api/fs.md b/doc/api/fs.md index c2d26a4a908c05..a6628e7bee061d 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -6822,6 +6822,9 @@ deprecated: - v20.12.0 - v18.20.0 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/55547 + description: The property is no longer read-only. - version: v23.0.0 pr-url: https://github.com/nodejs/node/pull/51050 description: Accessing this property emits a warning. It is now read-only. @@ -6831,7 +6834,7 @@ changes: * {string} -Alias for `dirent.parentPath`. Read-only. +Alias for `dirent.parentPath`. ### Class: `fs.FSWatcher` diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 2d85ed2196529d..5080bbf2580889 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -49,6 +49,7 @@ const { once, deprecate, isWindows, + setOwnProperty, } = require('internal/util'); const { toPathIfFileURL } = require('internal/url'); const { @@ -214,6 +215,9 @@ ObjectDefineProperty(Dirent.prototype, 'path', { get: deprecate(function() { return this.parentPath; }, 'dirent.path is deprecated in favor of dirent.parentPath', 'DEP0178'), + set(value) { + setOwnProperty(this, 'path', value); + }, configurable: true, enumerable: false, }); diff --git a/test/parallel/test-fs-utils-get-dirents.js b/test/parallel/test-fs-utils-get-dirents.js index dfc851090b269d..97dca15f38343d 100644 --- a/test/parallel/test-fs-utils-get-dirents.js +++ b/test/parallel/test-fs-utils-get-dirents.js @@ -78,6 +78,18 @@ const filename = 'foo'; }, )); } +{ + // Reassigning `.path` property should not trigger a warning + const dirent = getDirent( + tmpdir.path, + filename, + UV_DIRENT_UNKNOWN, + ); + assert.strictEqual(dirent.name, filename); + dirent.path = 'some other value'; + assert.strictEqual(dirent.parentPath, tmpdir.path); + assert.strictEqual(dirent.path, 'some other value'); +} { // string + Buffer const filenameBuffer = Buffer.from(filename);