3

I have two services: Story and Tag. The files are structured in this way:

Story
|-- StoryService
|   `-- proto
|       `-- storyservice.proto
`-- TagService
    `-- proto
        `-- tagservice.proto

Each one of .proto files needs to use messages from the other one. How can I import them into each other? I tried import "StoryService/proto/storyservice.proto" but it didn't work.

afenster
  • 3,468
  • 19
  • 26
Ali Alhajji
  • 174
  • 2
  • 12

2 Answers2

0

If you use protoc, all search paths for import directives should be relative to one of the folders listed in -I (--proto_path) parameter. E.g. if you run protoc -I/path/to/Story --go_out=out StoryService/proto/storyservice.proto, it will be able to import TagService/proto/tagservice.proto (since its path is relative to the Story folder which is listed in -I).

Having said that, you don't really need to make each of your proto files import another one. Recursive proto file import won't work with the error message similar to this one:

a.proto: File recursively imports itself: a.proto -> b.proto -> a.proto
afenster
  • 3,468
  • 19
  • 26
  • What about using a message defined in the other file? For example, in storyservice.proto I want to use the Tag message defined in tagservice.proto. The IDE complains that Tag is not defined. – Ali Alhajji Mar 10 '19 at 07:10
  • Should be no problem with that if you import `tagservice.proto` from `storyservice.proto` and use a message defined in `tagservice.proto` in `storyservice.proto`. If you just use `protoc` to compile your protos (`protoc -I/path/to/Story --go_out out StoryService/proto/storyservice.proto`) to a source code, without an IDE, does it work, or what error message does it print? – afenster Mar 10 '19 at 08:15
  • Now it's telling me that I imported the proto file, but it was not used. But when I try to use a message from that file, it says "Story" not defined. PS: I'm importing storyservice.proto into tagservice.proto. – Ali Alhajji Mar 10 '19 at 13:32
0

You cannot import two proto files into each other. There are two solutions for your scenario. 1. Having a third proto file that contains all the shared definitions. 2. Making all shared definitions into one of the two protos you have, so one can import the other one.

lyuxuan
  • 56
  • 2