Repository navigation
Description
Problem
The @typespec/protobuf emitter writes enum member names verbatim. Protobuf enum values use C++ scoping rules: values are siblings of their enum, so package-level enums cannot share a value name.
For example:
import "@typespec/protobuf";
using Protobuf;
@package({ name: "example.v1" })
namespace Example;
enum OrderState {
Unspecified: 0,
Pending: 1,
}
enum PaymentState {
Unspecified: 0,
Pending: 1,
}
model Order {
@field(1) state: OrderState;
@field(2) paymentState: PaymentState;
}With the current emitter, protoc rejects the generated file because Unspecified and Pending each occur twice in example.v1. Avoiding this requires repeating the enum name in every TypeSpec member.
Both AIP-126 and the Protobuf style guide recommend prefixing all package-level enum values with the enum name. The style guide explicitly recommends:
Prefix Every Value With the Enum Name (Converted to UPPER_SNAKE_CASE)
Proposal
Add an opt-in emitter option:
options:
"@typespec/protobuf":
enum-value-prefix: enum-nameWith this option, the example emits:
enum OrderState {
ORDER_STATE_UNSPECIFIED = 0;
ORDER_STATE_PENDING = 1;
}
enum PaymentState {
PAYMENT_STATE_UNSPECIFIED = 0;
PAYMENT_STATE_PENDING = 1;
}- Convert enum and member names to
UPPER_SNAKE_CASE, treating acronyms as words. - Preserve already-prefixed names and avoid adding a second prefix after case conversion.
- Report collisions in the resulting Protobuf package scope.
- Default to
none, preserving existing output. - Preserve enum type names, numbers, order, documentation, and aliases.
- Keep members and numbers explicit: do not synthesize an
Unspecifiedmember or infer numeric values.
This is a naming change only. Although binary numeric values remain unchanged, opting in changes generated identifiers and Protobuf JSON enum strings.
Related
microsoft/typespec#1857 tracks collision diagnostics more generally. This proposal adds an opt-in naming convention that prevents common enum value collisions without requiring verbose TypeSpec members.
Activity
github-actions commentedon Oct 2, 2026
🎯 Issue Triage
This requests an opt-in
@typespec/protobufemitter option that prefixes package-level enum values with their enum name.The proposal follows protobuf naming guidance, prevents common package-scope collisions, and preserves current output by default.
Category: Feature Request
Area:
emitter:protobuf🔍 Analysis
Protobuf enum values use package-level C++ scoping, so repeated member names across enums can make the emitted
.protoinvalid. The proposedenum-value-prefix: enum-nameoption addresses the common case through deterministicUPPER_SNAKE_CASEnaming without changing enum numbers, ordering, aliases, or type names.#1857 is related but not a duplicate: it tracks general collision diagnostics for protobuf package symbols and enum keys, whereas this issue requests an opt-in naming convention. #12103 is the associated implementation PR.
📝 Suggested next steps
feat(protobuf): add opt-in enum value prefixes (microsoft#12103)