From 7270f845964ed86e8bd70fa24dbc6616b3333b3a Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 28 Oct 2024 09:33:39 +0100 Subject: [PATCH] fs: make `dirent.path` writable PR-URL: https://github.com/nodejs/node/pull/55547 Refs: https://github.com/nodejs/node/issues/55538 Reviewed-By: Matteo Collina Reviewed-By: LiviaMedeiros Reviewed-By: Yagiz Nizipli Reviewed-By: Rafael Gonzaga Reviewed-By: Chemi Atlow --- doc/api/fs.md | 5 ++++- lib/internal/fs/utils.js | 4 ++++ test/parallel/test-fs-utils-get-dirents.js | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) 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);