Skip to content

fix(options): encode option message fields like protoc - #4

Merged
ZergsLaw merged 2 commits into
easyp-tech:mainfrom
yaroher:fix/option-presence-packing
Dec 3, 2026
Merged

ZergsLaw merged 2 commits into
easyp-tech:mainfrom
yaroher:fix/option-presence-packing

Conversation

@yaroher

@yaroher yaroher commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Fixes the root cause of easyp-tech/easyp#269.

protoc parses a message-typed option value into a message and serializes that, so field presence and packing rules apply to it. easyprotoc wrote every assigned value straight to the wire, which gave two differences from protoc:

  • a zero value in a field without presence stayed in the bytes ({on: false} on a proto3 bool on, features.field_presence = IMPLICIT in editions). protoc drops it; oneof members, proto3 optional, map entries and proto2 fields keep it, same as before.
  • repeated scalars were never packed. protoc packs them when the field is packed: the proto3 and editions default, [packed = true] in proto2, unless repeated_field_encoding = EXPANDED. That includes a repeated scalar extension used directly, like option (ints) = 1; option (ints) = 0;.

The meaning of the descriptor doesn't change, only its bytes, but protoc-gen-go and protoc-gen-es embed the raw descriptor, so the generated code differed from protoc's for the same source. In our repo that showed up as 505 changed files after an easyp upgrade.

The linker now records, per field, whether it is implicit and whether it is packed, resolving features from the file, the enclosing messages and the field. encodeScalarField uses that for both message fields and top-level extensions.

Testing: TestOptionValuesEncodeLikeProtoc covers proto3, proto2, editions 2023 and repeated extensions with nested messages and a map; the expected bytes come from protoc 36.2 on the same sources, and the test fails on main. go test -race ./... and golangci-lint pass, and bench/run.sh parity still reports "same as protoc" for wkt, compute, api-common and all of googleapis.

- drop zero values of fields without presence (proto3 implicit,
  editions field_presence = IMPLICIT) inside option message literals
- pack repeated scalars that are packed (proto3 and editions defaults,
  proto2 [packed = true]), including repeated extensions
- protoc serializes these values through a parsed message, so the
  descriptor bytes differ otherwise; plugins that embed the raw
  descriptor then generate different code than protoc
- an extension set inside an option message literal now carries its
  IsPacked into the encoder, so [(box) = {[n1.ext_packed]: [1, 2, 0]}]
  and (box).(n1.ext_packed) are written packed, as protoc 36.2 does
- fields and extensions whose type resolves to a message in another
  file no longer keep the Implicit and IsPacked computed while the
  type was unresolved; the field symbol also gets the resolved type
@yaroher

yaroher commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 887596a with fixes for both review points.

Nested extensions: encodeMessage in internal/options/evaluator.go never copied IsPacked from the extension symbol into the field info, so a repeated scalar set through a nested extension went out unpacked. It's copied now. A message-typed extension also gets IsPacked = false when it resolves in resolveExtension, since packing only applies to scalars.

Cross-file message fields: when a field's type is in another file, resolveMessage only learns it's a message after linking. The field symbol in the table still had the scalar defaults (Implicit, IsPacked), and the option encoder read those. The resolver now clears both on the field info and updates the symbol found through ByName, so the table and the descriptor agree.

Tests:

  • protoc/option_encoding_test.go: two new cases (proto3 and editions), compared byte for byte against protoc 36.2 output;
  • internal/linker/linker_test.go: TestCrossFileFieldEncodingFollowsTheResolvedType.

All packages pass with -race.

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.

2 participants