Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ prettyplease = "0.2.20"
syn = { version = "2.0.89", features = ["printing"] }
futures = "0.3.31"

wat = "1.237.0"
wasmparser = "0.237.0"
wasm-encoder = "0.237.0"
wasm-metadata = { version = "0.237.0", default-features = false }
wit-parser = "0.237.0"
wit-component = "0.237.0"
wasm-compose = "0.237.0"
wat = "1.238.0"
wasmparser = "0.238.0"
wasm-encoder = "0.238.0"
wasm-metadata = { version = "0.238.0", default-features = false }
wit-parser = "0.238.0"
wit-component = "0.238.0"
wasm-compose = "0.238.0"

wit-bindgen-core = { path = 'crates/core', version = '0.44.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.44.0' }
Expand Down
44 changes: 4 additions & 40 deletions crates/guest-rust/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct Config {

/// The source of the wit package definition
enum Source {
/// A path to a wit directory
/// A list of paths to wit directories
Paths(Vec<PathBuf>),
/// Inline sources have an optional path to a directory of their dependencies
Inline(String, Option<Vec<PathBuf>>),
Expand Down Expand Up @@ -174,9 +174,10 @@ impl Parse for Config {
)]));
}
}
let (resolve, pkgs, files) =
let (resolve, main_packages, files) =
parse_source(&source, &features).map_err(|err| anyhow_to_syn(call_site, err))?;
let world = select_world(&resolve, &pkgs, world.as_deref())
let world = resolve
.select_world(&main_packages, world.as_deref())
.map_err(|e| anyhow_to_syn(call_site, e))?;
Ok(Config {
opts,
Expand All @@ -188,43 +189,6 @@ impl Parse for Config {
}
}

fn select_world(
resolve: &Resolve,
pkgs: &[PackageId],
world: Option<&str>,
) -> anyhow::Result<WorldId> {
if pkgs.len() == 1 {
resolve.select_world(pkgs[0], world)
} else {
assert!(!pkgs.is_empty());
match world {
Some(name) => {
if !name.contains(":") {
anyhow::bail!(
"with multiple packages a fully qualified \
world name must be specified"
)
}

// This will ignore the package argument due to the fully
// qualified name being used.
resolve.select_world(pkgs[0], world)
}
None => {
let worlds = pkgs
.iter()
.filter_map(|p| resolve.select_world(*p, None).ok())
.collect::<Vec<_>>();
match &worlds[..] {
[] => anyhow::bail!("no packages have a world"),
[world] => Ok(*world),
_ => anyhow::bail!("multiple packages have a world, must specify which to use"),
}
}
}
}
}

/// Parse the source
fn parse_source(
source: &Option<Source>,
Expand Down
4 changes: 3 additions & 1 deletion crates/guest-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,9 @@
/// // ["../path/to/wit1", "../path/to/wit2"]
/// // Usually used in testing, our test suite may want to generate code
/// // from wit files located in multiple paths within a single mod, and we
/// // don't want to copy these files again.
/// // don't want to copy these files again. Currently these locations must
/// // be ordered, as later paths can't contain dependencies on earlier
/// // paths. This restriction may be lifted in the future.
/// path: "../path/to/wit",
///
/// // Enables passing "inline WIT". If specified this is the default
Expand Down
4 changes: 2 additions & 2 deletions crates/test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ pub fn run_component_codegen_test(
fn parse_wit(path: &Path) -> (Resolve, WorldId) {
let mut resolve = Resolve::default();
let (pkg, _files) = resolve.push_path(path).unwrap();
let world = resolve.select_world(pkg, None).unwrap_or_else(|_| {
let world = resolve.select_world(&[pkg], None).unwrap_or_else(|_| {
// note: if there are multiples worlds in the wit package, we assume the "imports" world
resolve.select_world(pkg, Some("imports")).unwrap()
resolve.select_world(&[pkg], Some("imports")).unwrap()
});
(resolve, world)
}
12 changes: 8 additions & 4 deletions crates/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Runner<'_> {
let mut worlds = Vec::new();

let mut push_world = |kind: Kind, name: &str| -> Result<()> {
let world = resolve.select_world(pkg, Some(name)).with_context(|| {
let world = resolve.select_world(&[pkg], Some(name)).with_context(|| {
format!("failed to find expected `{name}` world to generate bindings")
})?;
worlds.push((world, kind));
Expand Down Expand Up @@ -582,8 +582,12 @@ impl Runner<'_> {
let mut resolve = wit_parser::Resolve::default();
let (pkg, _) = resolve.push_path(test).context("failed to load WIT")?;
let world = resolve
.select_world(pkg, None)
.or_else(|err| resolve.select_world(pkg, Some("imports")).map_err(|_| err))
.select_world(&[pkg], None)
.or_else(|err| {
resolve
.select_world(&[pkg], Some("imports"))
.map_err(|_| err)
})
.context("failed to select a world for bindings generation")?;
let world = resolve.worlds[world].name.clone();

Expand Down Expand Up @@ -992,7 +996,7 @@ status: {}",
let (pkg, _) = resolve
.push_path(&compile.component.bindgen.wit_path)
.context("failed to load WIT")?;
let world = resolve.select_world(pkg, Some(&compile.component.bindgen.world))?;
let world = resolve.select_world(&[pkg], Some(&compile.component.bindgen.world))?;
let mut module = fs::read(&p1).context("failed to read wasm file")?;
let encoded = wit_component::metadata::encode(&resolve, world, StringEncoding::UTF8, None)?;

Expand Down
24 changes: 16 additions & 8 deletions src/bin/wit-bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ struct Common {
#[clap(long = "out-dir")]
out_dir: Option<PathBuf>,

/// Location of WIT file(s) to generate bindings for.
/// Locations of WIT file(s) to generate bindings for.
///
/// This path can be either a directory containing `*.wit` files, a `*.wit`
/// file itself, or a `*.wasm` file which is a wasm-encoded WIT package.
/// Most of the time it's likely to be a directory containing `*.wit` files
/// with an optional `deps` folder inside of it.
/// These paths can be either directories containing `*.wit` files, `*.wit`
/// files themselves, or `*.wasm` files which are wasm-encoded WIT packages.
/// Most of the time they're likely to be directories containing `*.wit`
/// files with optional `deps` folders inside of them.
///
/// Currently these locations must be ordered, as later paths can't contain
/// dependencies on earlier paths. This restriction may be lifted in the
/// future.
#[clap(value_name = "WIT", index = 1)]
wit: PathBuf,
wit: Vec<PathBuf>,

/// Optionally specified world that bindings are generated for.
///
Expand Down Expand Up @@ -219,8 +223,12 @@ fn gen_world(
resolve.features.insert(feature.to_string());
}
}
let (pkg, _files) = resolve.push_path(&opts.wit)?;
let world = resolve.select_world(pkg, opts.world.as_deref())?;
let mut main_packages = Vec::new();
for wit in &opts.wit {
let (pkg, _files) = resolve.push_path(wit)?;
main_packages.push(pkg);
}
Comment on lines +226 to +230

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this reminds me one of the reasons I found this tricky to do before. The ideal implementation in my head is such that there's no order dependency of arguments on the CLI and the implementation internally would figure out how to slurp up all the WITs. As-implemented here, however, while later arguments could refer to WITs of earlier arguments it's not possible the other way around due to the explicit order of push_path.

Internally Resolve has everything necessary to do all the topo-sorting and everything, but it's not exposed currently through a push_many support or anything like that.

This is probably fine to have as a TODO, however, as this equally affects the paths argument in the Rust macro. Perhaps worth documenting though in the meantime and having an issue on wasm-tools to fill out at some point?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. In my current use cases, this ordering requirement isn't a problem, but I agree we should think about lifting this requirement. I've now added documentation to this PR, and filed bytecodealliance/wasm-tools#2292.

let world = resolve.select_world(&main_packages, opts.world.as_deref())?;
generator.generate(&resolve, world, files)?;

Ok(())
Expand Down
Loading