diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index bdb4abfdf39aae..4567f7ff36a01d 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -943,6 +943,10 @@ async function readdirRecursive(originalPath, options) { async function readdir(path, options) { options = getOptions(options); + + // Make shallow copy to prevent mutating options from affecting results + options = copyObject(options); + path = getValidatedPath(path); if (options.recursive) { return readdirRecursive(path, options); diff --git a/test/parallel/test-fs-readdir-types.js b/test/parallel/test-fs-readdir-types.js index 3cc6b1cceff7fc..848d62399f0494 100644 --- a/test/parallel/test-fs-readdir-types.js +++ b/test/parallel/test-fs-readdir-types.js @@ -78,6 +78,14 @@ fs.readdir(readdirDir, { assertDirents(dirents); })().then(common.mustCall()); +// Check that mutating options doesn't affect results +(async () => { + const options = { withFileTypes: true }; + const direntsPromise = fs.promises.readdir(readdirDir, options); + options.withFileTypes = false; + assertDirents(await direntsPromise); +})().then(common.mustCall()); + // Check for correct types when the binding returns unknowns const UNKNOWN = constants.UV_DIRENT_UNKNOWN; const oldReaddir = binding.readdir;