Protobuf Editions Are a Number in the Descriptor (and the Parser Dies on the Line That Sets It)

Editions is the compiler's way out of the syntax line. Instead of a syntax declaration at the top of the file, you write edition = "2023";, and the compiler reads the edition as a set of defaults for everything in the file, which field is present, which repeated field is packed, what the JSON keys are. I fed that line to the parser that runs every tab of the converter, protobufjs 7.4.0, and measured what happened, because it is one word the parser's grammar may have never met.

It dies on the first line. The exact error is illegal token 'edition', the same shape of error a bare group keyword produces. With a capital E, Edition, it is a different word to the lexer and the same death. Moving the line below a syntax declaration does not help, a file that carries both lines still dies on the edition one. The parser has never learned the word, and it has no fallback for it, no warning, no did-you-mean.

The rest of the file is fine, and that is the part that matters. Comment out the edition line, put syntax = "proto3"; back, and the same message body parses clean. I encoded it, a string, an int64 past 2^53, an enum, a repeated string, a map, and the decode back matched field for field, the 13-digit ID included as an exact decimal string. The edition is a declaration about the file. Nothing on the wire carries it, and the bytes do not know which edition the file came from.

The declaration is not the only editions vocabulary the parser is missing. Editions lets you mark a field with a visibility keyword, and private string name = 1; comes back as illegal token 'name', the parser reads the word private as a type and then meets the real name. The feature defaults block, the place edition-level overrides live, comes back as illegal token 'feature_defaults'. So an editions file is not a one-line fix. It is a pass over the file, comment the declaration, drop the visibility keywords, delete the feature block, and hope the defaults you were relying on match the syntax you pasted the file under.

protoc 25.3, the compiler I tested against, treats editions as experimental. With no flags it refuses the file: This file uses editions, but --experimental_editions has not been enabled. This syntax is experimental and should be avoided. With the flag it compiles, and the descriptor is where the edition lands. I diffed the two descriptor sets for the identical message and the ten-byte gap breaks down exactly: five for the longer filename, two for the syntax label, which reads editions instead of proto3, and three for one new field, edition = 1000. The descriptor's own enum sits 2023 at 1000, with proto2 at 998 and proto3 at 999 under it. The field list is untouched, same names, same numbers, same json names.

So editions is a number in the descriptor and a set of defaults, and the wire does not read it. That is the same shape as the field options post, a label that changes meaning without changing bytes, except here the parser does not even store the label, it refuses the line that sets it. If your file has moved to editions and you still need the JavaScript toolchain, the edit is mechanical, comment the line, strip the keywords, put a syntax back, and then run one real message through the round trip, because the edition's defaults are exactly the part that can change what your bytes mean without changing a single byte. The wire format all of this rides on is in the wire format post, the four JSON rules the tool applies are in the JSON mapping post, and the file goes into proto.datajutsu.com after the one-line edit, on every tab.

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