【问题标题】:Using import in proto file with Visual Studio/Rider在 Visual Studio/Rider 中使用 proto 文件中的导入
【发布时间】:2020-03-11 11:46:54
【问题描述】:

在 .proto 文件中使用导入时出现“找不到文件”错误。 我正在使用 Rider,但在使用 Visual Studio 时遇到了同样的问题。

第一个 proto 文件:

syntax = "proto3";

import "/fileToImport.proto";

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

message SayHelloRequest {
  string name = 1;
}

message SayHelloResponse {
  string answer = 1;
}

我要导入的第二个 proto 文件:

syntax = "proto3";

message Foo {
  string bar = 1;
}

这两个文件在项目目录中彼此相邻。

.csprjo 文件:

<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>

如果我在没有导入行的情况下构建项目,一切都很好。但是使用导入行我得到“找不到文件”

我知道我可以使用--proto_path 告诉 protoc 所有文件。 但我不想构建额外的预构建脚本或类似的东西。 我想使用构建来支持 IDE。

【问题讨论】:

  • 看起来你指定了一个绝对路径,你可以试试相对路径:'import "fileToImport.proto"; '

标签: visual-studio protocol-buffers protobuf-net rider proto


【解决方案1】:

我遇到了和你一样的问题,对我有用的修复方法是将 .proto 文件的包含文件夹添加到导入中。假设两个 .proto 文件都在“Protos”文件夹中,请尝试更改

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

也尝试从

更改 .csproj 文件

&lt;Protobuf Include="**/*.proto" /&gt;

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

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-03
    • 2013-10-23
    • 2022-12-13
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多