Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobufs import from another directory

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;   
}
like image 471
Aarish Ramesh Avatar asked Jan 16 '14 10:01

Aarish Ramesh


People also ask

Is protobuf backwards compatible?

Protocol buffers provide a language-neutral, platform-neutral, extensible mechanism for serializing structured data in a forward-compatible and backward-compatible way.

Why is protobuf backwards compatible?

The sender is backward compatible because it's creating output that can be consumed by earlier versions. So long as you're careful about when and how you change and remove fields, your protobuf will be forward and backward compatible.

Is protobuf a library?

Protocol Buffers is a library from Google. It provides efficient and language-independent ways to serialize the data. It supports serialization and deserialization from languages like Java, Python, Go, Dart, etc.

Does protobuf support inheritance?

Inheritance is not supported in protocol buffers.


2 Answers

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";
like image 92
Kenton Varda Avatar answered Sep 22 '22 08:09

Kenton Varda


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"
like image 42
Dale Brubaker Avatar answered Sep 20 '22 08:09

Dale Brubaker