From 0e97fa4d5f056e12d3c0704bfb7bcdc56316ef94 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 24 Apr 2023 17:08:11 -0400 Subject: fix(npm): only include top level packages in top level node_modules directory (#18824) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We were indeterministically including packages in the top level `node_modules/` folder when using a local node_modules directory. This change aligns with pnpm and only includes top level packages in this folder. This should be faster for initializing the folder, but may expose issues in packages that reference other packages not defined in their dependencies. That said, the behaviour previously was previously broken. This has exposed a bug in the require implementation where it doesn't find a package (which is the main underlying issue here). There is a failing test already for this in the test suite after this change. Closes #18822 --------- Co-authored-by: Bartek IwaƄczuk --- ext/node/polyfills/01_require.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'ext') diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index 42ead05e3..8fbe5078c 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -557,15 +557,21 @@ Module._findPath = function (request, paths, isMain, parentPath) { } } - const isDenoDirPackage = ops.op_require_is_deno_dir_package( - curPath, - ); - const isRelative = ops.op_require_is_request_relative( - request, - ); - const basePath = (isDenoDirPackage && !isRelative) - ? pathResolve(curPath, packageSpecifierSubPath(request)) - : pathResolve(curPath, request); + let basePath; + + if (usesLocalNodeModulesDir) { + basePath = pathResolve(curPath, request); + } else { + const isDenoDirPackage = ops.op_require_is_deno_dir_package( + curPath, + ); + const isRelative = ops.op_require_is_request_relative( + request, + ); + basePath = (isDenoDirPackage && !isRelative) + ? pathResolve(curPath, packageSpecifierSubPath(request)) + : pathResolve(curPath, request); + } let filename; const rc = stat(basePath); @@ -615,7 +621,9 @@ Module._resolveLookupPaths = function (request, parent) { return paths; } - if (parent?.filename && parent.filename.length > 0) { + if ( + !usesLocalNodeModulesDir && parent?.filename && parent.filename.length > 0 + ) { const denoDirPath = ops.op_require_resolve_deno_dir( request, parent.filename, -- cgit v1.2.3