Error using import in .proto file

ImportProtocol Buffers

Import Problem Overview


I tried importing a proto file named test1.proto to another proto file named test2.proto using the import statement

import "com/test/test1.proto";

But i get the following error

com/test/test1.proto: File not found.
test2.proto: Import "com/test/test1.proto" was not found or had errors.

I tried in many ways to find the cause of the import error but couldn't. So could someone please tell me the correct way of doing proto file imports in case there is something wrong with the above statement??

Import Solutions


Solution 1 - Import

You have to use the --proto_path command-line flag (aka -I) to tell protoc where to look for .proto files. If you don't provide a path, by default it will only search the current directory. See the documentation (under "Generating Your Classes" at the end of the page), or type protoc --help.

Solution 2 - Import

Just to add, if you are building for .NET and/or you are Visual Studio, you can add an extra attribute to your proto file defination in the .csproj file to specify the Proto Root like so :

<Protobuf Include="Protos\ProtoFile.proto" GrpcServices="None" ProtoRoot="Protos\" /> 

This clears all import and related errors.

Solution 3 - Import

If you're using IntelliJ IDEA, go to Preferences -> Protobuf Support and add the path to your .proto file. This would resolve the error.

Solution 4 - Import

For people running Bazel based projects, add the required proto file in the deps field of the BUILD like this-

native.proto_library(
    name = "test_message"
    srcs = "test_message.proto"
    deps = "//path/to/proto/you_are/importing:test1"

test1 is target name of the test1.proto file just like test_message is the target name of test_message.proto file.

Solution 5 - Import

If you are using IntelliJ:

  1. Click on the Maven tab on the right side header.
  2. Click on Reload All Maven objects, it will be represented by a circular symbol.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAarish RameshView Question on Stackoverflow
Solution 1 - ImportKenton VardaView Answer on Stackoverflow
Solution 2 - ImportChipo HamayobeView Answer on Stackoverflow
Solution 3 - ImportAshlin KarkadaView Answer on Stackoverflow
Solution 4 - ImportPavan ManjunathView Answer on Stackoverflow
Solution 5 - ImportAdityaView Answer on Stackoverflow