Skip to content

Commit 9e66cb2

Browse files
committed
dependency kind enables component deps
1 parent f299395 commit 9e66cb2

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

crates/wasm-pkg-core/src/resolver.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ use crate::{lock::LockFile, wit::get_packages};
3030
/// The name of the default registry.
3131
pub const DEFAULT_REGISTRY_NAME: &str = "default";
3232

33+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
34+
pub enum DependencyKind {
35+
Component,
36+
Wit,
37+
}
38+
39+
impl Default for DependencyKind {
40+
fn default() -> Self {
41+
Self::Wit
42+
}
43+
}
3344
// TODO: functions for resolving dependencies from a lock file
3445

3546
/// Represents a WIT package dependency.
@@ -39,7 +50,7 @@ pub enum Dependency {
3950
Package(RegistryPackage),
4051

4152
/// The dependency is a path to a local directory or file.
42-
Local(PathBuf),
53+
Local(PathBuf, DependencyKind),
4354
}
4455

4556
impl Serialize for Dependency {
@@ -68,13 +79,14 @@ impl Serialize for Dependency {
6879
.serialize(serializer)
6980
}
7081
}
71-
Self::Local(path) => {
82+
Self::Local(path, kind) => {
7283
#[derive(Serialize)]
7384
struct Entry<'a> {
7485
path: &'a PathBuf,
86+
kind: &'a DependencyKind,
7587
}
7688

77-
Entry { path }.serialize(serializer)
89+
Entry { path, kind }.serialize(serializer)
7890
}
7991
}
8092
}
@@ -112,30 +124,37 @@ impl<'de> Deserialize<'de> for Dependency {
112124
package: Option<PackageRef>,
113125
version: Option<VersionReq>,
114126
registry: Option<String>,
127+
kind: DependencyKind,
115128
}
116129

117130
let entry = Entry::deserialize(MapAccessDeserializer::new(map))?;
118131

119-
match (entry.path, entry.package, entry.version, entry.registry) {
120-
(Some(path), None, None, None) => Ok(Self::Value::Local(path)),
121-
(None, name, Some(version), registry) => {
132+
match (
133+
entry.path,
134+
entry.package,
135+
entry.version,
136+
entry.registry,
137+
entry.kind,
138+
) {
139+
(Some(path), None, None, None, kind) => Ok(Self::Value::Local(path, kind)),
140+
(None, name, Some(version), registry, _) => {
122141
Ok(Self::Value::Package(RegistryPackage {
123142
name,
124143
version,
125144
registry,
126145
}))
127146
}
128-
(Some(_), None, Some(_), _) => Err(de::Error::custom(
147+
(Some(_), None, Some(_), _, _) => Err(de::Error::custom(
129148
"cannot specify both `path` and `version` fields in a dependency entry",
130149
)),
131-
(Some(_), None, None, Some(_)) => Err(de::Error::custom(
150+
(Some(_), None, None, Some(_), _) => Err(de::Error::custom(
132151
"cannot specify both `path` and `registry` fields in a dependency entry",
133152
)),
134-
(Some(_), Some(_), _, _) => Err(de::Error::custom(
153+
(Some(_), Some(_), _, _, _) => Err(de::Error::custom(
135154
"cannot specify both `path` and `package` fields in a dependency entry",
136155
)),
137-
(None, None, _, _) => Err(de::Error::missing_field("package")),
138-
(None, Some(_), None, _) => Err(de::Error::missing_field("version")),
156+
(None, None, _, _, _) => Err(de::Error::missing_field("package")),
157+
(None, Some(_), None, _, _) => Err(de::Error::missing_field("version")),
139158
}
140159
}
141160
}
@@ -469,10 +488,8 @@ impl<'a> DependencyResolver<'a> {
469488
&mut self,
470489
name: &PackageRef,
471490
dependency: &Dependency,
472-
is_wit: bool,
473491
) -> Result<()> {
474-
self.add_dependency_internal(name, dependency, false, is_wit)
475-
.await
492+
self.add_dependency_internal(name, dependency, false).await
476493
}
477494

478495
/// Add a dependency to the resolver. If the dependency already exists, then it will be
@@ -481,18 +498,15 @@ impl<'a> DependencyResolver<'a> {
481498
&mut self,
482499
name: &PackageRef,
483500
dependency: &Dependency,
484-
is_wit: bool,
485501
) -> Result<()> {
486-
self.add_dependency_internal(name, dependency, true, is_wit)
487-
.await
502+
self.add_dependency_internal(name, dependency, true).await
488503
}
489504

490505
async fn add_dependency_internal(
491506
&mut self,
492507
name: &PackageRef,
493508
dependency: &Dependency,
494509
force_override: bool,
495-
is_wit: bool,
496510
) -> Result<()> {
497511
match dependency {
498512
Dependency::Package(package) => {
@@ -533,7 +547,7 @@ impl<'a> DependencyResolver<'a> {
533547
},
534548
);
535549
}
536-
Dependency::Local(p) => {
550+
Dependency::Local(p, kind) => {
537551
// A local path dependency, insert a resolution immediately
538552
let res = DependencyResolution::Local(LocalResolution {
539553
name: name.clone(),
@@ -547,13 +561,15 @@ impl<'a> DependencyResolver<'a> {
547561

548562
// // Now that we check we haven't already inserted this dep, get the packages from the
549563
// // local dependency and add those to the resolver before adding the dependency
550-
if is_wit {
564+
// if is_wit {
565+
if kind == &DependencyKind::Wit {
551566
let (_, packages) = get_packages(p)
552567
.context("Error getting dependent packages from local dependency")?;
553568
Box::pin(self.add_packages(packages))
554569
.await
555570
.context("Error adding packages to resolver for local dependency")?;
556571
}
572+
// }
557573

558574
let prev = self.resolutions.insert(name.clone(), res);
559575
assert!(prev.is_none());
@@ -567,17 +583,16 @@ impl<'a> DependencyResolver<'a> {
567583
/// requirements to the resolver
568584
pub async fn add_packages(
569585
&mut self,
570-
packages: impl IntoIterator<Item = (PackageRef, VersionReq, bool)>,
586+
packages: impl IntoIterator<Item = (PackageRef, VersionReq)>,
571587
) -> Result<()> {
572-
for (package, req, is_wit) in packages {
588+
for (package, req) in packages {
573589
self.add_dependency(
574590
&package,
575591
&Dependency::Package(RegistryPackage {
576592
name: Some(package.clone()),
577593
version: req,
578594
registry: None,
579595
}),
580-
is_wit,
581596
)
582597
.await?;
583598
}

crates/wasm-pkg-core/src/wit.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::{
1515
config::Config,
1616
lock::LockFile,
1717
resolver::{
18-
DecodedDependency, Dependency, DependencyResolution, DependencyResolutionMap,
19-
DependencyResolver, LocalResolution, RegistryPackage,
18+
DecodedDependency, Dependency, DependencyKind, DependencyResolution,
19+
DependencyResolutionMap, DependencyResolver, LocalResolution, RegistryPackage,
2020
},
2121
};
2222

@@ -117,7 +117,7 @@ pub async fn fetch_dependencies(
117117
/// for resolving dependencies.
118118
pub fn get_packages(
119119
path: impl AsRef<Path>,
120-
) -> Result<(PackageRef, HashSet<(PackageRef, VersionReq, bool)>)> {
120+
) -> Result<(PackageRef, HashSet<(PackageRef, VersionReq)>)> {
121121
let group =
122122
wit_parser::UnresolvedPackageGroup::parse_path(path).context("Couldn't parse package")?;
123123

@@ -137,7 +137,7 @@ pub fn get_packages(
137137
);
138138

139139
// Get all package refs from the main package and then from any nested packages
140-
let packages: HashSet<(PackageRef, VersionReq, bool)> =
140+
let packages: HashSet<(PackageRef, VersionReq)> =
141141
packages_from_foreign_deps(group.main.foreign_deps.into_keys())
142142
.chain(
143143
group
@@ -168,12 +168,12 @@ pub async fn resolve_dependencies(
168168
let dep = match (ovride.path.as_ref(), ovride.version.as_ref()) {
169169
(Some(path), None) => {
170170
let path = tokio::fs::canonicalize(path).await?;
171-
Dependency::Local(path)
171+
Dependency::Local(path, DependencyKind::Wit)
172172
}
173173
(Some(path), Some(_)) => {
174174
tracing::warn!("Ignoring version override for local package");
175175
let path = tokio::fs::canonicalize(path).await?;
176-
Dependency::Local(path)
176+
Dependency::Local(path, DependencyKind::Wit)
177177
}
178178
(None, Some(version)) => Dependency::Package(RegistryPackage {
179179
name: Some(pkg.clone()),
@@ -186,7 +186,7 @@ pub async fn resolve_dependencies(
186186
}
187187
};
188188
resolver
189-
.add_dependency(&pkg, &dep, true)
189+
.add_dependency(&pkg, &dep)
190190
.await
191191
.context("Unable to add dependency")?;
192192
}
@@ -289,7 +289,7 @@ pub async fn populate_dependencies(
289289

290290
fn packages_from_foreign_deps(
291291
deps: impl IntoIterator<Item = PackageName>,
292-
) -> impl Iterator<Item = (PackageRef, VersionReq, bool)> {
292+
) -> impl Iterator<Item = (PackageRef, VersionReq)> {
293293
deps.into_iter().filter_map(|dep| {
294294
let name = PackageRef::new(dep.namespace.parse().ok()?, dep.name.parse().ok()?);
295295
let version = match dep.version {
@@ -301,7 +301,6 @@ fn packages_from_foreign_deps(
301301
version
302302
.parse()
303303
.expect("Unable to parse into version request, this is programmer error"),
304-
true,
305304
))
306305
})
307306
}

0 commit comments

Comments
 (0)