go build_error ai_generated true

panic: proto: field number 3 in message pb.Order is already used by "id"

ID: go/proto-duplicate-field-number

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-03-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf 1.31+ active — — —

Root Cause

Two fields in the same proto message were assigned the same field number. This is a proto schema error caught at init time by the generated code.

generic

中文

同一个 proto 消息中的两个字段被分配了相同的字段编号。这是 proto 模式错误,在初始化时被生成的代码捕获。

Workarounds

  1. 97% success
    Assign unique field numbers in the .proto file and regenerate:
    message Order {
      int64 id = 1;
      string sku = 2;
      int32 qty = 3;  // not another 3
    }
    // then: protoc --go_out=. order.proto
  2. 92% success
    Run buf lint in CI to catch duplicate field numbers before build:
    buf lint && buf breaking --against '.git#branch=main'

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The panic is triggered by the proto field numbers in the generated descriptors, not by Go field names; renaming Go fields does not change the descriptor.

  2. 95% fail

    The panic occurs during package init before main runs; recover cannot intercept init-time panics.