1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

fix(npm): only include top level packages in top level node_modules directory (#18824)

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 <biwanczuk@gmail.com>
This commit is contained in:
David Sherret 2023-04-24 17:08:11 -04:00 committed by GitHub
parent be9e3c430f
commit 0e97fa4d5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 20 deletions

View file

@ -4,7 +4,6 @@
use std::borrow::Cow;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
@ -365,18 +364,16 @@ async fn sync_resolution_with_fs(
}
}
// 4. Create all the packages in the node_modules folder, which are symlinks.
// 4. Create all the top level packages in the node_modules folder, which are symlinks.
//
// Symlink node_modules/<package_name> to
// node_modules/.deno/<package_id>/node_modules/<package_name>
let mut found_names = HashSet::new();
let mut pending_packages = VecDeque::new();
pending_packages.extend(snapshot.top_level_packages().map(|id| (id, true)));
while let Some((id, is_top_level)) = pending_packages.pop_front() {
let mut ids = snapshot.top_level_packages().collect::<Vec<_>>();
ids.sort_by(|a, b| b.cmp(a)); // create determinism and only include the latest version
for id in ids {
let root_folder_name = if found_names.insert(id.nv.name.clone()) {
id.nv.name.clone()
} else if is_top_level {
id.nv.to_string()
} else {
continue; // skip, already handled
};
@ -394,9 +391,6 @@ async fn sync_resolution_with_fs(
&local_registry_package_path,
&join_package_name(root_node_modules_dir_path, &root_folder_name),
)?;
for id in package.dependencies.values() {
pending_packages.push_back((id, false));
}
}
drop(single_process_lock);

View file

@ -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,