I have this shell script:
#!/usr/bin/env bash
dir_name="$(cd "$(dirname "${BASH_SOURCE}")" && PWD)"
protoc --go_out="$dir_name" --go-grpc_out="$dir_name" "${dir_name}/myservice.proto"
I get this strange error message:
alex@alex yoda % /Users/alex/codes/yoda/yoda.ores.int/src/proto/generate.sh
/Users/alex/codes/yoda/yoda.ores.int/src/proto
/Users/alex/codes/yoda/yoda.ores.int/src/proto/myservice.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
here is the error more legibly:
File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
the input file(s), output files, and generate script are all in the same folder. Can someone help me figure out what I am doing wrong?
The single input file currently looks like this:
// myservice.proto
syntax = "proto3";
package myservice;
// Define your gRPC service.
service MyService {
rpc SendMessage (MessageRequest) returns (MessageResponse);
}
// Define your request and response message types.
message MessageRequest {
string message_data = 1;
// Add more fields as needed
}
message MessageResponse {
string status = 1;
// Add more fields as needed
}
protocis challenging with respect to import paths.Because (!) you reference
myservice.protoprefixed with${dir_name}, you must include--proto_path=${dir_name}too:Your use of
$dir_nameis unclear to me; generally I'd use${PWD}:If
service.protois in the${PWD}, you can simplify this (but I prefer the more explicit version above) as:Once you remove the
${PWD}(or equivalent) frommyservice.proto, you no longer need--proto_path(in this case).However (!) ...
myservice.protois defined to be within apackage, specificallypackage myservice. It's curious that it's not enforced but the convention is to represent protobuf (!)packagehierarchies as folders (underproto_pathroots), i.e.:You can see this in Google's Well-Known Types usually distributed with
protocin theincludefolder:If pick any of these, e.g.
timestamp.proto, you'll see that its package isgoogle.protobufand it's represented in theincludefolder asgoogle/protobuf/timestamp.proto.Using this convention, your command would be:
And, lastly, Go is going to moan about being "unable to determine Go import path".
There are actually different ways to do this.
Commonly you'll see
--go_opt=paths=source_relativebut my preference is to use--go_opt=module=${MODULE}as follows:Assuming:
And:
Then update
myservice.prototo include the value (!) of:And:
Yielding: