@@ -179,11 +179,10 @@ func (p Parser) ParseFiles(filenames ...string) ([]*desc.FileDescriptor, error)
179
179
}
180
180
181
181
fds := make ([]protoreflect.FileDescriptor , len (results ))
182
+ alreadySeen := make (map [string ]struct {}, len (results ))
182
183
for i , res := range results {
183
- if linkRes , ok := res .(linker.Result ); ok {
184
- removeDynamicExtensions (linkRes .FileDescriptorProto ())
185
- }
186
- fds [i ] = results [i ]
184
+ removeDynamicExtensions (res , alreadySeen )
185
+ fds [i ] = res
187
186
}
188
187
return desc .WrapFiles (fds )
189
188
}
@@ -261,7 +260,7 @@ func (p Parser) ParseFilesButDoNotLink(filenames ...string) ([]*descriptorpb.Fil
261
260
if err != nil {
262
261
return nil , err
263
262
}
264
- removeDynamicExtensions (protos [i ])
263
+ removeDynamicExtensionsFromProto (protos [i ])
265
264
}
266
265
if p .IncludeSourceCodeInfo {
267
266
protos [i ].SourceCodeInfo = sourceinfo .GenerateSourceInfo (res .AST (), optsIndex )
@@ -597,7 +596,23 @@ func fixupFilenames(protos map[string]parser.Result) map[string]parser.Result {
597
596
return revisedProtos
598
597
}
599
598
600
- func removeDynamicExtensions (fd * descriptorpb.FileDescriptorProto ) {
599
+ func removeDynamicExtensions (fd protoreflect.FileDescriptor , alreadySeen map [string ]struct {}) {
600
+ if _ , ok := alreadySeen [fd .Path ()]; ok {
601
+ // already processed
602
+ return
603
+ }
604
+ alreadySeen [fd .Path ()] = struct {}{}
605
+ res , ok := fd .(linker.Result )
606
+ if ok {
607
+ removeDynamicExtensionsFromProto (res .FileDescriptorProto ())
608
+ }
609
+ // also remove extensions from dependencies
610
+ for i , length := 0 , fd .Imports ().Len (); i < length ; i ++ {
611
+ removeDynamicExtensions (fd .Imports ().Get (i ).FileDescriptor , alreadySeen )
612
+ }
613
+ }
614
+
615
+ func removeDynamicExtensionsFromProto (fd * descriptorpb.FileDescriptorProto ) {
601
616
// protocompile returns descriptors with dynamic extension fields for custom options.
602
617
// But protoparse only used known custom options and everything else defined in the
603
618
// sources would be stored as unrecognized fields. So to bridge the difference in
0 commit comments