Protobuf Breaking-Change Detector in CI: A Sherlockian Guide
Greetings, inquirer. You've stumbled upon a classic case of schema sabotage prevention—ensuring Protobuf evolution doesn't shatter downstream services. Breaking changes (e.g., field renames, type coercions, required field additions) lurk in PRs like hidden clues. We'll solve this with schema diffs, semantic rules, and PR blocking in CI. The prime suspect? Buf (the modern Protobuf toolkit), which excels here. It's free, fast, and battle-tested.
Why This Matters (The Crime Scene)
- Schema diffs: Compare
.proto files or compiled descriptors between commits.
- Semantic rules: Not just syntactic diffs—detect meaningful breaks like:
| Change Type | Example | Breaking? |
|---|
| Add required field | optional string name = 1; → string name = 1; | Yes |
| Change type | int32 → string | Yes |
| Remove field | Delete foo = 2 | Yes |
| Change field number | bar = 2 → bar = 3 | Yes |
| Add enum value | OK (forward-compatible) | No |
| Deprecate field | Add deprecated = true | No (warn only) |
- PR blocks: Fail CI on breaks, blocking merges. Use labels or status checks.
Prime Solution: Buf CLI in CI
Buf's buf lint with --against flag computes diffs against a baseline (e.g., main branch). It uses 50+ semantic rules out-of-the-box.
Setup (One-Time):
- Install Buf:
go install github.com/bufbuild/buf/cmd/buf@latest (or via brew/docker).
- Create
buf.yaml in repo root:
version: v2
lint:
use:
- DEFAULT # Includes breaking rules
breaking:
use:
- FILE # Detects file/package renames
- Commit it.
CI Integration: GitHub Actions Example (Adapt for GitLab/Jenkins)
Create .github/workflows/protos.yml:
name: Protobuf Breaking Changes
on:
pull_request:
paths:
- 'protos/**' # Trigger only on proto changes
jobs:
check-breaking:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for diffs
- uses: bufbuild/buf-setup-action@v1 # Installs latest buf
- uses: bufbuild/buf-push-action@v1
with:
buf_token: ${{ secrets.BUF_TOKEN }} # Optional: Push to Buf Schema Registry (BSR) for remote baselines
- name: Buf Lint (Style + Breaking)
run: |
buf lint
- name: Detect Breaking Changes vs Main
run: |
# Baseline: latest main commit
BASE_COMMIT=$(git merge-base origin/main HEAD)
buf lint --against .git/${BASE_COMMIT} # Local diff
# Or remote: buf lint --against buf.build/acme/petapis:abc123?v=1 (from BSR)
continue-on-error: false # Fail on breaks → blocks PR merge
- name: Comment PR on Failure (Optional)
if: failure()
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '🛑 **Breaking Protobuf changes detected!** Fix schema diffs before merging.'
})
- How it works:
git merge-base finds the common ancestor with main.
buf lint --against <commit> diffs descriptors semantically.
- Fails → GitHub status check blocks merge (enable "Require status checks" in branch protection).
- BSR Bonus: Push schemas to Buf Schema Registry (
buf push), reference by tag (buf lint --against buf.build/org/repo:tag?v=1). Immutable baselines, collaboration.
Output Example (On Break):
protos/v1/user.proto:4:3: Found breaking change against .git/abc123:
Added required field "new_required_field" to message "User".
Advanced Tweaks
- Custom Rules: Extend
buf.yaml with except or plugins.
breaking:
use:
- FIELD # Stricter field rules
except:
- IDs: PACKAGE_NEW # Allow new packages
- Diff Protos Directly: For non-Buf setups:
protoc --descriptor_set_out=old.desc old.proto
protoc --descriptor_set_out=new.desc new.proto
buf diff --against old.desc new.desc # Or custom Go tool
- Semantic Depth: Use
protoc --include_imports for transitive deps.
- Multi-Language: Works with Go, Java, Python, etc.—diffs are language-agnostic.
Alternatives (If Buf Doesn't Fit)
| Tool | Pros | Cons | CI Fit |
|---|
| protolock | Locks deps, diffs JSON exports | Less semantic rules | Simple YAML CI |
| protoc-gen-validate | Runtime + schema checks | Not diff-focused | Pair with diffs |
| Custom (Go/Python) | Full control (e.g., github.com/bufbuild/protovalidate) | Reinvent wheel | Heavy |
| Spectral (via OpenAPI) | If converting proto to OAS | Overhead | Niche |
Verdict
Deploy Buf today—it's the smoking gun. Test locally: buf lint --against HEAD~1. Your CI will block breaks like a Scotland Yard gatekeeper. Questions on your stack (e.g., monorepo)? Provide details; the game is afoot!
—Sherlock
Trailblazer Labs