Skip to content

Commit 06e5c08

Browse files
authored
Update desc/sourceinfo to not wrap descriptors (#622)
The `desc/sourceinfo` package previously wrapped descriptors, by returning an implementation of the various `protoreflect.Descriptor` interfaces that _embedded_ the original descriptor values. However, these various interfaces are all defined as "[do not implement](https://github.com/protocolbuffers/protobuf-go/blob/c33baa8f3a0d35fd5a39e43c22a50a050f707d34/reflect/protoreflect/type.go#L111)". So, to avoid future issues where the protobuf runtime may type-assert implementations to their own internal types (which would panic/fail with the concrete wrapper types in this package), we no longer wrap this way. This also happens to resolve #618 (since there's no more wrapping, an incorrect typed-nil wrapper can't escape).
1 parent 645b9dd commit 06e5c08

22 files changed

+879
-854
lines changed

desc/descriptor_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,22 @@ func TestFileDescriptorObjectGraph(t *testing.T) {
472472
},
473473
},
474474
}},
475-
"enums": {(*FileDescriptor).GetEnumTypes, nil},
476-
"services": {(*FileDescriptor).GetServices, nil},
475+
"enums": {(*FileDescriptor).GetEnumTypes, nil},
476+
"services": {(*FileDescriptor).GetServices, []descCase{
477+
{
478+
name: "testprotos.SomeService",
479+
references: map[string]childCases{
480+
"methods": {(*ServiceDescriptor).GetMethods, []descCase{
481+
{
482+
name: "testprotos.SomeService.SomeRPC",
483+
},
484+
{
485+
name: "testprotos.SomeService.SomeOtherRPC",
486+
},
487+
}},
488+
},
489+
},
490+
}},
477491
"extensions": {(*FileDescriptor).GetExtensions, []descCase{
478492
{
479493
name: "testprotos.xtm",

desc/imports.go

-36
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ var (
3434
// package or when the alternate path is only used from one file (so you don't
3535
// want the alternate path used when loading every other file), use an
3636
// ImportResolver instead.
37-
//
38-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
39-
// correct and that descriptors can be linked during package initialization. So
40-
// registering alternate paths is no longer useful or necessary.
4137
func RegisterImportPath(registerPath, importPath string) {
4238
if len(importPath) == 0 {
4339
panic("import path cannot be empty")
@@ -60,10 +56,6 @@ func RegisterImportPath(registerPath, importPath string) {
6056
// ResolveImport resolves the given import path. If it has been registered as an
6157
// alternate via RegisterImportPath, the registered path is returned. Otherwise,
6258
// the given import path is returned unchanged.
63-
//
64-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
65-
// correct and that descriptors can be linked during package initialization. So
66-
// registering alternate paths is no longer useful or necessary.
6759
func ResolveImport(importPath string) string {
6860
importPath = clean(importPath)
6961
globalImportPathMu.RLock()
@@ -244,76 +236,48 @@ func (r *ImportResolver) registerImportPathFrom(registerPath, importPath, source
244236
// LoadFileDescriptor is the same as the package function of the same name, but
245237
// any alternate paths configured in this resolver are used when linking the
246238
// given descriptor proto.
247-
//
248-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
249-
// correct and that descriptors can be linked during package initialization. So
250-
// registering alternate paths is no longer useful or necessary.
251239
func (r *ImportResolver) LoadFileDescriptor(filePath string) (*FileDescriptor, error) {
252240
return LoadFileDescriptor(filePath)
253241
}
254242

255243
// LoadMessageDescriptor is the same as the package function of the same name,
256244
// but any alternate paths configured in this resolver are used when linking
257245
// files for the returned descriptor.
258-
//
259-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
260-
// correct and that descriptors can be linked during package initialization. So
261-
// registering alternate paths is no longer useful or necessary.
262246
func (r *ImportResolver) LoadMessageDescriptor(msgName string) (*MessageDescriptor, error) {
263247
return LoadMessageDescriptor(msgName)
264248
}
265249

266250
// LoadMessageDescriptorForMessage is the same as the package function of the
267251
// same name, but any alternate paths configured in this resolver are used when
268252
// linking files for the returned descriptor.
269-
//
270-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
271-
// correct and that descriptors can be linked during package initialization. So
272-
// registering alternate paths is no longer useful or necessary.
273253
func (r *ImportResolver) LoadMessageDescriptorForMessage(msg proto.Message) (*MessageDescriptor, error) {
274254
return LoadMessageDescriptorForMessage(msg)
275255
}
276256

277257
// LoadMessageDescriptorForType is the same as the package function of the same
278258
// name, but any alternate paths configured in this resolver are used when
279259
// linking files for the returned descriptor.
280-
//
281-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
282-
// correct and that descriptors can be linked during package initialization. So
283-
// registering alternate paths is no longer useful or necessary.
284260
func (r *ImportResolver) LoadMessageDescriptorForType(msgType reflect.Type) (*MessageDescriptor, error) {
285261
return LoadMessageDescriptorForType(msgType)
286262
}
287263

288264
// LoadEnumDescriptorForEnum is the same as the package function of the same
289265
// name, but any alternate paths configured in this resolver are used when
290266
// linking files for the returned descriptor.
291-
//
292-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
293-
// correct and that descriptors can be linked during package initialization. So
294-
// registering alternate paths is no longer useful or necessary.
295267
func (r *ImportResolver) LoadEnumDescriptorForEnum(enum protoEnum) (*EnumDescriptor, error) {
296268
return LoadEnumDescriptorForEnum(enum)
297269
}
298270

299271
// LoadEnumDescriptorForType is the same as the package function of the same
300272
// name, but any alternate paths configured in this resolver are used when
301273
// linking files for the returned descriptor.
302-
//
303-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
304-
// correct and that descriptors can be linked during package initialization. So
305-
// registering alternate paths is no longer useful or necessary.
306274
func (r *ImportResolver) LoadEnumDescriptorForType(enumType reflect.Type) (*EnumDescriptor, error) {
307275
return LoadEnumDescriptorForType(enumType)
308276
}
309277

310278
// LoadFieldDescriptorForExtension is the same as the package function of the
311279
// same name, but any alternate paths configured in this resolver are used when
312280
// linking files for the returned descriptor.
313-
//
314-
// Deprecated: the new protobuf runtime (v1.4+) verifies that import paths are
315-
// correct and that descriptors can be linked during package initialization. So
316-
// registering alternate paths is no longer useful or necessary.
317281
func (r *ImportResolver) LoadFieldDescriptorForExtension(ext *proto.ExtensionDesc) (*FieldDescriptor, error) {
318282
return LoadFieldDescriptorForExtension(ext)
319283
}

desc/load.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package desc
22

33
import (
4+
"errors"
45
"fmt"
56
"reflect"
67
"sync"
@@ -33,7 +34,7 @@ var (
3334
// re-processed if the same file is fetched again later.
3435
func LoadFileDescriptor(file string) (*FileDescriptor, error) {
3536
d, err := sourceinfo.GlobalFiles.FindFileByPath(file)
36-
if err == protoregistry.NotFound {
37+
if errors.Is(err, protoregistry.NotFound) {
3738
// for backwards compatibility, see if this matches a known old
3839
// alias for the file (older versions of libraries that registered
3940
// the files using incorrect/non-canonical paths)
@@ -42,7 +43,7 @@ func LoadFileDescriptor(file string) (*FileDescriptor, error) {
4243
}
4344
}
4445
if err != nil {
45-
if err != protoregistry.NotFound {
46+
if !errors.Is(err, protoregistry.NotFound) {
4647
return nil, internal.ErrNoSuchFile(file)
4748
}
4849
return nil, err
@@ -64,7 +65,7 @@ func LoadFileDescriptor(file string) (*FileDescriptor, error) {
6465
func LoadMessageDescriptor(message string) (*MessageDescriptor, error) {
6566
mt, err := sourceinfo.GlobalTypes.FindMessageByName(protoreflect.FullName(message))
6667
if err != nil {
67-
if err == protoregistry.NotFound {
68+
if errors.Is(err, protoregistry.NotFound) {
6869
return nil, nil
6970
}
7071
return nil, err

desc/protoprint/testfiles/desc_test1-compact.proto

+7
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,10 @@ extend AnotherTestMessage {
107107
// Comment for xui
108108
optional uint64 xui = 103;
109109
}
110+
// Comment for SomeService
111+
service SomeService {
112+
// Comment for SomeRPC
113+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
114+
// Comment for SomeOtherRPC
115+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
116+
}

desc/protoprint/testfiles/desc_test1-custom-sort.proto

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ extend AnotherTestMessage {
1818
optional string xs = 101;
1919
}
2020

21+
// Comment for SomeService
22+
service SomeService {
23+
// Comment for SomeRPC
24+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
25+
26+
// Comment for SomeOtherRPC
27+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
28+
}
29+
2130
// Comment for TestMessage
2231
message TestMessage {
2332
// Comment for NestedEnum

desc/protoprint/testfiles/desc_test1-default.proto

+9
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,12 @@ extend AnotherTestMessage {
144144
// Comment for xui
145145
optional uint64 xui = 103;
146146
}
147+
148+
// Comment for SomeService
149+
service SomeService {
150+
// Comment for SomeRPC
151+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
152+
153+
// Comment for SomeOtherRPC
154+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
155+
}

desc/protoprint/testfiles/desc_test1-multiline-style-comments.proto

+9
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,12 @@ extend AnotherTestMessage {
144144
/* Comment for xui */
145145
optional uint64 xui = 103;
146146
}
147+
148+
/* Comment for SomeService */
149+
service SomeService {
150+
/* Comment for SomeRPC */
151+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
152+
153+
/* Comment for SomeOtherRPC */
154+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
155+
}

desc/protoprint/testfiles/desc_test1-no-trailing-comments.proto

+9
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,12 @@ extend AnotherTestMessage {
144144
// Comment for xui
145145
optional uint64 xui = 103;
146146
}
147+
148+
// Comment for SomeService
149+
service SomeService {
150+
// Comment for SomeRPC
151+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
152+
153+
// Comment for SomeOtherRPC
154+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
155+
}

desc/protoprint/testfiles/desc_test1-only-doc-comments.proto

+9
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,12 @@ extend AnotherTestMessage {
144144
// Comment for xui
145145
optional uint64 xui = 103;
146146
}
147+
148+
// Comment for SomeService
149+
service SomeService {
150+
// Comment for SomeRPC
151+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
152+
153+
// Comment for SomeOtherRPC
154+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
155+
}

desc/protoprint/testfiles/desc_test1-sorted-AND-multiline-style-comments.proto

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ message TestMessage {
127127
}
128128
}
129129

130+
/* Comment for SomeService */
131+
service SomeService {
132+
/* Comment for SomeOtherRPC */
133+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
134+
135+
/* Comment for SomeRPC */
136+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
137+
}
138+
130139
/* Comment for AnotherTestMessage extensions (2) */
131140
extend AnotherTestMessage {
132141
/* Comment for xtm */

desc/protoprint/testfiles/desc_test1-sorted.proto

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ message TestMessage {
127127
}
128128
}
129129

130+
// Comment for SomeService
131+
service SomeService {
132+
// Comment for SomeOtherRPC
133+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
134+
135+
// Comment for SomeRPC
136+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
137+
}
138+
130139
// Comment for AnotherTestMessage extensions (2)
131140
extend AnotherTestMessage {
132141
// Comment for xtm

desc/protoprint/testfiles/desc_test1-trailing-on-next-line.proto

+9
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,12 @@ extend AnotherTestMessage {
144144
// Comment for xui
145145
optional uint64 xui = 103;
146146
}
147+
148+
// Comment for SomeService
149+
service SomeService {
150+
// Comment for SomeRPC
151+
rpc SomeRPC ( TestMessage ) returns ( TestMessage );
152+
153+
// Comment for SomeOtherRPC
154+
rpc SomeOtherRPC ( AnotherTestMessage ) returns ( AnotherTestMessage );
155+
}

desc/sourceinfo/export_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sourceinfo
2+
3+
import "google.golang.org/protobuf/reflect/protoreflect"
4+
5+
// exported for tests
6+
7+
func CanUpgrade(d protoreflect.Descriptor) bool {
8+
return canUpgrade(d)
9+
}
10+
11+
func UpdateDescriptor[D protoreflect.Descriptor](d D) (D, error) {
12+
return updateDescriptor(d)
13+
}
14+
15+
func UpdateField(fld protoreflect.FieldDescriptor) (protoreflect.FieldDescriptor, error) {
16+
return updateField(fld)
17+
}

0 commit comments

Comments
 (0)