diff --git a/lib/fs.js b/lib/fs.js index dd256566d268f3..714fce8e8b29be 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1530,6 +1530,9 @@ function readdir(path, options, callback) { } if (options.recursive) { + // Make shallow copy to prevent mutating options from affecting results + options = copyObject(options); + readdirRecursive(path, options, callback); return; } diff --git a/test/parallel/test-fs-readdir-types.js b/test/parallel/test-fs-readdir-types.js index 848d62399f0494..c6225c919e4a22 100644 --- a/test/parallel/test-fs-readdir-types.js +++ b/test/parallel/test-fs-readdir-types.js @@ -86,6 +86,14 @@ fs.readdir(readdirDir, { assertDirents(await direntsPromise); })().then(common.mustCall()); +{ + const options = { recursive: true, withFileTypes: true }; + fs.readdir(readdirDir, options, common.mustSucceed((dirents) => { + assertDirents(dirents); + })); + options.withFileTypes = false; +} + // Check for correct types when the binding returns unknowns const UNKNOWN = constants.UV_DIRENT_UNKNOWN; const oldReaddir = binding.readdir;