RV.
RV.

Reputation: 3018

Importing from parent folder to child in proto

I'm trying to import the proto defn from parent proto into the child proto with following folder structure.

|
|--parent.proto
|
|--sub
    |--child.proto

parent.proto

message Attribute {
  ---
}

child.proto

import "parent.proto"

    message Child {
      int32 attributeNo = 1;
      com.model.Attribute attribute = 2;

    }

Currently it's giving me error saying it couldn't found the parent.proto. Please suggest.

Upvotes: 3

Views: 3522

Answers (1)

Michał
Michał

Reputation: 2282

protoc looks for its imports in the directories specified using an -I flag. For example, you could add -I/home/user/my_awesome_proto_lib to the protoc command line args, and the compiler would look for your imports there.

From the help page of protoc, about the --proto_path:

  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.

So currently, when you're running protoc it will look for parent.proto in the sub directory. This is obviously not what you need. You could change your import to import "../parent.proto" which would go back to the root level and grab parent.proto from there. But the generally encouraged style in protobuf is not to use relative imports.

Instead, you could consider adding the root of your proto project as an -I/--proto_path flag.

Another option would be to compile your proto files from the root of the project. You could cd to the root directory of the project and protoc from there.

Upvotes: 3

Related Questions