4

I have two .proto files, which has two packages that have mutual dependency.

a.proto

syntax = "proto3";
import "b.proto";

package a;

message cert {
    string filename = 1;
    uint32 length = 2;
}

enum state {
    UP = 1;
    DOWN = 2;
}

message events {
    repeated b.event curevent = 1;
    uint32 val = 2;
}

b.proto

syntax = "proto3";
import "a.proto";

package b;

message event {
     a.cert certificate = 1;
     a.state curstate = 2;
}

When I try to generate cpp files, following error is seen

# protoc -I. --cpp_out=. b.proto b.proto: File recursively imports itself: b.proto -> a.proto -> b.proto

How can this be achieved ?

Note : protoc version used is libprotoc 3.3.0

  • If it was a mutual dependency then moving the shared part to c.proto would work - however, looking at your scenario, I don't think that can be resolved short of moving everything to one file. – Marc Gravell Jul 28 '17 at 17:26

1 Answers1

1

proto compiler won't let you include circular dependencies. You will have to organize your code such that there aren't any recursive imports. One organization of your sample code above could be:

a.proto

syntax = "proto3";

package a;

message cert {
    string filename = 1;
    uint32 length = 2;
}

enum state {
    UNDEFINED = 0;
    UP = 1;
    DOWN = 2;
}

b.proto

syntax = "proto3";
import "a.proto";

package b;

message event {
    a.cert certificate = 1;
    a.state curstate = 2;
}

message events {
    repeated event curevent = 1;
    uint32 val = 2;
}

Your events type doesn't use anything from a.proto, and also uses event type from b.proto. It makes sense to move it to b.proto.

Ulas Keles
  • 1,681
  • 16
  • 20
  • But when you compile `a.proto`, you won't have `b.proto` included. So if you compile `b.proto` you will have `a.proto`, but once you got `c.proto`, `d.proto` and `e.proto`, you'll be running into problems again. – Robert de W Dec 18 '18 at 21:51