A Protobuf Service Is a Name the Wire Never Reads (and the Diff Tab Compares the Spelling, Not the Type)

A service block is the part of a .proto file that looks the most like a real API, and it is the part the bytes know the least about. I wrote a small service with a couple of RPCs and ran it through both the JavaScript parser and the real protoc 25.3 binary, and the whole service is metadata that lives in the descriptor and nowhere else. The message that the RPC sends, the request, encodes to exactly the same bytes whether the service that declares it exists or not. I encoded the same request with the service in the file and with the file trimmed down to just the message, and both produced the identical bytes, the request string and nothing else. The service name, the method name, the request and response type, the streaming flags: none of it is in the stream. protoc's descriptor confirms it the other way. The file with the service is 91 bytes larger than the same file without, and that is the entire footprint of the API you just wrote, all of it sitting in the descriptor where a code generator reads it, not in the wire where a peer would see it.

Here is the shape of what the descriptor actually stores, because it is the shape the diff tool reads from. A method is a name, an input type, an output type, an options bag, and two streaming booleans, client and server. The input and output types are stored fully qualified, with the package in front, so a request type that you wrote as a bare name comes back as the dotted path. And that is the seed of the thing that bit me, because the parser keeps two versions of the type reference and they are not the same string. One is the reference exactly as you typed it in the file, the other is the resolved type that the name actually points to. In the common case they describe the same type, but they do not read the same, and which one a tool decides to compare is the whole question.

The options on an RPC behave like the options everywhere else in this parser, and the bracket form is the tell. Put an option inside the method body as a statement, option deprecated = true, and it is stored on the method, exactly where you would expect, the same shape the field and message option posts already cover. But the bracket form that protoc accepts on a field, rpc M (A) returns (B) [deprecated = true], dies in this parser with illegal token bracket, semicolon expected. Same error class as the container option finding, a file that compiles clean with protoc refuses to parse in a JavaScript tool on the option line. The streaming flags round out the method: a unary call stores neither flag, a server streaming call stores the one on the response, a bidirectional call stores both, and those flags are the one place the descriptor has to say something the wire format does not, because the frame layout for a streaming call is decided by the transport, not by the message.

This is where the diff tab stops being careful, and it is the part worth knowing before you trust its output on a service change. I ran the real comparison function, the one the tab calls, not a copy, over two edits. The first edited a request type from a bare name to its fully qualified spelling, and the type did not change at all, the name still resolves to the same message. The report came back with two breaking changes, request type changed and response type changed, one for each side of the call, because it compared the two reference strings, and the strings are different even though the types behind them are the same. I ran it again with a genuine retype, a different message for the request, and it reported one breaking change, the correct one, for the one side that actually moved. So the tool is right when the type moves and wrong when only the spelling moves, and it has no way to tell the two apart, because it never looks at the resolved type, only at the string you happened to type.

That is the opposite of how the same report treats a field, and the contrast is the point. At the field level the report compares parsed names, the camel case normalized form, so renaming a field's spelling in the file text reads as no difference at all, which is the behavior the diff report post documents. At the RPC level it compares the raw reference, so the same class of edit, a spelling change of a thing that did not actually move, reads as two breaking changes instead of zero. One door normalizes before it compares, the other does not, and the two sit in the same report a few lines apart. The practical consequence is the one that costs a person an afternoon: you clean up a .proto file, qualify a handful of type references so they survive a package move, run the diff to prove nothing broke, and the report tells you the request and response types of every method you touched have changed, and you start investigating types that are, in fact, the same.

That is where it lands. A service is a name in the descriptor and a promise in a transport, and the wire that carries its messages does not read either. The options sit in the method's options bag and the bracket form is a parser trap, the streaming flags are the one schema fact the transport honors, and the diff tab that is supposed to tell you what broke compared the spelling of a type reference, not the type, so a cosmetic edit reported as a breaking one. That is fixed now. The comparison resolves each reference to the message it actually points at before it decides anything changed, so qualifying a type, or dropping the qualifier, reads as no difference, and the type changed line only comes back when the reference points at a different message, which is the one case where old bytes really do decode into the wrong shape. The field-level behavior, the one that hides a spelling change, is in the diff report post, the option storage and the container bracket trap are in the options post, and the streaming frames it is describing are in thegRPC metadata post. The tool atproto.datajutsu.com has the diff tab that does the resolved comparison now, and if you paste a .proto with a service and qualify its type references, the report says no differences, which is what the wire sees. The breaking change shows up when the reference moves to a different message, one line per side of the call, and that is the one that breaks old clients.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe