Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to parse WIT files from multiple paths #1003

Merged
merged 1 commit into from
Jul 17, 2024

Conversation

iawia002
Copy link
Contributor

Background information reference: bytecodealliance/wasmtime#8939 (comment)

This patch allows us to parse WIT files from multiple paths, which is often useful in testing. We can unify all the generated code into a single mod. For example, for Wasmtime's case, we can do it like this:

before:

wit_bindgen::generate!({
    world: "test-command",
    path: "../wasi/wit",
    generate_all,
});

pub mod config {
    wit_bindgen::generate!({
        path: "../wasi-runtime-config/wit",
        world: "wasi:config/imports",
    });
}

// use the generated code:

use test_programs::wasi::sockets;
use test_programs::config::wasi::config;

after:

wit_bindgen::generate!({
    inline: "
        package wasmtime:test;

        world test {
            include wasi:cli/imports@0.2.0;
            include wasi:config/imports@0.2.0-draft;
            import wasi:http/types@0.2.0;
            import wasi:http/outgoing-handler@0.2.0;
        }
    ",
    paths: ["../wasi/wit", "../wasi-runtime-config/wit"],
    generate_all,
});

// use the generated code:

use test_programs::wasi::sockets;
use test_programs::wasi::config;

I am not familiar with the whole process of wit parsing, so I am not sure if there will be any side effects of doing this. I have tested the wasmtime case locally and it does work.

cc @alexcrichton

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Thanks for this!

/// // 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.
/// paths: ["../path/to/wit1", "../path/to/wit2"],
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using a new key for this could this syntax be folded into the previous key? For example path: "..." or path: [...] would be supported

@@ -0,0 +1,12 @@
wit_bindgen::generate!({
Copy link
Member

Choose a reason for hiding this comment

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

Adding a runtime test for this is pretty heavyweight and not much runtime functionality is tested here, so could this be added to crates/rust/tests/* instead?

let (pkg, sources) = resolve.push_path(normalized_path)?;
files.extend(sources);
Ok(pkg)
let mut parse = |paths: &Vec<String>| -> anyhow::Result<()> {
Copy link
Member

Choose a reason for hiding this comment

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

Can this be &[PathBuf] instead of &Vec<String>? That keeps the type-level annotation that these are paths and additionally avoids the dance of into_os_string().into_string().unwrap() below too

@iawia002
Copy link
Contributor Author

Thanks for the review, All comments addressed, PTAL

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants