Compiling Buf plugins to WebAssembly
WebAssembly (Wasm) is a portable binary instruction format that allows you to run code written in multiple languages. Compiling Buf plugins to Wasm allows them to run on any platform the Buf CLI supports, including Windows, macOS, and Linux. You can use any language that supports Wasm, including Go, Rust, and C++.
To use WebAssembly with Buf plugins, you need to compile the plugin to Wasm.
The process varies depending on the language you're using—in this example, we'll use the rpc-suffix
plugin built in Go from the tutorial.
Compiling the plugin
To compile your plugin to Wasm, you need to target the WebAssembly System Interface (WASI) syscall API. WASI provides a standard interface for Wasm modules to interact with the host system. The supported version is WASI 0.1.
For Go plugins, you can compile your plugin by setting the GOOS
and GOARCH
environment variables to wasip1
and wasm
, respectively.
This tells the Go compiler to target the WASI syscall API and compile the code to Wasm.
For example, this command compiles the rpc-suffix
plugin outputting a rpc-suffix.wasm
file:
The output of this command is a Wasm binary file named rpc-suffix.wasm
.
The .wasm
suffix is an important convention, as it tells the Buf CLI the plugin type.
If the plugin doesn't have the .wasm
suffix, the Buf CLI interprets it as a native plugin.
Using the plugin
To use your Wasm plugin with Buf, update your buf.yaml
file to point to the compiled Wasm binary.
The plugin must contain the .wasm
extension in the plugin
field.
For example, update the plugin field in your buf.yaml
file to the path of the rpc-suffix.wasm
binary:
version: v2
modules:
- path: proto
name: buf.build/tutorials/lint-plugin
lint:
use:
- STANDARD
- RPC_SUFFIX
plugins:
- - plugin: rpc-suffix
+ - plugin: rpc-suffix.wasm
Related docs
Now that you have a working WebAssembly plugin, you can learn how to create a plugin from scratch in the tutorial. Check out these docs for more detailed information on the Buf plugin framework:
- Bufplugin framework: The APIs for plugins to the Buf platform
bufplugin-go
: The Go library for creating plugins on the Bufplugin platform- PluginRPC: The underlying framework for writing plugins
pluginrpc-go
: The Go library for writing SDKs using PluginRPC- Go WASI support: Go support for the WASI syscall API