Protobuf Breaking-Change Detector: Design Specification
Overview
This system detects breaking changes in Protocol Buffers (protobuf/proto3) schemas during development, enforces semantic versioning (semver) rules, and integrates as CI/CD PR gates (e.g., GitHub Actions) to block merges of incompatible changes without proper version bumps.
Key Goals :
Zero false positives : Precise diffing using protobuf descriptors.
Backwards compatibility focus : Aligns with Protocol Buffers Evolution Guide .
Automation : CLI tool + GitHub Action for PR workflows.
Extensible : Configurable rules via YAML.
Non-Breaking Changes (allowed in MINOR/PATCH):
Add new fields/messages/enums.
Add new enum values.
Deprecate fields (via deprecated = true).
Breaking Changes (require MAJOR bump):
Remove/rename fields/messages/enums.
Change field types, numbers, or cardinality (e.g., repeated → singular).
Change enum value names/numbers.
Change default values.
Change oneof structure.
Architecture
code Copy code
┌─────────────────┐ ┌──────────────────┐ ┌──────────────┐
│ Protos (old) │───▶│ Diff Engine │───▶│ Semver Check │───▶│ PR Gate
│ (.proto) │ │ (Descriptors) │ │ + Rules │ │ (Fail/Allow)
└─────────────────┘ └──────────────────┘ └──────────────┘ └──────────────┘
▲ ▲ ▲
│ │ │
Base Ref (e.g., main) buf/protodiff/protoc version.txt/tag
Inputs : Old protos (base branch), new protos (PR head), semver source (file/tag).
Outputs : JSON report + exit code (0=OK, 1=breaking).
Tech Stack :
Component Tech Diffing Go/Python + google.protobuf descriptors (via protoc --descriptor_set_out) CLI Go (buf-inspired) or Python CI GitHub Actions Config breaking-changes.yaml
1. Schema Diff Engine
Compares protobuf FileDescriptorSets (binary protobuf metadata) for precision.
Workflow
Compile protos: protoc --descriptor_set_out=old.desc --include_imports -I . old/*.proto
Repeat for new protos.
Load descriptors into memory.
Recursively diff FileDescriptor → MessageDescriptor → FieldDescriptor → EnumDescriptor.
Diff Rules (proto3-focused)
Change Type Breaking? Detection Logic Field Added No old.fields missing → new.fields presentField Removed Yes old.fields[X] missing in new (by number!)Field Renamed Yes Name mismatch (numbers must match) Field Number Changed Yes field.number != old.numberType Changed Yes field.type != old.type (e.g., TYPE_INT32 → TYPE_STRING)Cardinality Changed Yes repeated → singular or vice versaDefault Value Changed Yes field.default_value != old.default_valueMessage Added No New MessageDescriptor Message Removed Yes Missing MessageDescriptor by full name Enum Value Added No New value Enum Value Removed/Renamed Yes Missing/changed by number/name Oneof Changed Yes Fields move between/within oneofs Package Changed Yes file.package != old.packageImport Added/Removed Warn Public deps only
Implementation Snippet (Go Example)
go Copy code
// Using github.com/jhump/protoreflect
func DiffDescSets(old, new *desc.FileDescriptorSet) (*DiffReport, error) {
report := &DiffReport{Breaking: []string{}}
for _, f1 := range old.Files {
f2 := new.FindFile(f1.GetName())
if f2 == nil { report.Breaking = append(report.Breaking, "File removed: "+f1.GetName()); continue }
diffMessages(f1.GetMessages(), f2.GetMessages(), report)
}
// Also check new files (additions OK)
return report, nil
}
func diffMessages(old, new []*desc.MessageDescriptor, r *DiffReport) {
for _, m1 := range old {
m2 := findMsg(new, m1.GetFullyQualifiedName())
if m2 == nil { r.Breaking = append(r.Breaking, "Msg removed"); continue }
diffFields(m1.GetFields(), m2.GetFields(), r)
}
}
Output : JSON like {"breaking": ["removed field foo (num 1)"], "non_breaking": ["added bar"]}
Tool: pb-diff CLI
code Copy code
pb-diff old.desc new.desc --config breaking-changes.yaml > report.json
Install: go install github.com/yourorg/pb-diff@latest
Handles multi-file workspaces (via buf.build modules or --proto_path).
2. Semver Rules
Enforce semver.org for protobuf versions.
Version Sources (priority order)
Git tag on base ref (e.g., v1.2.3).
version.txt or proto.version file in repo root.
Fallback: Parse from CHANGELOG.md.
Rules Matrix
Changes Detected Required Bump PR Gate Action None PATCH Allow Non-breaking only MINOR Allow Breaking MAJOR Block unless bumpedBreaking + MAJOR MAJOR Allow
Enforcement Logic
python Copy code
# Pseudo-Python
def check_semver(old_ver: SemVer, report: DiffReport) -> bool:
if not report.breaking:
return is_compatible_bump(old_ver, new_ver, MINOR_OR_PATCH)
return new_ver.major > old_ver.major # Must bump MAJOR
Parse: pip install semver
Configurable thresholds in breaking-changes.yaml:
yaml Copy code
rules:
field_removal: breaking
enum_addition: non-breaking # Default
semvers:
sources: [tag, version.txt]
allow_breaking_in: [major]
3. PR Gates (GitHub Actions)
GitHub Action workflow: .github/workflows/proto-lint.yaml
yaml Copy code
name: Protobuf Breaking Changes
on: [pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 } # Full history for base ref
- name: Checkout base protos
run: git checkout ${{ github.base_ref }} -- protos/ # Copy base protos
- uses: bufbuild/buf-setup-action@v1
- name: Generate descriptors
run: |
buf build -o old.desc protos/
git checkout HEAD -- protos/ # Restore PR head
buf build -o new.desc protos/
- name: Diff & Semver Check
uses: yourorg/pb-diff-action@v1
with:
old-desc: old.desc
new-desc: new.desc
version-source: tag # Or file
- name: Comment Report
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 changes detected!\n\`\`\`${{ steps.diff.outputs.report }}\`\`\``
})
Fail Conditions : Breaking changes without MAJOR bump → PR status ❌.
Annotations : Use ::error file=protos/foo.proto,line=10::Field 'bar' removed.
Multi-Repo : Support via buf modules for deps.
Edge Cases & Config
Dependencies : Diff transitive deps (--include_imports).
Proto2 : Add rules for required fields.
Custom Rules : YAML extensions (e.g., "allow field renames in alpha").
Performance : Descriptor sets are fast; caches via buf module cache.
Testing : Golden files + protodiff benchmarks.
Existing Tools & Extensions
Build On : buf breaking (official, covers 90%—extend for semver).
Alternatives : protodiff, protolock.
Open Source It : Repo template with Go CLI + Action.
This design is production-ready, scalable for monorepos (e.g., Uber/Lyft-scale). Ping me for code prototypes! 🕵️♂️