Buf Tooling
Sonata uses Buf for protobuf schema management, providing linting, breaking change detection, and code generation.
Overview
Buf is a modern build tool for Protocol Buffers that replaces the traditional protoc compiler with a more developer-friendly experience.
Configuration
buf.yaml
Defines the module and linting rules:
version: v2
modules:
- path: proto
lint:
use:
- DEFAULT
breaking:
use:
- FILEbuf.gen.yaml
Configures code generation plugins:
version: v2
plugins:
- remote: buf.build/connectrpc/go:v1.18.1
out: gen
opt: paths=source_relative
- remote: buf.build/protocolbuffers/go:v1.33.0
out: gen
opt: paths=source_relativeCommon Commands
Generate Code
buf generateGenerates Go code from all .proto files into the gen/ directory.
Lint Schemas
buf lintChecks schemas against style and correctness rules.
Check Breaking Changes
buf breaking --against '.git#branch=main'Detects breaking changes compared to a reference (e.g., main branch).
Format Schemas
buf format -wFormats all .proto files consistently.
Generated Output
The gen/ directory mirrors the proto/ structure:
gen/
├── account/v1/v1.pb.go
├── api/v1/
│ ├── account.pb.go
│ ├── chain.pb.go
│ └── v1connect/
│ ├── account.connect.go
│ └── chain.connect.go
├── chain/v1/
│ ├── account.pb.go
│ ├── tx.pb.go
│ └── ...
└── ...*.pb.go— Message types and serialization*connect/*.connect.go— Service client/server stubs
Remote Plugins
Buf uses remote plugins from buf.build, eliminating the need to install local protoc plugins:
| Plugin | Purpose |
|---|---|
buf.build/protocolbuffers/go | Go message types |
buf.build/connectrpc/go | ConnectRPC service stubs |
Workflow
- Edit
.protofiles inproto/ - Run
buf lintto check for issues - Run
buf generateto updategen/ - Commit both
proto/andgen/changes together