19

While trying to compile a proto file named UserOptions.proto which has an import named Account.proto using the below command

protoc --proto_path=/home/project_new1/account --java_out=/home/project_new1/source   /home/project_new1/settings/Useroptions.proto

I get the following error :

/home/project_new1/settings/UserOpti‌​ons.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file.

PS: UserOptions.proto present in the directory /home/project_new1/settings
imports Account.proto present in the directory /home/project_new1/account

Proto descriptor files:

UserOptions.proto

package settings;

import "Account.proto";

option java_outer_classname = "UserOptionsVOProto";

Account.proto

package account;

option java_outer_classname = "AccountVOProto";

message Object
{
    optional string userId = 1;
    optional string service = 2;   
}
ch271828n
  • 15,854
  • 5
  • 53
  • 88
Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
  • It would be really helpful if you'd give a short but complete example - file layout, file contents, `protoc` command and result - which demonstrates the problem. – Jon Skeet Jan 16 '14 at 10:44
  • @JonSkeet: I also tried by specifying the package level details in the import statement in the file and mentioning only the upperlevel directory in the --protoc while compiling. It doesn't detect the import in this case. – Aarish Ramesh Jan 16 '14 at 11:01
  • Please edit this into the question - it's hard to read it in comments. And give the proto details so we can reproduce the problem, rather than just describing them. – Jon Skeet Jan 16 '14 at 12:04
  • @JonSkeet: sry ab that an nw i ve edited that into a question – Aarish Ramesh Jan 16 '14 at 12:18
  • Not really - you still haven't given us everything we need to reproduce this simply. – Jon Skeet Jan 16 '14 at 13:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45381/discussion-between-aarish-and-jon-skeet) – Aarish Ramesh Jan 16 '14 at 14:17
  • Okay, *now* that's probably enough information, although we really don't need that level of nesting etc to demonstrate the problem - in future, please do your best to make it *really easy* for people to reproduce the issue. I'll have a look when I get time tonight. – Jon Skeet Jan 16 '14 at 14:22
  • @JonSkeet: ya sure ll do t in future an okay tq:) – Aarish Ramesh Jan 16 '14 at 14:25

3 Answers3

31

As the error message states, the file you pass on the command line needs to be in one of the --proto_paths. In your case, you have only specified one --proto_path of:

/home/project_new1/

But the file you're passing is:

/home/project_new1/settings/UserOpti‌ons.proto

Notice that the file is not in the account subdirectory; it's in settings instead.

You have two options:

  • (Not recommended) Pass a second --proto_path argument to add .../settings to the path.
  • (Recommended) Use the root of your source tree as the proto path. E.g.:

    protoc --proto_path=/home/project_new1/ --java_out=/home/project_new1 /home/project_new1/settings/UserOpti‌ons.proto
    

In this case, to import Account.proto, you'll need to write:

import "acco‌​unt/Account.proto";
Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
  • 2
    Due to the `package` declaration in `MailAccount.proto`, you need to write it as `com.adventnet.vo.kernel.model.account.BaseDFSLookupProto`. `java_outer_classname` only applies to Java code, not `.proto` code. – Kenton Varda Jan 18 '14 at 01:51
  • Would be great if u take a look at this question too http://stackoverflow.com/questions/21227924/handling-null-values-in-protobuffers – Aarish Ramesh Jan 20 '14 at 07:00
  • It worked on changing the package declaration but i don't understand the need for changing it and how did it resolve the import ,so can you please explain?? and how should the package declaration be if i import multiple messages of the same proto?? – Aarish Ramesh Jan 21 '14 at 21:04
  • One final question, if i have a default package declaration in proto like the above one and i can't alter it, wouldn't t be possible for me to import at all??.. sorry for the flurry of questions, using imports in protobuffs r quite confusing thats y! – Aarish Ramesh Jan 21 '14 at 21:07
  • 1
    If you import a `.proto` which has a different `package` line, you must prefix any type name from that file with its package name. That's really all there is to it. – Kenton Varda Jan 21 '14 at 21:39
1

For those of us who want this really spelled out, here is an example where I have installed the protoc beta for gRPC using NuGet Packages Google.Protobuf, Grpc.Core and Grpc.Tools. My solution packages are one level above my Grpc directory (i.e. at BruTrader\packages). My .proto files are at BruTrader\Grpc\protos.

1. My .proto file:
syntax = "proto3";
import "timestamp.proto";
import "enums.proto";
package BruTrader.Grpc;

message DividendMessage {
    double amount = 1;
    google.protobuf.Timestamp dateUnix = 2;
}

2. my GenerateProto.bat file:
..\packages\Google.Protobuf.3.0.0-beta2\tools\protoc.exe -I..\Grpc\protos -I..\packages\Google.Protobuf.3.0.0-beta2\tools\google\protobuf --csharp_out=..\Grpc\Generated --grpc_out=..\Grpc\Generated --plugin=protoc-gen-grpc=..\packages\Grpc.Tools.0.13.0\tools\grpc_csharp_plugin.exe %1

3. my BuildProtos.bat
call GenerateProto ..\Grpc\protos\masterinstrument.proto
call GenerateProto .\protos\instrument.proto
etc.

4. BuildProtos.bat is executed as a Pre-build event on my Grpc project like this:
CD $(ProjectDir)
CALL "$(ProjectDir)BuildProtos.bat"
Dale Brubaker
  • 393
  • 5
  • 6
0

For my environment, Windows 10 Pro operating system and C++ programming languaje, I used the protoc-3.12.2-win64.zip that you can downloat it from here. You should open a Windows PowerShell inside the protoc-3.12.2-win64\bin path and then you must execute one of the next commands:

.\protoc.exe -I=C:\Users\UserName\Desktop\SRC --cpp_out=C:\Users\UserName\Desktop\DST C:\Users\UserName\Desktop\SRC\addressbook.proto

Or

.\protoc.exe --proto_path=C:\Users\UserName\Desktop\SRC --cpp_out=C:\Users\UserName\Desktop\DST C:\Users\UserName\Desktop\SRC\addressbook.proto

Note:

1- My source folder is in: C:\Users\UserName\Desktop\SRC

2- My destination folder is in: C:\Users\UserName\Desktop\DST

3- My .proto file is in: C:\Users\UserName\Desktop\SRC\addressbook.proto

Juan Carlos
  • 131
  • 1
  • 6