One of the core promises of Protobuf is forwards and backwards compatibility. However, making sure that your Protobuf schema doesn't introduce breaking changes isn't automatic - there are rules you need to follow to ensure your schema remains compatible for the lifetime of it's evolution.
Buf provides a breaking change detector through
buf breaking, which runs a set of
breaking rules across the current version of your entire Protobuf
schema in comparison to a past version of your Protobuf schema. The rules are
selectable, and split up into logical categories depending on the nature of breaking
changes you care about:
FILE: Generated source code breaking changes on a per-file basis, that is changes that would break the generated stubs where definitions cannot be moved across files. This makes sure that for languages such as C++ and Python where header files are included, your source code will never break for a given Protobuf change. This category also verifies wire and JSON compatibility.
PACKAGE: Generated source code breaking changes on a per-package basis, that is changes that would break the generated stubs, but only accounting for package-level changes. This is useful for languages such as Java (with
option java_multiple_files = true;set) or Golang where it is fine to move Protobuf types across files, as long as they stay within the same Protobuf package. This category also verifies wire and JSON compatibility.
WIRE: Wire breaking changes, that is changes that would break wire compatibility, including checks to make sure you reserve deleted types of which re-use in the future could cause wire incompatibilities.
WIRE_JSON: Wire breaking changes and JSON breaking changes, that is changes that would break either wire compatibility or JSON compatibility. This mostly extends
WIREto include field and enum value names.
Buf can compare your current Protobuf schema against different Input types:
- Stored Images or FileDescriptorSets from protoc, representing your past schema. These Images can be in either local or remote (http/https) locations.
- Branches or tags of your git repository, either local or remote. Buf can clone the head of the branch, and
.protofiles in-memory on that branch to compare against, on the fly.
- Tar or zip archives, either local or remote, containing your past
.protofiles. Buf then compiles these files in-memory on the fly and compares against them.
Other features of
buf breaking include:
Automatic file discovery. By default, Buf will build your
.protofiles by walking your file tree and building them per your build configuration. This means you no longer need to manually specify your
--proto_pathsand files every time you run the tool. However, Buf does allow manual file specification through command-line flags if you want no file discovery to occur, for example in Bazel setups.
File references. Buf's breaking change detector will produce file references to the location of the breaking change, including if a reference moves across files between your past and current file versions. For example, if a field changes type, Buf will produce a reference to the field. If a field is deleted, Buf will produce a reference to the location of the message in the current file.
Selectable error output. By default, Buf outputs
file:line:col:messageinformation for every breaking change, with the file path carefully outputted to match the input location, including if absolute paths are used. JSON output that includes the end line and end column of the breaking change is also available, and JUnit output is coming soon.
Speed. Buf's internal Protobuf compiler utilizes all available cores to compile your Protobuf schema, while still maintaining deterministic output. As an unscientific example, Buf can compile all 2,311
.protofiles in googleapis in about 0.8s on a four-core machine, as opposed to about 4.3s for
protocon the same machine. While both are very fast, this allows for instantaneous feedback. A typical breaking check operation for 2,300
.protofiles that clones the head of a local git repository branch, compiles all the
.protofiles on that branch, compiles all the current local files, and compares the two results for breaking changes, takes about 2.2s.