From 5cecfe98baacae557cad7308aafc3f127aee3323 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Mon, 23 Sep 2024 10:56:33 -0700 Subject: [PATCH] fix: Don't error out on non-existent optional peer dependency (#69) --- src/registry.rs | 11 +++++------ src/resolution/graph.rs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index da88ab3..6efcb1c 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -534,12 +534,11 @@ impl NpmRegistryApi for TestNpmRegistryApi { name: &str, ) -> Result, NpmRegistryPackageInfoLoadError> { let infos = self.package_infos.borrow(); - Ok(Arc::new( - infos - .get(name) - .cloned() - .unwrap_or_else(|| panic!("Not found: {name}")), - )) + Ok(Arc::new(infos.get(name).cloned().ok_or_else(|| { + NpmRegistryPackageInfoLoadError::PackageNotExists { + package_name: name.into(), + } + })?)) } } diff --git a/src/resolution/graph.rs b/src/resolution/graph.rs index 8052b34..ba1a518 100644 --- a/src/resolution/graph.rs +++ b/src/resolution/graph.rs @@ -981,8 +981,17 @@ impl<'a, TNpmRegistryApi: NpmRegistryApi> let mut child_deps_iter = child_deps.iter(); while let Some(package_info) = infos.next().await { - let package_info = package_info?; let dep = child_deps_iter.next().unwrap(); + let package_info = match package_info { + Ok(info) => info, + // npm doesn't fail on non-existent optional peer dependencies + Err(NpmRegistryPackageInfoLoadError::PackageNotExists { .. }) + if matches!(dep.kind, NpmDependencyEntryKind::OptionalPeer) => + { + continue + } + Err(e) => return Err(e.into()), + }; match dep.kind { NpmDependencyEntryKind::Dep => { @@ -3895,6 +3904,31 @@ mod test { } } + #[tokio::test] + async fn non_existent_optional_peer_dep() { + let api = TestNpmRegistryApi::default(); + api.ensure_package_version("package-a", "1.0.0"); + api.ensure_package_version("package-b", "1.0.0"); + api.add_optional_peer_dependency( + ("package-b", "1.0.0"), + ("package-non-existent", "*"), + ); + api.add_dependency(("package-a", "1.0.0"), ("package-b", "*")); + let snapshot = + run_resolver_and_get_snapshot(api, vec!["package-a@1.0.0"]).await; + let packages = package_names_with_info( + &snapshot, + &NpmSystemInfo { + os: "darwin".to_string(), + cpu: "x86_64".to_string(), + }, + ); + assert_eq!( + packages, + vec!["package-a@1.0.0".to_string(), "package-b@1.0.0".to_string(),] + ); + } + #[derive(Debug, Clone, PartialEq, Eq)] struct TestNpmResolutionPackage { pub pkg_id: String,