Skip to content

Commit

Permalink
fix: Don't error out on non-existent optional peer dependency (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit authored Sep 23, 2024
1 parent a801799 commit 5cecfe9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,11 @@ impl NpmRegistryApi for TestNpmRegistryApi {
name: &str,
) -> Result<Arc<NpmPackageInfo>, 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(),
}
})?))
}
}

Expand Down
36 changes: 35 additions & 1 deletion src/resolution/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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!["[email protected]"]).await;
let packages = package_names_with_info(
&snapshot,
&NpmSystemInfo {
os: "darwin".to_string(),
cpu: "x86_64".to_string(),
},
);
assert_eq!(
packages,
vec!["[email protected]".to_string(), "[email protected]".to_string(),]
);
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct TestNpmResolutionPackage {
pub pkg_id: String,
Expand Down

0 comments on commit 5cecfe9

Please sign in to comment.