reflect/protoregistry: panic upon registration conflicts

In this CL, we switch the behavior of registration conflicts
from simply logging a warning to panicking.
This is a change that we have warned about ever since
the release of v1.20.0 in 2020-03-02.

For users that have not fixed their dependencies or are unable to,
they may set the GOLANG_PROTOBUF_REGISTRATION_CONFLICT environment
to "warn" in order to obtain the prior behavior of simply logging
and ignoring subsequent conflicts (first wins).

Change-Id: Ied7937f0fd5cb5d875858411714ebeb5ce145574
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/235298
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2020-05-23 01:00:39 -07:00
parent 711224230b
commit aa45c46752

View File

@ -17,7 +17,7 @@ package protoregistry
import (
"fmt"
"log"
"os"
"strings"
"sync"
@ -31,12 +31,19 @@ import (
// 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 {
log.Printf(""+
"WARNING: %v\n"+
"A future release will panic on registration conflicts. See:\n"+
"https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict\n"+
"\n", err)
return true
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", "":
panic(fmt.Sprintf("%v\nSee %v\n", err, faq))
case "warn":
fmt.Fprintf(os.Stderr, "WARNING: %v\nSee %v\n\n", err, faq)
return true
case "ignore":
return true
default:
panic("invalid " + env + " value: " + os.Getenv(env))
}
}
var globalMutex sync.RWMutex