wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Default to only generating bindings for the local wit package

Open rylev opened this issue 2 years ago • 0 comments

This issue was originally encountered using wasmtime::bindgen, but I'm posting this here since I think it likely impacts all bindings generators.

Currently, bindings are generated for the package that contains the target world as well as all its dependencies. This can sometimes be the right thing, especially if the generated code does not need to interact with pre-existing code where bindings have already been generated. However, many users will want to interact with pre-existing code (mainly wasi projects) that already have their own bindings.

wit-bindgen and wasmtime::bindgen support pointing the generator at existing types in the form of the with directive. However, if the user forgets to do this, and generates code that is redundant with existing bindings that live somewhere else, the user can run into some very cryptic and hard to understand errors (e.g., resource types not type-checking).

In order to fix this, I propose we flip the default around to requiring the user to specify how non-local types should be treated. This can happen in two ways. First, the user can use the existing with directive. If the user forgets to add a with directive , they will get a constructive error message saying that they need to point to where that type lives.

Second, is the case of users who don't want to use existing bindings (i.e., because there might not even be an authoritative set of bindings, and this is totally fine as long as they don't interact with code that expects authoritative bindings). In that case, we would add a second option which is essentially "please, generate the bindings for me". This would mirror the current behavior with the key difference that it is opt-in. The exact syntax for this is left as an exercise for the reader.

rylev avatar Mar 21 '24 18:03 rylev