I'm trying to get rules_rust to build a Wasm binary for me dependent on some Rust libraries I've configured with rust_library, but I can't quite figure out the right spelling.
How do I build a Rust Wasm binary with Bazel?
I'm trying to get rules_rust to build a Wasm binary for me dependent on some Rust libraries I've configured with rust_library, but I can't quite figure out the right spelling.
How do I build a Rust Wasm binary with Bazel?
The key is to realize that you need to convince Bazel to apply a platform transition to the build graph. Please take a moment to read the Bazel configuration transition docs for context on what on earth is actually happening here.
I've built a few helpers that make this easier for my project. The core of all of this is a wasm "platform transition":
I apply this transition to a cheeky little helper that "just" copies the final rust binary file for me... but keep in mind that this little transition at the final step in the build graph makes everything work! I believe I learned this trick via the Envoy Rust Bazel Wasm rules... but TBH, I can't quite remember.
And to make everything ergonomic, I provide a macro that forwards everything I need to a
rust_binaryand wraps it in the copier/transition-applier.Then I can include any of my Rust libraries as normal
rust_librarys included viadepsfor awasm_rust_binaryand the transition will apply through the whole build graph.P.S. Note that there's a special little config value in that
rust_binaryselectwhich forces these binaries to fail when forced into a build graph not configured for the wasm platform. Here's all that is: