Siddharth
Siddharth

Reputation: 5219

Importing proto files and compiling

I have two proto files in the following directories -

parsers/
   > flights/
       > flights_proto/
           > flights.proto
           > flights.pb.go
   > flightspostbooking
       > flights_postbooking_proto/
           > flights_postbooking.proto
           > flights_postbooking.pb.go

Following are the contents of the proto files -

flights.proto
=============

package "flights_proto";

message Flight {
   ...
}

flights_postbooking.proto
=========================

package "flights_postbooking_proto"
import "flights_proto/flights.proto"

message Cancel {
    Flight flight = 1;
    ...
}

I am unable to figure out how to compile the flights_postbooking.proto to generate the pb.go file.

I tried to do the following from the parsers directory.

protoc --proto_path=flightspostbooking/flights_postbooking_proto --proto_path=flights/flights_proto flightspostbooking/flights_postbooking_proto/flights_postbooking.proto --go_out=plugins:flights_postbooking_proto

But I get an error that flights_proto/flights.proto: File not found

Upvotes: 1

Views: 908

Answers (1)

Burak Serdar
Burak Serdar

Reputation: 51577

Run protoc from the parsers directory, and use the base directory for your proto files, which is .

protoc --proto_path=. --go_out=plugins=grpc:flightpostbooking/flights_postbooking_proto flightpostbooking/flights_postbooking_proto/flights_postbooking.proto

Since you have the parsers directory as your base, your import should be:

import "flights/flights_proto/flights.proto"

The key is to use import paths relative to your proto_path.

Upvotes: 1

Related Questions