Skip to content

Commit 21e33cc

Browse files
committed
reflect/protoregistry: restore conflicting file names check
There are inconsistencies between implementations on what happens when a single program contains generated code for multiple files with the same source path. At least one canonical implementation (C++) will fail at link time. Others print warnings. Some silently resolve the registry conflict in favor of one file or the other. The protobuf maintainers agree, however, that the desired behavior is for this condition to be an error. Updates golang/protobuf#1122 Change-Id: I716708f16ef90210bdfceb0888691e47783df172 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/322729 Trust: Damien Neil <[email protected]> Reviewed-by: Joe Tsai <[email protected]>
1 parent 426f20b commit 21e33cc

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

reflect/protoregistry/registry.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ func (r *Files) RegisterFile(file protoreflect.FileDescriptor) error {
121121
r.filesByPath = make(map[string][]protoreflect.FileDescriptor)
122122
}
123123
path := file.Path()
124-
if len(r.filesByPath[path]) > 0 {
124+
if prev := r.filesByPath[path]; len(prev) > 0 {
125125
r.checkGenProtoConflict(path)
126+
err := errors.New("file %q is already registered", file.Path())
127+
err = amendErrorWithCaller(err, prev[0], file)
128+
if !(r == GlobalFiles && ignoreConflict(file, err)) {
129+
return err
130+
}
126131
}
127132

128133
for name := file.Package(); name != ""; name = name.Parent() {

reflect/protoregistry/registry_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestFiles(t *testing.T) {
6969
files: []testFile{
7070
{inFile: mustMakeFile(`syntax:"proto2" name:"test1.proto" package:"foo.bar"`)},
7171
{inFile: mustMakeFile(`syntax:"proto2" name:"foo/bar/test.proto" package:"my.test"`)},
72-
{inFile: mustMakeFile(`syntax:"proto2" name:"foo/bar/test.proto" package:"foo.bar.baz"`)},
72+
{inFile: mustMakeFile(`syntax:"proto2" name:"foo/bar/test.proto" package:"foo.bar.baz"`), wantErr: "already registered"},
7373
{inFile: mustMakeFile(`syntax:"proto2" name:"test2.proto" package:"my.test.package"`)},
7474
{inFile: mustMakeFile(`syntax:"proto2" name:"weird" package:"foo.bar"`)},
7575
{inFile: mustMakeFile(`syntax:"proto2" name:"foo/bar/baz/../test.proto" package:"my.test"`)},
@@ -112,8 +112,10 @@ func TestFiles(t *testing.T) {
112112
{"weird", "foo.bar"},
113113
},
114114
}, {
115-
inPath: "foo/bar/test.proto",
116-
wantErr: `multiple files named "foo/bar/test.proto"`,
115+
inPath: "foo/bar/test.proto",
116+
wantFiles: []file{
117+
{"foo/bar/test.proto", "my.test"},
118+
},
117119
}},
118120
}, {
119121
// Test when new enum conflicts with existing package.

0 commit comments

Comments
 (0)