internal/filedesc: print warnings on registration conflicts

Rather than panicking at init time due to registration failures,
print a warning to stderr. Historically, the Go protobuf implementation
has not been strict about registration conflicts, which has led users
to unknowningly tolerating conflicts that may or may not expose
themselvs as a bug.

Registration conlicts now produce a log message:
<<<
2019/07/17 17:36:42 WARNING: proto: file "path/to/example.proto" is already registered
	previously from: "example.com/company/example_proto"
	currently from:  "example.com/user/example_proto"
A future release will panic on registration conflicts.

>>>

Change-Id: I2d583f04977c8bc8cb6bbd33d239277690bbec54
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/186181
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2019-07-15 00:04:27 -07:00
parent ad8dff3eec
commit 1ac7b53cc5
3 changed files with 17 additions and 5 deletions

View File

@ -6,6 +6,8 @@
package filedesc
import (
"log"
"google.golang.org/protobuf/internal/encoding/wire"
"google.golang.org/protobuf/internal/fieldnum"
"google.golang.org/protobuf/reflect/protoreflect"
@ -105,7 +107,7 @@ func (db DescBuilder) Build() (out struct {
out.Services = fd.allServices
if err := db.FileRegistry.Register(fd); err != nil {
panic(err)
CheckRegistryError(err)
}
return out
}
@ -150,3 +152,13 @@ func (db *DescBuilder) unmarshalCounts(b []byte, isFile bool) {
}
}
}
// CheckRegistryError handles registration errors.
// It is a variable so that its behavior can be replaced in another source file.
var CheckRegistryError = func(err error) {
log.Printf(""+
"WARNING: %v\n"+
"A future release will panic on registration conflicts.\n"+
// TODO: Add a URL pointing to documentation on how to resolve conflicts.
"\n", err)
}

View File

@ -155,7 +155,7 @@ func (tb TypeBuilder) Build() (out struct {
// Register enum types.
if err := tb.TypeRegistry.Register(&out.Enums[i]); err != nil {
panic(err)
fdesc.CheckRegistryError(err)
}
}
}
@ -183,7 +183,7 @@ func (tb TypeBuilder) Build() (out struct {
// Register message types.
if err := tb.TypeRegistry.Register(&out.Messages[i]); err != nil {
panic(err)
fdesc.CheckRegistryError(err)
}
}
@ -251,7 +251,7 @@ func (tb TypeBuilder) Build() (out struct {
// Register extension types.
if err := tb.TypeRegistry.Register(&out.Extensions[i]); err != nil {
panic(err)
fdesc.CheckRegistryError(err)
}
}
}

View File

@ -617,7 +617,7 @@ func amendErrorWithCaller(err error, prev, curr interface{}) error {
if prevPkg == "" || currPkg == "" || prevPkg == currPkg {
return err
}
return errors.New("%s; previously from %q, currently from %q", err, prevPkg, currPkg)
return errors.New("%s\n\tpreviously from: %q\n\tcurrently from: %q", err, prevPkg, currPkg)
}
func goPackage(v interface{}) string {