jhump/protoreflect

panic: invalid type: got *dynamicpb.Message when GetExtension

Adol1111 opened this issue · 6 comments

When getting the Extension of the dependency file, it will report panic: invalid type: got *dynamicpb.Message. But if you read this file directly, there will be no error.

Additionally, this error only occurs in versions 1.15 and above, while versions below 1.15 work normally.

demo: https://github.com/Adol1111/protoreflect_demo

jhump commented

@Adol1111, thanks for reporting this and providing such a great repro case!

This was in fact supposed to be fixed in v1.15.1 (here). But I see the problem: in that patch, we only try to replace dynamic extensions in the files that are directly compiled, not their imports. So with your repro case, it only tries to replace the dynamic extensions in a.proto, but the problematic extensions are in the imported b.proto file. I think the fix will be fairly straight-forward.

jhump commented

FYI, a work-around until a release with a fix is created is to have your code provide all file names to the call to ParseFiles, including any indirectly imported files.

So in your repro case, the following diff causes it to work as expected because we supply both a.proto and b.proto to the parser:

diff --git a/main.go b/main.go
index fc7d6de..3aa6765 100644
--- a/main.go
+++ b/main.go
@@ -15,7 +15,7 @@ import (
 )
 
 func main() {
-       pb, err := loadProto("a.proto", "./example", "./third_parts")
+       pb, err := loadProto()
        if err != nil {
                fmt.Println(err)
        }
@@ -40,12 +40,12 @@ func main() {
        fmt.Println("ok")
 }
 
-func loadProto(entryFile string, dirs ...string) (*desc.FileDescriptor, error) {
+func loadProto() (*desc.FileDescriptor, error) {
        parser := protoparse.Parser{
                IncludeSourceCodeInfo: true,
-               ImportPaths:           dirs,
+               ImportPaths:           []string{"./example", "./third_parts"},
        }
-       ds, err := parser.ParseFiles(entryFile)
+       ds, err := parser.ParseFiles("a.proto", "b.proto")
        if err != nil {
                return nil, err
        }

Thanks, but it's still quite troublesome. I will use the old version first for now, and wait for your patch.

jhump commented

A patch is available in 4f8401b.

I am still in the process of trying to introduce a v2 of this repo, and am hoping that there will only need to be one more (final) v1 release. So I've put this patch on the v1 branch instead of on the default branch. (Hopefully soon, the default branch will become what's on the v2 branch.)

You could use this patch if you want via go get github.com/jhump/protoreflect@4f8401b.

jhump commented

I ended up putting the patch commit on master and making another v1 release (v1.15.2). This is fixed in that newer release.

I'm very sorry for the late feedback. I updated the version last week. It seems to be fixed already. Thank you very much.