.NET 5: Import was not found or had errors

1.7k views Asked by At

A simple scenario derived from the well-known introductory example (https://learn.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.1&tabs=visual-studio). In order to make the HelloRequest message reusable I extracted it in another file – request.proto. The file is in the same folder as the original greet.proto (see the picture at the end).

I need to make HelloRequest (in request.proto) known to the Greeter service (in greet.proto). All attempts to import it in greet.proto failed with the message

...
1>Protos/greet.proto(7,1): error : Import "request.proto" was not found or had errors.
...

Here are the greet.proto

syntax = "proto3";

option csharp_namespace = "GrpcService1";

package greeter;

import "request.proto";
//import "Protos/request.proto";
//import "GrpcService1/Protos/request.proto";
// all other combinations I may have thought off...

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// The request message containing the user's name.
/*message HelloRequest {
  string name = 1;
}*/

// The response message containing the greetings.
message HelloReply {
  string message = 1;
}

and request.proto:

syntax = "proto3";

package greeter;  // needles to say this was commented out in all my attempts

message HelloRequest {
  string name = 1;
}

Here's what the Solution Explorer shows: enter image description here

2

There are 2 answers

0
Alexander Christov On

As I expected answer is as simple as the question – this was just a lucky guess! – the import should look like

import "protos/request.proto";

Apparently protobuf compiler "thinks" it operates in project's root folder. That makes sense.

One more note – if the imported file defined a new package name this name should have prefixed the message name, like this:

request.proto:

package common;

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

and using it in greet.proto:

package greet;

import "protos/request.proto";

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (common.HelloRequest) returns (HelloReply);
}

Note the common in common.HelloRequest.

This simple observations lead to a more general question (and probably a more important one).

How to import protos defined in another project, say CustomTypes.csproj so as to be (re-)used in a number of gRPC protobuf IDLs?

I have no answer to this – too much Unix... :(

If somebody knows it, please post it here. I'd be glad to vote for it.

0
Svetlana On

For anyone looking the answer here - you can set ProtoRoot for your proto files in the csproj file like:

<Protobuf Include="Protos\base_service.proto" GrpcServices="Client" ProtoRoot="Protos/"/>