protobuf_generate
protobuf_generate runs one protoc generator with the managed compiler. Generation runs during xx run; pass the returned dependency to deps when another task needs generated files.
Arguments
| Argument | Required | Description |
|---|---|---|
sources | Yes | Non-empty list of project-relative .proto files. |
generator | Yes | Built-in generator name or a generator returned by protobuf_generator or a language package helper. |
output | Yes | Project-relative output directory. |
proto_paths | No | Non-empty list of project-relative import roots. Defaults to ["."]. |
options | No | Generator option strings, each passed through the matching --<generator>_opt flag. Defaults to []. |
deps | No | Dependencies activated before generation, in list order. Defaults to []. |
The selected compiler is automatic; do not add protoc to deps. Standard imports such as google/protobuf/timestamp.proto work without another import root. Source files, import roots, and outputs must remain inside the project after symlinks are resolved. Missing output directories are created. Output trees cannot contain symbolic links or overlap another task's output.
String generators are limited to cpp, csharp, java, js, kotlin, objc, php, pyi, python, rbs, ruby, and rust. These built-ins vary by Protobuf version. For example, js exists only in older releases, while newer generators require newer releases. Unsupported generator/version combinations fail in protoc.
Use a typed generator or language-specific helper for plugins. Plugin installation runs automatically; do not also add it to deps. Prefer helpers such as go_protobuf. Use deps only for additional prerequisites.
Example
load("@[email protected]", "protobuf_generate")
protobuf_generate(
sources=["proto/example/v1/service.proto"],
generator="python",
output="generated/python",
proto_paths=["proto"],
)
Go plugin example
load("@[email protected]", "go_protobuf")
load("@[email protected]", "protobuf_generate")
protobuf_generate(
sources=["proto/example/v1/service.proto"],
generator=go_protobuf("1.36.6"),
output="generated/go",
proto_paths=["proto"],
options=["paths=source_relative"],
)