Can't get import "../<file.proto" to compile

3,779 views
Skip to first unread message

Steve Maher

unread,
Oct 3, 2013, 9:35:15 AM10/3/13
to prot...@googlegroups.com
Hi,

I've found posts on dealing with .proto files in different directories, using packages, proto_path and canonical names, but I still can't figure out how to compile a basic protobuf file layout with a.proto and sub/b.proto that includes a.proto.

Perhaps the sticking point is I want to use a "import ../<file.proto>" format import statement so the Eclipse protobuf editor can resolve <file.proto>.  There is no proto_path setting in Eclipse so I need to use relative file names.

File ./a.proto:

message A {
        required int32 a = 1;
}


File sub/b.proto:

import "../a.proto";
message B {
        required int32 b = 1;
        required A a = 2;
}

I have not found an invocation or protoc (2.5) that can compile this.  For example:

In directory ./:

$ protoc sub/b.proto --java_out=/tmp
../a.proto: File not found.
sub/b.proto: Import "../a.proto" was not found or had errors.
sub/b.proto:4:18: "A" is not defined.


$ protoc --proto_path=. sub/b.proto --java_out=/tmp
../a.proto: File not found.
sub/b.proto: Import "../a.proto" was not found or had errors.
sub/b.proto:4:18: "A" is not defined.


In directory ./sub:

$ protoc b.proto --java_out=/tmp
../a.proto: File not found.
b.proto: Import "../a.proto" was not found or had errors.
b.proto:4:18: "A" is not defined.

 
$ protoc --proto_path=. --proto_path=../ b.proto --java_out=/tmp
../a.proto: File not found.
b.proto: Import "../a.proto" was not found or had errors.
b.proto:4:18: "A" is not defined.


Any tips are greatly appreciated!

Steve 

Feng Xiao

unread,
Oct 3, 2013, 2:15:17 PM10/3/13
to Steve Maher, Protocol Buffers
For each .proto file there is a canonical name and this name is used in the import statement. The canonical name is a relative file path name ("." and ".." is not allowed) and you can choose which directory to relative to by using the --proto_path argument properly.
For example, suppose you have a .proto file /foo/bar/baz.proto.
It's canonical name can be "foo/bar/baz.proto" if you compile it as:
protoc --proto_path=/ /foo/bar/baz.proto ...
Or it can be "bar/baz.proto" if you use:
protoc --proto_path=/foo /foo/bar/baz.proto ...
Generated files will have the same structure as the canonical name of the .proto file. So for the first command, it will generate foo/bar/baz.pb.(h|cc) and for the second one it will generate bar/baz.pb.(h|cc).
The name used in import statement must be the canonical name. When you have multiple .protos in different directories you can specify multiple --proto_path.
For example:
protoc --proto_path=/foo --proto_path=/test /foo/bar/baz.proto /test/test.proto ...
The canonical name for baz.proto will be "bar/baz.proto" and the name for test.proto will be "test.proto". If you want to import baz.proto in test.proto, you must use "bar/baz.proto", not "/foo/bar/baz.proto" or "../foo/bar/baz.proto".

The answer to your question very simple: you just need to change 'import "../a.proto"' to 'import "a.proto"' and then the first command you use will work well. Note that the canonical name of b.proto will be "sub/b.proto" so if you want to import it in another .proto file you need to use this name. Or maybe you want the canonical name to be "b.proto", then you can compile the protos with the following command:
protoc --proto_path=. --proto_path=sub sub/b.proto --java_out=/tmp

--
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 post to this group, send email to prot...@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.

Steve Maher

unread,
Oct 3, 2013, 3:04:42 PM10/3/13
to Feng Xiao, Protocol Buffers
Thanks for the explanation.  Turns out there is a per-project import path setting in Eclipse that will let me do things the right way and also make the plugin happy.

Steve
Reply all
Reply to author
Forward
0 new messages