Skip to content

Commit 9ff99da

Browse files
authored
add registry-backed WIT resolution (#250)
1 parent 33bee20 commit 9ff99da

11 files changed

Lines changed: 2041 additions & 107 deletions

File tree

‎Cargo.lock‎

Lines changed: 1136 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pyo3 = { version = "0.28.3", features = [
3030
], optional = true }
3131
wasmtime = "48.0.0"
3232
wasmtime-wasi = { version = "48.0.0", features = [ "p3" ] }
33+
wasm-pkg-client = "0.16.1"
3334
once_cell = "1.20.2"
3435
component-init-transform = "0.2.0"
3536
async-trait = "0.1.83"

‎README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ And finally generate the component:
6262
componentize-py -d hello.wit -w hello componentize --stub-wasi app -o app.wasm
6363
```
6464

65+
An exact, versioned world can also be loaded from a WebAssembly package
66+
registry:
67+
68+
```shell
69+
componentize-py -w wasi:cli/command@0.2.0 bindings wasi_cli
70+
```
71+
72+
Use `--registry-config PATH` to merge a standard `wasm-pkg` configuration file
73+
with the global configuration. Use `--no-default-registries` to start with an
74+
empty configuration before loading that file. Registry loading is lazy, and a
75+
matching local WIT definition takes precedence. Remote world references must
76+
use the exact `namespace:package/world@version` form.
77+
6578
To test it, you can install `wasmtime-py` and write a simple host app which uses
6679
it to load and run our component:
6780

‎src/command.rs‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ pub struct Common {
104104
/// If this is not specified, the module name will default to "wit".
105105
#[arg(long)]
106106
pub bindings_module: Option<String>,
107+
108+
/// Path to a wasm package registry configuration file.
109+
#[arg(long)]
110+
pub registry_config: Option<PathBuf>,
111+
112+
/// Disable the global and built-in registry mappings.
113+
#[arg(long)]
114+
pub no_default_registries: bool,
107115
}
108116

109117
#[derive(clap::Subcommand, Debug)]
@@ -242,7 +250,7 @@ pub fn run<T: Into<OsString> + Clone, I: IntoIterator<Item = T>>(args: I) -> Res
242250
}
243251

244252
fn generate_bindings(common: Common, bindings: Bindings) -> Result<()> {
245-
BindingsGenerator {
253+
let generator = BindingsGenerator {
246254
wit_paths: &common
247255
.wit_path
248256
.iter()
@@ -268,8 +276,10 @@ fn generate_bindings(common: Common, bindings: Bindings) -> Result<()> {
268276
.iter()
269277
.map(|(a, b)| (a.as_str(), b.as_str()))
270278
.collect(),
271-
}
272-
.generate()
279+
registry_config: common.registry_config.as_deref(),
280+
default_registries: !common.no_default_registries,
281+
};
282+
Runtime::new()?.block_on(generator.generate())
273283
}
274284

275285
fn componentize(common: Common, componentize: Componentize) -> Result<()> {
@@ -323,6 +333,8 @@ fn componentize(common: Common, componentize: Componentize) -> Result<()> {
323333
.map(|(a, b)| (a.as_str(), b.as_str()))
324334
.collect(),
325335
intersect_world: componentize.intersect_world.as_deref(),
336+
registry_config: common.registry_config.as_deref(),
337+
default_registries: !common.no_default_registries,
326338
}
327339
.generate(),
328340
)?;
@@ -461,6 +473,8 @@ mod tests {
461473
all_features: false,
462474
import_interface_name: Vec::new(),
463475
export_interface_name: Vec::new(),
476+
registry_config: None,
477+
no_default_registries: false,
464478
};
465479
let bindings = bindings(out_dir.path());
466480
generate_bindings(common, bindings)?;
@@ -489,6 +503,8 @@ mod tests {
489503
all_features: false,
490504
import_interface_name: Vec::new(),
491505
export_interface_name: Vec::new(),
506+
registry_config: None,
507+
no_default_registries: false,
492508
};
493509
let bindings = bindings(out_dir.path());
494510
generate_bindings(common, bindings)?;
@@ -517,6 +533,8 @@ mod tests {
517533
all_features: true,
518534
import_interface_name: Vec::new(),
519535
export_interface_name: Vec::new(),
536+
registry_config: None,
537+
no_default_registries: false,
520538
};
521539
let bindings = bindings(out_dir.path());
522540
generate_bindings(common, bindings)?;
@@ -544,6 +562,8 @@ mod tests {
544562
all_features: false,
545563
import_interface_name: Vec::new(),
546564
export_interface_name: Vec::new(),
565+
registry_config: None,
566+
no_default_registries: false,
547567
};
548568
let bindings = bindings(out_dir.path());
549569
generate_bindings(common.clone(), bindings)?;
@@ -654,6 +674,8 @@ world lib-world {
654674
all_features: false,
655675
import_interface_name: Vec::new(),
656676
export_interface_name: Vec::new(),
677+
registry_config: None,
678+
no_default_registries: false,
657679
},
658680
bindings(lib_wit_dir),
659681
)?;
@@ -668,6 +690,8 @@ world lib-world {
668690
all_features: false,
669691
import_interface_name: Vec::new(),
670692
export_interface_name: Vec::new(),
693+
registry_config: None,
694+
no_default_registries: false,
671695
},
672696
Componentize {
673697
app_name: "app".into(),
@@ -702,6 +726,8 @@ world lib-world {
702726
all_features: false,
703727
import_interface_name: Vec::new(),
704728
export_interface_name: Vec::new(),
729+
registry_config: None,
730+
no_default_registries: false,
705731
}
706732
}
707733

‎src/lib.rs‎

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod link;
4343
mod prelink;
4444
#[cfg(feature = "pyo3")]
4545
mod python;
46+
mod registry;
4647
mod stubwasi;
4748
mod summary;
4849
#[cfg(test)]
@@ -183,12 +184,14 @@ pub struct BindingsGenerator<'a> {
183184
pub output_dir: &'a Path,
184185
pub import_interface_names: &'a HashMap<&'a str, &'a str>,
185186
pub export_interface_names: &'a HashMap<&'a str, &'a str>,
187+
pub registry_config: Option<&'a Path>,
188+
pub default_registries: bool,
186189
/// Write into `output_dir` even if the bindings module directory exists.
187190
pub allow_existing: bool,
188191
}
189192

190193
impl BindingsGenerator<'_> {
191-
pub fn generate(&self) -> Result<()> {
194+
pub async fn generate(&self) -> Result<()> {
192195
// Here we parse the specified WIT paths and resolve the specified
193196
// worlds, then union them together and generate stub code for the
194197
// unioned world.
@@ -215,12 +218,27 @@ impl BindingsGenerator<'_> {
215218
packages.push((path, resolve.push_path(path)?.0));
216219
}
217220

218-
if packages.is_empty() {
221+
if packages.is_empty()
222+
&& (Path::new("wit").exists()
223+
|| !self
224+
.worlds
225+
.iter()
226+
.any(|world| registry::is_qualified_world_reference(world)))
227+
{
219228
// If no WIT directory was provided as a parameter, use ./wit by default.
220229
packages.push((Path::new("wit"), resolve.push_path("wit")?.0));
221230
}
222231

223-
let worlds = select_worlds(&resolve, self.worlds, &packages)?;
232+
let worlds = registry::resolve_requested_worlds(
233+
&mut resolve,
234+
&packages,
235+
self.worlds,
236+
self.registry_config,
237+
self.default_registries,
238+
)
239+
.await?
240+
.into_iter()
241+
.collect::<IndexSet<_>>();
224242
let world = match &worlds.iter().copied().collect::<Vec<_>>()[..] {
225243
[] => select_world(&resolve, None, &packages)?,
226244
&[world] => world,
@@ -395,6 +413,8 @@ pub struct ComponentGenerator<'a> {
395413
pub import_interface_names: &'a HashMap<&'a str, &'a str>,
396414
pub export_interface_names: &'a HashMap<&'a str, &'a str>,
397415
pub intersect_world: Option<&'a str>,
416+
pub registry_config: Option<&'a Path>,
417+
pub default_registries: bool,
398418
}
399419

400420
impl ComponentGenerator<'_> {
@@ -550,16 +570,47 @@ impl ComponentGenerator<'_> {
550570
packages.push((path, resolve.push_path(path)?.0));
551571
}
552572

553-
if packages.is_empty() {
573+
let registry_worlds = self
574+
.worlds
575+
.iter()
576+
.copied()
577+
.chain(self.intersect_world)
578+
.collect::<Vec<_>>();
579+
580+
if packages.is_empty()
581+
&& (Path::new("wit").exists()
582+
|| !registry_worlds
583+
.iter()
584+
.any(|world| registry::is_qualified_world_reference(world)))
585+
{
554586
// If no WIT directory was provided as a parameter and none were
555587
// referenced by Python packages, use ./wit by default.
556588
packages.push((Path::new("wit"), resolve.push_path("wit")?.0));
557589
}
558590

559-
let worlds = select_worlds(&resolve, self.worlds, &packages)?;
591+
let default_world =
592+
if self.worlds.is_empty() && configs.values().all(|(_, worlds)| worlds.is_empty()) {
593+
Some(select_world(&resolve, None, &packages)?)
594+
} else {
595+
None
596+
};
597+
598+
let registry_world_ids = registry::resolve_requested_worlds(
599+
&mut resolve,
600+
&packages,
601+
&registry_worlds,
602+
self.registry_config,
603+
self.default_registries,
604+
)
605+
.await?;
606+
607+
let worlds = registry_world_ids[..self.worlds.len()]
608+
.iter()
609+
.copied()
610+
.collect::<IndexSet<_>>();
560611

561-
let intersector = if let Some(world) = self.intersect_world {
562-
let intersector = select_world(&resolve, Some(world), &packages)?;
612+
let intersector = if self.intersect_world.is_some() {
613+
let intersector = *registry_world_ids.last().unwrap();
563614

564615
for &world in &worlds {
565616
intersect_world(&mut resolve, intersector, world);
@@ -588,7 +639,7 @@ impl ComponentGenerator<'_> {
588639

589640
if all_worlds.is_empty() {
590641
// No worlds specified; pick the default one, if available:
591-
let world = select_world(&resolve, None, &packages)?;
642+
let world = default_world.expect("the local default world was selected above");
592643

593644
if let Some(intersector) = intersector {
594645
intersect_world(&mut resolve, intersector, world);
@@ -1036,17 +1087,6 @@ fn select_world(
10361087
})
10371088
}
10381089

1039-
fn select_worlds(
1040-
resolve: &Resolve,
1041-
worlds: &[&str],
1042-
packages: &[(&Path, PackageId)],
1043-
) -> Result<IndexSet<WorldId>> {
1044-
worlds
1045-
.iter()
1046-
.map(|world| select_world(resolve, Some(world), packages))
1047-
.collect()
1048-
}
1049-
10501090
fn union_world(
10511091
resolve: &mut Resolve,
10521092
name: &str,

‎src/python.rs‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use {
1818
#[allow(clippy::too_many_arguments)]
1919
#[pyo3::pyfunction]
2020
#[pyo3(name = "componentize")]
21-
#[pyo3(signature = (wit_path, worlds, features, all_features, bindings_module, python_path, module_worlds, app_name, output_path, stub_wasi, import_interface_names, export_interface_names, full_names = None, intersect_world = None, world_module = None))]
21+
#[pyo3(signature = (wit_path, worlds, features, all_features, bindings_module, python_path, module_worlds, app_name, output_path, stub_wasi, import_interface_names, export_interface_names, full_names = None, intersect_world = None, world_module = None, registry_config = None, no_default_registries = false))]
2222
fn python_componentize(
2323
wit_path: Vec<PathBuf>,
2424
worlds: Vec<String>,
@@ -35,6 +35,8 @@ fn python_componentize(
3535
full_names: Option<bool>,
3636
intersect_world: Option<&str>,
3737
world_module: Option<&str>,
38+
registry_config: Option<PathBuf>,
39+
no_default_registries: bool,
3840
) -> PyResult<()> {
3941
let bindings_module = crate::resolve_deprecated(
4042
bindings_module.map(str::to_owned),
@@ -72,6 +74,8 @@ fn python_componentize(
7274
.map(|(a, b)| (a.as_ref(), b.as_ref()))
7375
.collect(),
7476
intersect_world,
77+
registry_config: registry_config.as_deref(),
78+
default_registries: !no_default_registries,
7579
}
7680
.generate(),
7781
)
@@ -82,7 +86,7 @@ fn python_componentize(
8286
#[allow(clippy::too_many_arguments)]
8387
#[pyo3::pyfunction]
8488
#[pyo3(name = "generate_bindings")]
85-
#[pyo3(signature = (wit_path, worlds, features, all_features, bindings_module, output_dir, import_interface_names, export_interface_names, allow_existing = false, full_names = None, world_module = None))]
89+
#[pyo3(signature = (wit_path, worlds, features, all_features, bindings_module, output_dir, import_interface_names, export_interface_names, allow_existing = false, full_names = None, world_module = None, registry_config = None, no_default_registries = false))]
8690
fn python_generate_bindings(
8791
wit_path: Vec<PathBuf>,
8892
worlds: Vec<String>,
@@ -95,6 +99,8 @@ fn python_generate_bindings(
9599
allow_existing: bool,
96100
full_names: Option<bool>,
97101
world_module: Option<&str>,
102+
registry_config: Option<PathBuf>,
103+
no_default_registries: bool,
98104
) -> PyResult<()> {
99105
let bindings_module = crate::resolve_deprecated(
100106
bindings_module.map(str::to_owned),
@@ -103,24 +109,30 @@ fn python_generate_bindings(
103109
false,
104110
)
105111
.map_err(|e| PyAssertionError::new_err(format!("{e:?}")))?;
106-
BindingsGenerator {
107-
wit_paths: &wit_path.iter().map(|v| v.as_path()).collect::<Vec<_>>(),
108-
worlds: &worlds.iter().map(|v| v.as_str()).collect::<Vec<_>>(),
109-
features: &features.iter().map(|v| v.as_str()).collect::<Vec<_>>(),
110-
all_features,
111-
bindings_module: bindings_module.as_deref(),
112-
output_dir: &output_dir,
113-
allow_existing,
114-
import_interface_names: &import_interface_names
115-
.iter()
116-
.map(|(a, b)| (a.as_ref(), b.as_ref()))
117-
.collect(),
118-
export_interface_names: &export_interface_names
119-
.iter()
120-
.map(|(a, b)| (a.as_ref(), b.as_ref()))
121-
.collect(),
122-
}
123-
.generate()
112+
(|| {
113+
Runtime::new()?.block_on(
114+
BindingsGenerator {
115+
wit_paths: &wit_path.iter().map(|v| v.as_path()).collect::<Vec<_>>(),
116+
worlds: &worlds.iter().map(|v| v.as_str()).collect::<Vec<_>>(),
117+
features: &features.iter().map(|v| v.as_str()).collect::<Vec<_>>(),
118+
all_features,
119+
bindings_module: bindings_module.as_deref(),
120+
output_dir: &output_dir,
121+
allow_existing,
122+
import_interface_names: &import_interface_names
123+
.iter()
124+
.map(|(a, b)| (a.as_ref(), b.as_ref()))
125+
.collect(),
126+
export_interface_names: &export_interface_names
127+
.iter()
128+
.map(|(a, b)| (a.as_ref(), b.as_ref()))
129+
.collect(),
130+
registry_config: registry_config.as_deref(),
131+
default_registries: !no_default_registries,
132+
}
133+
.generate(),
134+
)
135+
})()
124136
.map_err(|e| PyAssertionError::new_err(format!("{e:?}")))
125137
}
126138

0 commit comments

Comments
 (0)