Overview
The microservices-app project usesbuf generate to automatically generate code for both backend (Go) and frontend (TypeScript) from proto definitions.
buf.gen.yaml Configuration
Thebuf.gen.yaml file at the repository root configures code generation:
Configuration Details
- version: Uses buf v2 configuration format
- clean: Removes existing generated files before regenerating (ensures clean output)
- plugins: Defines four code generation plugins
Code Generation Plugins
Go Plugins (Backend)
1. protocolbuffers/go
Generates standard Go protobuf message types:- Output directory:
services/gen/go/ - Option:
paths=source_relative- Generates files relative to proto file location - Creates: Message structs, serialization methods, getters
2. connectrpc/go
Generates Connect-RPC service stubs for Go:- Output directory:
services/gen/go/ - Option:
paths=source_relative - Creates: Service interfaces, client implementations, HTTP handlers
- Compatible with both gRPC and HTTP/JSON
TypeScript Plugins (Frontend)
3. bufbuild/es
Generates TypeScript message types using Protobuf-ES:- Output directory:
frontend/src/gen/ - Option:
target=ts- Generates TypeScript (not JavaScript) - Creates: Message classes with type-safe builders
4. connectrpc/query-es
Generates connect-query hooks for React:- Output directory:
frontend/src/gen/ - Option:
target=ts - Creates: React Query hooks for each RPC method
- Integrates with TanStack Query for data fetching
Running Code Generation
Generate code from all proto files:- Cleans existing generated files (due to
clean: true) - Runs all four plugins in order
- Outputs to
services/gen/go/andfrontend/src/gen/
Generated File Structure
Go Backend Output
TypeScript Frontend Output
Generated Code Examples
Go Service Interface
Fromproto/greeter/v1/greeter.proto:
services/gen/go/greeter/v1/greeterconnect/greeter.connect.go:
TypeScript React Query Hook
Generates infrontend/src/gen/greeter/v1/greeter-GreeterService_connectquery.ts:
Integration with Build Process
Development Workflow
- Edit proto files in
proto/ - Run
buf generateto regenerate code - Use generated types in Go services and TypeScript frontend
Git Tracking
Generated files are tracked in git to ensure:- Consistent builds across environments
- No dependency on buf during container builds
- Clear diffs when proto definitions change
Pre-commit Hooks
The devenv pre-commit hooks do not automatically runbuf generate. You must manually regenerate code after changing proto files.
CI Validation
GitHub Actions CI verifies that generated code is up-to-date:Ignoring Generated Code
Frontend Linting
Generated TypeScript code infrontend/src/gen/ is excluded from Biome linting:
Backend Linting
Generated Go code inservices/gen/go/ is excluded from golangci-lint using build tags and path patterns.
Troubleshooting
Stale Generated Files
If you encounter import errors after updating proto files:clean: true option automatically removes old files.
Plugin Version Issues
Buf automatically downloads plugins from the Buf Schema Registry. If you see version conflicts:Missing Dependencies
Ensure your environment includes buf:direnv allow to reload the devenv environment.
Next Steps
- Creating Services - Learn how to create new proto definitions
- Overview - Return to Protocol Buffers overview