Skip to content

build: check the Protobuf schema with buf - #28

Merged
xtk8532704 merged 3 commits into
mainfrom
feat/buf-schema-checks
Dec 3, 2026
Merged

xtk8532704 merged 3 commits into
mainfrom
feat/buf-schema-checks

Conversation

@Max-Bin

@Max-Bin Max-Bin commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #27.

CI now checks the Protobuf schema with buf 1.75.0, in a new protobuf job that is part of the aggregate ci result:

Check Runs on Rules
buf lint every CI run STANDARD, except four rules the released names cannot meet
buf format --diff --exit-code every CI run buf's formatter
buf breaking pull requests FILE, against the latest release tag reachable from the base

Two commits:

  1. style(proto) formats the schema as buf format does: six one-line messages, and two comments that followed a closing brace and now follow their field. The descriptors without source info are byte-identical, and protoc generates the same Python bindings.
  2. build adds:
    • buf.yaml, the job, and buf's lint and format pre-commit hooks;
    • the checks in the README ("Check changes") and in VERSIONING.md ("Compatibility").

Design choices

These answer #27's questions provisionally; each is easy to change.

  • Lint exceptions.
    • The rules: PACKAGE_DIRECTORY_MATCH, PACKAGE_VERSION_SUFFIX, ENUM_VALUE_PREFIX and ENUM_ZERO_VALUE_SUFFIX.
    • Meeting them would rename t4scene.protobuf or every enum value, which breaks the generated bindings and the JSON form. New declarations follow the existing style, and buf.yaml says why each rule is off.
  • FILE rules for compatibility.
    • The release covers the generated Python and C++ bindings, so a renamed or removed field breaks callers even when its number is kept.
    • Removing a field whose number and name are reserved is reported too, since generated code loses the field.
  • Against the latest release tag. The job compares with git describe --tags --abbrev=0 --match 'v[0-9]*' of the pull request's base, today v0.1.0, because VERSIONING.md speaks of released fields. subdir=t4scene/proto lines the tag's files up with the module, so tags made before buf.yaml existed compare too.
  • Intended breaks.
    • The step reports the break and then passes with a warning, provided the pull request carries the breaking-change label and changes CHANGELOG.md.
    • The labels are read through the API when the job runs, not from the event, so re-running the job after labelling sees them.
    • The label does not exist yet; until it is created, every break fails.
  • Only on pull requests. That is where the label decision is made. Pushes, tags and merge groups run lint and the format check.
  • buf from its release, checksum-checked, as the workflow already installs actionlint. No new action, no registry, no account.
  • Code generation unchanged. Building the SDK still uses grpc_tools.protoc and protoc, and never needs buf.

Verification

Check Result
buf lint, buf format --diff --exit-code pass
buf breaking against v0.1.0 no change reported
buf breaking on scratch edits reports a field renumbered, renamed, retyped or deleted; an enum value renamed; a field removed with its number and name reserved. Passes an added field.
The compatibility step's script, taken from ci.yml and run with a stub gh compatible: passes. Break without the label: fails. Break with the label but CHANGELOG.md unchanged: fails. Both: passes with a warning.
Formatting commit descriptors without source info byte-identical; grpc_tools.protoc output (_pb2.py, .pyi) identical
actionlint 1.7.12 with shellcheck clean
pre-commit run --all-files (ruff, buf lint, buf format, actionlint) all pass; the buf hooks build buf 1.75.0

🤖 Generated with Claude Code

Max-Bin and others added 2 commits December 3, 2026 17:55
One-line messages are written over several lines, and two comments that
followed a closing brace now follow the field they describe. Nothing else
changes: the descriptors without source info are byte-identical, and protoc
generates the same Python bindings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
VERSIONING.md asks that released field numbers and meanings be kept and that
retired fields be reserved, but nothing checked it: compatibility rested on
review. buf 1.75.0 now checks the schema in CI (job `protobuf`, part of the
required `ci` result):

- lint with buf's STANDARD rules, except four the released names cannot meet
  without renaming the package or every enum value (buf.yaml says which);
- formatting, with `buf format`;
- on pull requests, compatibility with the latest release tag reachable from
  the base, with the FILE rules: the release covers the generated Python and
  C++ bindings, so a renamed or removed field breaks callers even when its
  number is kept. A pull request that intends a break carries the
  `breaking-change` label and changes CHANGELOG.md; the labels are read when
  the job runs, so re-running it after labelling is enough.

buf is downloaded from its release and checked against the release's SHA-256,
as actionlint is. The pre-commit configuration gains buf's lint and format
hooks, and README.md and VERSIONING.md describe the checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Max-Bin

Max-Bin commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

@xtk8532704 Could you review this, and answer three questions from #27? Each is a small change either way. CI is green.

  1. Label for intended breaks. Is breaking-change the right name? It does not exist yet. Until it is created, any incompatible schema change fails the check.
  2. FILE or WIRE_JSON. The PR uses FILE, because the release covers the generated Python and C++ bindings. That also reports removing a field whose number and name are reserved. WIRE_JSON would let such a removal pass.
  3. Compare against main too? The check compares only with the latest release tag, since VERSIONING.md speaks of released fields. A break of a field added since v0.1.0 would therefore not be reported until the next release.

@Max-Bin
Max-Bin requested a review from xtk8532704 December 3, 2026 09:11

@xtk8532704 xtk8532704 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FILE rules and the v0.1.0 release baseline look right. I ran Buf 1.75.0 lint, format and the release comparison, compared the two compiled descriptors byte-for-byte, and confirmed a renumbered released field is rejected without the required label and changelog. One blocking behavior remains: the intended-break exception must not make the job pass when Buf never loaded the baseline.

Comment thread .github/workflows/ci.yml Outdated
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
release=$(git describe --tags --abbrev=0 --match 'v[0-9]*' "$BASE_SHA")
if buf breaking --against ".git#tag=${release},subdir=t4scene/proto"; then

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This treats any buf breaking failure as a confirmed schema incompatibility. I ran the exact step with the real Buf binary, the breaking-change label stub and a committed changelog edit. When the temporary directory was unavailable, Buf printed could not clone … and exited 1 without comparing against v0.1.0, but the step emitted an incompatibility warning and returned 0. Please fail if the baseline cannot be built/read or if the comparison fails without actual compatibility diagnostics. Only confirmed schema violations should take the label/changelog exception. Add a regression for a baseline-load failure with both exception prerequisites present.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2290bc9. Thanks for running it with the real binary.

With Buf 1.75.0, buf breaking exits 100 when it reports violations and 1 when it cannot load an input. Your could not clone case exits 1. It also exits 100 when the changed schema does not compile, because it annotates compile errors as COMPILE with the same status. Checking the exit code alone would have left that second hole open.

The step now runs .github/scripts/check_breaking.py:

  1. It builds the release schema with buf build first. If that fails, the job fails, and the label is not consulted.
  2. It compares against the built image. Exit 0 passes. Any exit other than 100 fails.
  3. On exit 100, every annotation must name a rule from buf config ls-breaking-rules --configured-only. Otherwise the job fails.
  4. Only then are the label and CHANGELOG.md consulted. The violations are reported as annotations on the proto file.

.github/scripts/test_check_breaking.py runs the real Buf against a scratch repository. It commits a one-message schema, tags it v0.1.0, and uses a stub gh for the labels. It has five cases:

  • compatible;
  • a break without the label, or without a changelog change;
  • a labelled and recorded break;
  • a release that cannot be built (TMPDIR missing), with the label and the changelog both present;
  • a changed schema that does not compile, with both present.

The last two fail against the previous acceptance logic and pass now. CI runs the tests before the check (Ran 5 tests … OK, then Compatible with v0.1.0.). VERSIONING.md now states that the label never excuses a comparison that could not run.

The compatibility step treated any `buf breaking` failure as an
incompatibility, so a release schema that could not be cloned or built
passed once the pull request was labelled and the changelog changed.

Build the release schema first and fail when that fails. Accept the label
only when buf exits 100 and every annotation names a configured breaking
rule: buf reports compile errors in the changed schema with the same
status. The check moves to .github/scripts/check_breaking.py, with
regression tests that run the real buf against a scratch repository.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Max-Bin
Max-Bin requested a review from xtk8532704 December 3, 2026 09:40

@xtk8532704 xtk8532704 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked at 2290bc91 with the pinned Buf 1.75.0. Lint, formatting, the comparison against v0.1.0, and all five compatibility-script regression tests passed. The two failure cases I was concerned about now fail even with the label and changelog edit, while an actual compatibility violation still goes through the intended exception.

@xtk8532704
xtk8532704 merged commit 736adb5 into main Dec 3, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check the Protobuf schema with buf: lint, formatting and compatibility with the latest release

2 participants