Compile all proto files in a directory (including subdirectory proto files)

1,792 views
Skip to first unread message

Novice User

unread,
Aug 29, 2020, 12:54:59 AM8/29/20
to prot...@googlegroups.com
I've nested proto (proto3) files [ Level0.proto depends on Level1.proto which in turn depend on Level2.proto] with the below tree structure :



Level0.proto

syntax = "proto3";

package protobufs;
import "level1/Level1.proto";

message Level0Message {
  Level1Message level1message = 1;
}

Level1.proto

syntax = "proto3";

package protobufs.folderA;
import "level2/Level2.proto";

message Level1Message {
    Level2Message level2Message = 1;
}

Level2.proto

syntax = "proto3";

package protobufs.folderA.folderB;

message Level2Message {
  string innerMost = 1;
}

Compilation fails :


$ protoc -I=./ --python_out=. *.proto
level2/Level2.proto: File not found.
level1/Level1.proto:4:1: Import "level2/Level2.proto" was not found or had errors.
level1/Level1.proto:7:5: "Level2Message" is not defined.
Level0.proto:4:1: Import "level1/Level1.proto" was not found or had errors.
Level0.proto:7:3: "Level1Message" is not defined.

Nadav Samet

unread,
Aug 29, 2020, 12:59:37 AM8/29/20
to Novice User, Protocol Buffers
The imports in proto files are relative to the search path (what you provide by -I), not to the files. You either need to the Level2 directory under the protobuf directory, or change the import in Level1.proto to import "level1/level2/Level2.proto"

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/81E01306-D492-4B20-903C-E1943FCD86AA%40gmail.com.


--
-Nadav

Novice User

unread,
Aug 29, 2020, 1:07:22 AM8/29/20
to Nadav Samet, Protocol Buffers
I modified the Level1.proto , but still getting error.

Level1.proto :
syntax = "proto3";

package protobufs.level1;
import "level1/level2/Level2.proto";

message Level1Message {
Level2Message level2Message = 1;
}
$ protoc -I=./ --python_out=. *.proto
level1/Level1.proto:7:5: "Level2Message" is not defined.
Level0.proto:4:1: Import "level1/Level1.proto" was not found or had errors.
Level0.proto:7:3: "Level1Message" is not defined.

(Running it from directory which has Level0.proto)


On Aug 28, 2020, at 9:59 PM, Nadav Samet <thes...@gmail.com> wrote:

The imports in proto files are relative to the search path (what you provide by -I), not to the files. You either need to the Level2 directory under the protobuf directory, or change the import in Level1.proto to import "level1/level2/Level2.proto"

On Fri, Aug 28, 2020 at 9:54 PM Novice User <novice...@gmail.com> wrote:
I've nested proto (proto3) files [ Level0.proto depends on Level1.proto which in turn depend on Level2.proto] with the below tree structure :

<xT5gv.png>


--
-Nadav

Nadav Samet

unread,
Aug 29, 2020, 1:15:01 AM8/29/20
to Novice User, Protocol Buffers
See the error level1/Level1.proto:7:5: "Level2Message" is not defined? The package statement for level1 indicates protobufs.level1. For level2.proto the package is protobufs.folderA.folderB. When level1 references the message in level2 it needs to do so by its relative name or full name:

protobufs.folderA.folderB.Level2Message level2Message = 1;

Similar logic applies to how level0 references Level1Message.
--
-Nadav

Novice User

unread,
Aug 29, 2020, 2:31:17 AM8/29/20
to Nadav Samet, Protocol Buffers
Great! Thanks much
Reply all
Reply to author
Forward
0 new messages