diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-11-08 14:17:24 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-08 14:17:24 -0500 |
commit | cbb3f854332c348bb253e1284f7dcd7287bdf28d (patch) | |
tree | 93e2db9439bd745d48118a931bdc8bea61b81af5 /cli/lockfile.rs | |
parent | 2c72e8d5f45f12948310c1f0e1e2ed4f1d80fb51 (diff) |
feat(unstable/npm): support peer dependencies (#16561)
This adds support for peer dependencies in npm packages.
1. If not found higher in the tree (ancestor and ancestor siblings),
peer dependencies are resolved like a dependency similar to npm 7.
2. Optional peer dependencies are only resolved if found higher in the
tree.
3. This creates "copy packages" or duplicates of a package when a
package has different resolution due to peer dependency resolution—see
https://pnpm.io/how-peers-are-resolved. Unlike pnpm though, duplicates
of packages will have `_1`, `_2`, etc. added to the end of the package
version in the directory in order to minimize the chance of hitting the
max file path limit on Windows. This is done for both the local
"node_modules" directory and also the global npm cache. The files are
hard linked in this case to reduce hard drive space.
This is a first pass and the code is definitely more inefficient than it
could be.
Closes #15823
Diffstat (limited to 'cli/lockfile.rs')
-rw-r--r-- | cli/lockfile.rs | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/cli/lockfile.rs b/cli/lockfile.rs index d9d0c6d85..aa60b5789 100644 --- a/cli/lockfile.rs +++ b/cli/lockfile.rs @@ -16,6 +16,7 @@ use std::rc::Rc; use std::sync::Arc; use crate::args::ConfigFile; +use crate::npm::NpmPackageId; use crate::npm::NpmPackageReq; use crate::npm::NpmResolutionPackage; use crate::tools::fmt::format_json; @@ -40,7 +41,7 @@ pub struct NpmPackageInfo { #[derive(Clone, Debug, Default, Serialize, Deserialize)] pub struct NpmContent { - /// Mapping between requests for npm packages and resolved specifiers, eg. + /// Mapping between requests for npm packages and resolved packages, eg. /// { /// "chalk": "chalk@5.0.0" /// "react@17": "react@17.0.1" @@ -269,7 +270,7 @@ impl Lockfile { &mut self, package: &NpmResolutionPackage, ) -> Result<(), LockfileError> { - let specifier = package.id.serialize_for_lock_file(); + let specifier = package.id.as_serialized(); if let Some(package_info) = self.content.npm.packages.get(&specifier) { let integrity = package .dist @@ -286,7 +287,7 @@ This could be caused by: * the source itself may be corrupt Use \"--lock-write\" flag to regenerate the lockfile at \"{}\".", - package.id, self.filename.display() + package.id.display(), self.filename.display() ))); } } else { @@ -300,7 +301,7 @@ Use \"--lock-write\" flag to regenerate the lockfile at \"{}\".", let dependencies = package .dependencies .iter() - .map(|(name, id)| (name.to_string(), id.serialize_for_lock_file())) + .map(|(name, id)| (name.to_string(), id.as_serialized())) .collect::<BTreeMap<String, String>>(); let integrity = package @@ -309,7 +310,7 @@ Use \"--lock-write\" flag to regenerate the lockfile at \"{}\".", .as_ref() .unwrap_or(&package.dist.shasum); self.content.npm.packages.insert( - package.id.serialize_for_lock_file(), + package.id.as_serialized(), NpmPackageInfo { integrity: integrity.to_string(), dependencies, @@ -321,12 +322,13 @@ Use \"--lock-write\" flag to regenerate the lockfile at \"{}\".", pub fn insert_npm_specifier( &mut self, package_req: &NpmPackageReq, - version: String, + package_id: &NpmPackageId, ) { - self.content.npm.specifiers.insert( - package_req.to_string(), - format!("{}@{}", package_req.name, version), - ); + self + .content + .npm + .specifiers + .insert(package_req.to_string(), package_id.as_serialized()); self.has_content_changed = true; } } @@ -559,10 +561,12 @@ mod tests { id: NpmPackageId { name: "nanoid".to_string(), version: NpmVersion::parse("3.3.4").unwrap(), + peer_dependencies: Vec::new(), }, + copy_index: 0, dist: NpmPackageVersionDistInfo { - tarball: "foo".to_string(), - shasum: "foo".to_string(), + tarball: "foo".to_string(), + shasum: "foo".to_string(), integrity: Some("sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==".to_string()) }, dependencies: HashMap::new(), @@ -574,10 +578,12 @@ mod tests { id: NpmPackageId { name: "picocolors".to_string(), version: NpmVersion::parse("1.0.0").unwrap(), + peer_dependencies: Vec::new(), }, + copy_index: 0, dist: NpmPackageVersionDistInfo { - tarball: "foo".to_string(), - shasum: "foo".to_string(), + tarball: "foo".to_string(), + shasum: "foo".to_string(), integrity: Some("sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==".to_string()) }, dependencies: HashMap::new(), @@ -590,10 +596,12 @@ mod tests { id: NpmPackageId { name: "source-map-js".to_string(), version: NpmVersion::parse("1.0.2").unwrap(), + peer_dependencies: Vec::new(), }, + copy_index: 0, dist: NpmPackageVersionDistInfo { - tarball: "foo".to_string(), - shasum: "foo".to_string(), + tarball: "foo".to_string(), + shasum: "foo".to_string(), integrity: Some("sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==".to_string()) }, dependencies: HashMap::new(), @@ -606,7 +614,9 @@ mod tests { id: NpmPackageId { name: "source-map-js".to_string(), version: NpmVersion::parse("1.0.2").unwrap(), + peer_dependencies: Vec::new(), }, + copy_index: 0, dist: NpmPackageVersionDistInfo { tarball: "foo".to_string(), shasum: "foo".to_string(), |