Patrick
Patrick

Reputation: 381

Using import in proto file with Visual Studio/Rider

I am getting a "File not found" error when using import in a .proto file. I am using Rider but have the same problem when using Visual Studio.

First proto File:

syntax = "proto3";

import "/fileToImport.proto";

service GreeterAPI {
  rpc SayHello (SayHelloRequest) returns (SayHelloResponse);
}

message SayHelloRequest {
  string name = 1;
}

message SayHelloResponse {
  string answer = 1;
}

Second proto file that i want to import:

syntax = "proto3";

message Foo {
  string bar = 1;
}

Both files are located next to each other in the project directory.

.csprjo File:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Google.Protobuf" Version="3.10.1" />
      <PackageReference Include="Grpc.Core" Version="2.25.0" />
      <PackageReference Include="Grpc.Tools" Version="2.25.0" />

      <Protobuf Include="**/*.proto" />
    </ItemGroup>

</Project>

If i build the project without the import line everything is fine. But with the import line i get "File not found"

I know i can use the --proto_path to tell protoc all the files. But i don't want to build an extra pre-build script or something like that. I want to use the build in support of the IDE.

Upvotes: 6

Views: 6955

Answers (1)

Alex.C.
Alex.C.

Reputation: 141

I had the same problem as you, the fix that worked for me was adding the containing folder of the .proto files to the import. Assuming both .proto file are in a folder "Protos", try changing

import "/fileToImport.proto"; to import "Protos/fileToImport.proto".

Also try changing in the .csproj file from

<Protobuf Include="**/*.proto" />

to

<ItemGroup> <Protobuf Include="Protos/includingFile.proto" Link="includingFile.proto"/> <Protobuf Include="Protos/fileToInclude.proto" Link="fileToInclude.proto"/> </ItemGroup>

Hope that helps

Upvotes: 13

Related Questions