[protobuf] reflect/protoregistry: add compile-time opt-out for registration conflicts

98 views
Skip to first unread message

Joe Tsai (Gerrit)

unread,
Feb 23, 2021, 2:36:42 AM2/23/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Joe Tsai has uploaded this change for review.

View Change

reflect/protoregistry: add compile-time opt-out for registration conflicts

The next release of this module will panic on registration conflicts.
While we have been warning that this will become a fatal error for about
a year now, it's almost certainly the case that some users still have
ambiguous registrations.

In addition to the GOLANG_PROTOBUF_REGISTRATION_CONFLICT environment variable,
also define a linker-initialized variable that users can set.

Change-Id: I3325d841b9b6c692e2862100d81aebdcfda069fc
---
M reflect/protoregistry/registry.go
1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/reflect/protoregistry/registry.go b/reflect/protoregistry/registry.go
index c04d5f1..66dcbcd 100644
--- a/reflect/protoregistry/registry.go
+++ b/reflect/protoregistry/registry.go
@@ -27,14 +27,30 @@
"google.golang.org/protobuf/reflect/protoreflect"
)

+// conflictPolicy configures the policy for handling registration conflicts.
+//
+// It can be over-written at compile time with a linker-initialized variable:
+// go build -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
+//
+// It can be over-written at program execution with an environment variable:
+// GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn ./main
+//
+// Neither of the above are covered by the compatibility promise and
+// may be removed in a future release of this module.
+var conflictPolicy = "panic" // "panic" | "warn" | "ignore"
+
// ignoreConflict reports whether to ignore a registration conflict
// given the descriptor being registered and the error.
// It is a variable so that the behavior is easily overridden in another file.
var ignoreConflict = func(d protoreflect.Descriptor, err error) bool {
const env = "GOLANG_PROTOBUF_REGISTRATION_CONFLICT"
const faq = "https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict"
- switch os.Getenv(env) {
- case "panic", "":
+ policy := conflictPolicy
+ if v := os.Getenv(env); v != "" {
+ policy = v
+ }
+ switch policy {
+ case "panic":
panic(fmt.Sprintf("%v\nSee %v\n", err, faq))
case "warn":
fmt.Fprintf(os.Stderr, "WARNING: %v\nSee %v\n\n", err, faq)

To view, visit change 295349. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: I3325d841b9b6c692e2862100d81aebdcfda069fc
Gerrit-Change-Number: 295349
Gerrit-PatchSet: 1
Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
Gerrit-MessageType: newchange

Joe Tsai (Gerrit)

unread,
Feb 23, 2021, 2:36:57 AM2/23/21
to Joe Tsai, goph...@pubsubhelper.golang.org, Damien Neil, Joe Tsai, golang-co...@googlegroups.com

Attention is currently required from: Joe Tsai, Damien Neil.

Patch set 1:Trust +1

View Change

    To view, visit change 295349. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: protobuf
    Gerrit-Branch: master
    Gerrit-Change-Id: I3325d841b9b6c692e2862100d81aebdcfda069fc
    Gerrit-Change-Number: 295349
    Gerrit-PatchSet: 1
    Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Joe Tsai <thebroke...@gmail.com>
    Gerrit-Attention: Joe Tsai <joe...@digital-static.net>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    Gerrit-Comment-Date: Tue, 23 Feb 2021 07:36:53 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Damien Neil (Gerrit)

    unread,
    Feb 23, 2021, 12:08:07 PM2/23/21
    to Joe Tsai, goph...@pubsubhelper.golang.org, Joe Tsai, golang-co...@googlegroups.com

    Attention is currently required from: Joe Tsai.

    Patch set 1:Code-Review +2

    View Change

      To view, visit change 295349. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: protobuf
      Gerrit-Branch: master
      Gerrit-Change-Id: I3325d841b9b6c692e2862100d81aebdcfda069fc
      Gerrit-Change-Number: 295349
      Gerrit-PatchSet: 1
      Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Joe Tsai <thebroke...@gmail.com>
      Gerrit-Attention: Joe Tsai <joe...@digital-static.net>
      Gerrit-Comment-Date: Tue, 23 Feb 2021 17:08:03 +0000
      Reply all
      Reply to author
      Forward
      0 new messages