diff --git a/lib/fs.js b/lib/fs.js index 9d941dd65ab613..af72ac36144c55 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1531,6 +1531,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;