Rahul Vedpathak
Rahul Vedpathak

Reputation: 1436

Protobuf Compilation error while importing messages from other files

I have 2 protobuf files files -

file1.proto

**package abcd.xyz**

message Msg1{
   uint32 id =1;
   string name = 2;
}

file2.proto

**package com.abcd.xyz**

import "abcd/xyz/Msg1.proto";

message Msg2{
   uint32 id =1;
   string name = 2;
   Msg1 msg = 3;
}

When I am compiling this, getting below error -

 [exec] com/abcd/xyz/Msg2.proto:5: "abcd.xyz.Msg1" is resolved to "com.abcd.xyz.Msg1", which is not defined. The innermost scope is searched first in name resolution. Consider using a leading '.'(i.e., ".abcd.xyz.Msg1") to start from the outermost scope.

Msg1.proto files is used in multiple places and I cant change its package.

Any idea what changes do I need to compile Msg2.proto file without changing the package name?

Upvotes: 0

Views: 2018

Answers (1)

Stf Kolev
Stf Kolev

Reputation: 730

Here is the example I was able to run and hopefully will help you fix yours. As I looked at your code I noticed that you imported the package, but did not specify Msg1's package location in the definition of the message Msg2. To make sure you understood what I told you, let me show you an example how your code will look after doing so:

Right now I have the current project directory:

testprotoc
 - test
    - file1.proto
 - xyz
    - file2.proto

Where test and xyz are inner packages of the testprotoc main package.

The file1.proto looks like the following:

syntax = "proto3";

package testprotoc.test;

message Msg1 {
   uint32 id =1;
   string name = 2;
}
 

So you specify the protocol buffer package and the syntax the protobuf is going to use. Then we have the file2.proto:

syntax = "proto3";

package testprotoc.xyz;

import "test/file1.proto";

message Msg2 {
   uint32 id =1;
   string name = 2;
   test.Msg1 msg = 3;
}

Where we have the same things, syntax, package and we have import. A relative import of the inner packages, not including the main one.

Make sure to notice that I specified the package of the message definition when using it. That is the point you were missing and that is the way to fix it!

Upvotes: 2

Related Questions