Skip to content

Commit 46f768a

Browse files
authored
Support lists of WITs in Resolve::select_world (#2288)
* Support lists of WITs in `Resolve::select_world` This extends `Resolve::select_world` to be usable for lists of WITs. One such example is wit-bindgen's `generate` macro's `path:` parameter, which can take a list of WIT paths. It currently uses a [custom `select_world` implementation], but with this patch in wit-parser, it would be able to use `Resolve::select_world` instead. I'm also considering extending the wit-bindgen CLI to accept multiple WIT paths; with this patch, it could use `Resolve::select_world` as well. [custom `select_world` implementation]: https://github.com/bytecodealliance/wit-bindgen/blob/0e2201b8629b4144f45867161de7bc1fe26133bb/crates/guest-rust/macro/src/lib.rs#L191 Specifically, this PR makes the package argument to `Resolve::select_world` an `Option`, and adds code to handle the case where it's `None`. A `None` means the caller has a list of WITs and as such doesn't have a single main package. This also rewrites the documentation for `select_world` to clean up already-obsolete references to the `packages` array argument, and to hopefully make it more clear how `select_world` makes its choice. * Remove debugging code. * Make an error message about the main package call it the main package. * Update test output for a new error message. * Switch back to a list of main packages.
1 parent 3f454c0 commit 46f768a

File tree

10 files changed

+484
-124
lines changed

10 files changed

+484
-124
lines changed

crates/wit-component/src/encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,7 @@ world test {
29712971
"#,
29722972
)
29732973
.unwrap();
2974-
let world = resolve.select_world(pkg, None).unwrap();
2974+
let world = resolve.select_world(&[pkg], None).unwrap();
29752975

29762976
let mut module = dummy_module(&resolve, world, ManglingAndAbi::Standard32);
29772977

crates/wit-component/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ world test-world {}
147147
// Parse pre-canned WIT to build resolver
148148
let mut resolver = Resolve::default();
149149
let pkg = resolver.push_str("in-code.wit", COMPONENT_WIT)?;
150-
let world = resolver.select_world(pkg, Some("test-world"))?;
150+
let world = resolver.select_world(&[pkg], Some("test-world"))?;
151151

152152
// Embed component metadata
153153
embed_component_metadata(&mut bytes, &resolver, world, StringEncoding::UTF8)?;

crates/wit-component/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl Bindgen {
364364
DecodedWasm::Component(..) => bail!("expected encoded wit package(s)"),
365365
};
366366
resolve = r;
367-
world = resolve.select_world(pkg, Some(world_name.into()))?;
367+
world = resolve.select_world(&[pkg], Some(world_name.into()))?;
368368
}
369369

370370
// Current format where `data` is a wasm component itself.

crates/wit-component/tests/components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ fn read_core_module(path: &Path, resolve: &Resolve, pkg: PackageId) -> Result<Ve
243243
let mut wasm = wat::parse_file(path)?;
244244
let name = path.file_stem().and_then(|s| s.to_str()).unwrap();
245245
let world = resolve
246-
.select_world(pkg, Some(name))
246+
.select_world(&[pkg], Some(name))
247247
.context("failed to select a world")?;
248248

249249
// Add this producer data to the wit-component metadata so we can make sure it gets through the

crates/wit-component/tests/linking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn encode(wat: &str, wit: Option<&str>) -> Result<Vec<u8>> {
141141
if let Some(wit) = wit {
142142
let mut resolve = Resolve::default();
143143
let pkg = resolve.push_str("test.wit", wit)?;
144-
let world = resolve.select_world(pkg, None)?;
144+
let world = resolve.select_world(&[pkg], None)?;
145145

146146
wit_component::embed_component_metadata(
147147
&mut module,

crates/wit-component/tests/targets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn load_test_wit(path: &Path) -> Result<(Resolve, WorldId)> {
8282
let mut resolve = Resolve::default();
8383
let pkg = resolve.push_file(&test_wit_path)?;
8484
let world_id = resolve
85-
.select_world(pkg, Some(TEST_TARGET_WORLD_ID))
85+
.select_world(&[pkg], Some(TEST_TARGET_WORLD_ID))
8686
.with_context(|| "failed to select world from package".to_string())?;
8787

8888
Ok((resolve, world_id))

crates/wit-component/tests/wit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn parse_wit_dir() -> Result<()> {
1212
let (package_id, _) = resolver.push_path("tests/wit/parse-dir/wit")?;
1313
assert!(
1414
resolver
15-
.select_world(package_id, "foo-world".into())
15+
.select_world(&[package_id], "foo-world".into())
1616
.is_ok()
1717
);
1818

@@ -26,7 +26,7 @@ fn parse_wit_file() -> Result<()> {
2626

2727
let mut resolver = Resolve::default();
2828
let (package_id, _) = resolver.push_path("tests/wit/parse-dir/wit/deps/bar/bar.wit")?;
29-
resolver.select_world(package_id, "bar-world".into())?;
29+
resolver.select_world(&[package_id], "bar-world".into())?;
3030
assert!(
3131
resolver
3232
.interfaces

crates/wit-parser/src/resolve.rs

Lines changed: 469 additions & 109 deletions
Large diffs are not rendered by default.

src/bin/wasm-tools/component.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl EmbedOpts {
349349
fn run(self) -> Result<()> {
350350
let (resolve, pkg_id) = self.resolve.load()?;
351351

352-
let world = resolve.select_world(pkg_id, self.world.as_deref())?;
352+
let world = resolve.select_world(&[pkg_id], self.world.as_deref())?;
353353

354354
if self.only_custom {
355355
let encoded = metadata::encode(
@@ -706,7 +706,7 @@ impl WitOpts {
706706
);
707707
}
708708
DecodedWasm::WitPackage(resolve, id) => {
709-
let world = resolve.select_world(*id, Some(world))?;
709+
let world = resolve.select_world(&[*id], Some(world))?;
710710
(resolve, world)
711711
}
712712
};
@@ -814,7 +814,7 @@ impl WitOpts {
814814
);
815815
}
816816
(DecodedWasm::WitPackage(resolve, id), world) => {
817-
let world = resolve.select_world(*id, world)?;
817+
let world = resolve.select_world(&[*id], world)?;
818818
(resolve, world)
819819
}
820820
};
@@ -959,7 +959,7 @@ impl TargetsOpts {
959959
/// Executes the application.
960960
fn run(self) -> Result<()> {
961961
let (resolve, pkg_id) = self.resolve.load()?;
962-
let world = resolve.select_world(pkg_id, self.world.as_deref())?;
962+
let world = resolve.select_world(&[pkg_id], self.world.as_deref())?;
963963
let component_to_test = self.input.get_binary_wasm()?;
964964

965965
wit_component::targets(&resolve, world, &component_to_test)?;
@@ -999,8 +999,8 @@ impl SemverCheckOpts {
999999

10001000
fn run(self) -> Result<()> {
10011001
let (resolve, pkg_id) = self.resolve.load()?;
1002-
let prev = resolve.select_world(pkg_id, Some(self.prev.as_str()))?;
1003-
let new = resolve.select_world(pkg_id, Some(self.new.as_str()))?;
1002+
let prev = resolve.select_world(&[pkg_id], Some(self.prev.as_str()))?;
1003+
let new = resolve.select_world(&[pkg_id], Some(self.new.as_str()))?;
10041004
wit_component::semver_check(resolve, prev, new)?;
10051005
Ok(())
10061006
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
error: multiple worlds found; one must be explicitly chosen:
1+
error: There are multiple worlds in `test:root`; one must be explicitly chosen:
22
test:root/w1
33
test:root/w2

0 commit comments

Comments
 (0)