You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Missing functions when using messages from imported files in rpc functions
Using messages in imported proto files in rpc functions occurs undefined errors because functions are not generated in pb files.
Introduction
I have been doing some experiments with protobuf and jumped into a problem.
I have multiple proto files to generate a service in one single proto file.
After auto-generation, functions/messages from imported proto files are not in pb.go file.
I have already extracted the problematic part of my source here: https://github.com/farkow/goprototest
It contains 2 different branches; master and working for you to check auto-generated files without any action.
Description
Let's say you will import a message from a different proto file into one other proto file to use it in an rpc call.
Imported proto file (image.proto - partially visible below)
When you use the message in rpc call like in rpc CreateImage(image.ImageCreateRequest), auto-generated pb files are not generating those functions.
It exists in the interface, but the function is not generated.
// MainServer is the server API for Main service.
type MainServer interface {
// image
CreateImage(context.Context, *ImageCreateRequest) (*ImageCreateResponse, error)
However, when those definitions are in main proto file - just like in working.proto -, it can generate the functions.
func (x *ImageCreateRequest) Reset() {
*x = ImageCreateRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_working_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
There are 2 branches - master and working - for you to easily see differences in files under /pkg/api/v1 without running any command.
Prerequisites
go
protoc
Versions
libprotoc 3.11.4
go1.14.2 linux/amd64
What I did?
I have imported image.proto and news.proto into main.proto
I have created endpoints from the functions inside imported proto files
I have generated pb.* files
Remove files inside /pkg/api/v1
Run make protoc-main for problematic version
Run make protoc-working for working version
make build
For imported version (protoc-main), you will have errors
For combined version (protoc-working), it will successfully build
What I expect to see?
When I import other proto files, I expect to use their functions / messages in service block.
What I see instead?
undefined messages for all messages which comes from imported files inside service block.
Errors
After you run make protoc-main, main.pb* files will be generated under pkg/api/v1.
Run make build and you will have following errors:
After you run make protoc-working, you will successfully build with make build.
The text was updated successfully, but these errors were encountered:
farkow
changed the title
Using "message"s in imported proto files in rpc functions occurs "undefined" errors because functions are not generated in "pb" files
Using "message"s of imported proto files in rpc functions occurs "undefined" errors because functions are not generated in "pb" files
Apr 27, 2020
Fundamentally, the problem is that a go_package option needs to be specified with the full Go package path. protoc-gen-go has a bug that unfortunately suggested a poor value for certain use-cases. https://go-review.googlesource.com/c/protobuf/+/232338 fixes protoc-gen-go to stop doing that.
For the codebase you linked to, you'll want to change the go_package option in the following files:
Missing functions when using messages from imported files in rpc functions
Using
message
s in imported proto files in rpc functions occursundefined
errors because functions are not generated inpb
files.Introduction
I have been doing some experiments with protobuf and jumped into a problem.
I have multiple proto files to generate a service in one single proto file.
After auto-generation, functions/messages from imported proto files are not in pb.go file.
I have already extracted the problematic part of my source here:
https://github.com/farkow/goprototest
It contains 2 different branches;
master
andworking
for you to check auto-generated files without any action.Description
Let's say you will import a message from a different proto file into one other proto file to use it in an rpc call.
Imported proto file (image.proto - partially visible below)
Main proto file (main.proto - partially visible below)
When you use the message in rpc call like in
rpc CreateImage(image.ImageCreateRequest)
, auto-generated pb files are not generating those functions.It exists in the interface, but the function is not generated.
However, when those definitions are in main proto file - just like in working.proto -, it can generate the functions.
There are 2 branches -
master
andworking
- for you to easily see differences in files under/pkg/api/v1
without running any command.Prerequisites
Versions
What I did?
make protoc-main
for problematic versionmake protoc-working
for working versionWhat I expect to see?
When I import other proto files, I expect to use their functions / messages in
service
block.What I see instead?
undefined
messages for allmessage
s which comes from imported files insideservice
block.Errors
After you run make protoc-main, main.pb* files will be generated under pkg/api/v1.
Run make build and you will have following errors:
After you run make protoc-working, you will successfully build with make build.
The text was updated successfully, but these errors were encountered: