From 54896ab011b6cb631a9d1f0154e9aa060f55be13 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 7 Jan 2023 16:36:51 +0100 Subject: [PATCH] module: fix unintended mutation Refs: https://github.com/nodejs/node/pull/46061#issuecomment-1372246648 PR-URL: https://github.com/nodejs/node/pull/46108 Reviewed-By: Rafael Gonzaga Reviewed-By: James M Snell Reviewed-By: Geoffrey Booth Reviewed-By: Luigi Pinca Reviewed-By: Jacob Smith --- lib/internal/modules/cjs/loader.js | 10 +++++----- test/fixtures/require-resolve.js | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 46e58400fb34b5..80b931ef70825c 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -29,6 +29,7 @@ const { ArrayPrototypeJoin, ArrayPrototypeMap, ArrayPrototypePush, + ArrayPrototypePushApply, ArrayPrototypeSlice, ArrayPrototypeSplice, ArrayPrototypeUnshift, @@ -667,12 +668,11 @@ Module._findPath = function(request, paths, isMain) { return filename; } - if (exts === undefined) { - exts = ['']; - } else { - ArrayPrototypeUnshift(exts, ''); + const extensions = ['']; + if (exts !== undefined) { + ArrayPrototypePushApply(extensions, exts); } - reportModuleNotFoundToWatchMode(basePath, exts); + reportModuleNotFoundToWatchMode(basePath, extensions); } return false; diff --git a/test/fixtures/require-resolve.js b/test/fixtures/require-resolve.js index 86bc6415c8eb55..9a7fc3385143aa 100644 --- a/test/fixtures/require-resolve.js +++ b/test/fixtures/require-resolve.js @@ -98,3 +98,8 @@ assert.strictEqual( require.resolve('./printA.js', {}), require.resolve('./printA.js') ); + +assert.strictEqual( + require.resolve('no_index/'), + path.join(__dirname, 'node_modules', 'no_index', 'lib', 'index.js'), +)