从父目录导入proto文件无法工作

huangapple go评论83阅读模式
英文:

proto importing from parent directory does not work

问题

无法弄清楚如何在proto中进行导入,非常感谢您的帮助。

文件结构:

  • proto
    • api
      • item.proto
    • item.proto

我试图将proto/item.proto导入到proto/api/item.proto中。

proto/api/item.proto

syntax = "proto2";
import "item.proto"

service ItemService {

  rpc ListItems(ListItemsRequest) returns (ListItemsResponse);
}

message ListItemsRequest {
  //Empty message
}

message ListItemsResponse {

  option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_schema) = {
    json_schema: {
      required:["items"]
    }
  };

  repeated .item.Item wallets = 1;
}

生成的命令行:

protoc \
    --proto_path=proto/ \
    --proto_path=third_party/grpc-gateway/third_party/googleapis \
    --proto_path=third_party/grpc-gateway \
    --include_source_info \
    --include_imports \
    --descriptor_set_out="$DESCRIPTOR_OUT" \
    --cpp_out="$CPP_OUT" \
    --grpc_out="$CPP_OUT" \
    --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` \
    proto/api/item.proto

报错信息:

api/item.proto:57:12: ".item.Item"未定义。
英文:

Can't figure out how to do imports in proto, some help much appreciated.

The file structure:

  • proto
    • api
      • item.proto
    • item.proto

I'm trying to import proto/item.proto into proto/api/item.proto

proto/api/item.proto:

syntax = "proto2";
import "item.proto"

service ItemService {

  rpc ListItems(ListItemsRequest) returns (ListItemsResponse);
}

message ListItemsRequest {
  //Empty message
}

message ListItemsResponse {

  option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_schema) = {
    json_schema: {
      required:["items"]
    }
  };

  repeated .item.Item wallets = 1;
}

Line for generating:

protoc \
    --proto_path=proto/ \
    --proto_path=third_party/grpc-gateway/third_party/googleapis \
    --proto_path=third_party/grpc-gateway \
    --include_source_info \
    --include_imports \
    --descriptor_set_out="$DESCRIPTOR_OUT" \
    --cpp_out="$CPP_OUT" \
    --grpc_out="$CPP_OUT" \
    --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` \
    proto/api/item.proto

Spits out error:

api/item.proto:57:12: ".item.Item" is not defined.

答案1

得分: 1

我假设你没有在proto/item.proto文件中添加包说明符。请参考这个链接这个链接。如果在导入的文件中指定了包,你应该不会出现错误:

syntax = "proto2";
package item;

message Item {}

你也可以不在导入的文件中添加包说明符,并且从消息名称中删除包(在proto/api/item.proto中):

message ListItemsResponse {
    repeated .Item wallets = 1;
}

另外,Protobuf文档建议始终相对于项目根目录导入文件。我假设这种方法有助于保持项目的可管理性,但我没有找到任何技术上的理由来推荐这样做。类似这样:

proto/api/item.proto:

syntax = "proto2";
package api.item;

import "proto/item.proto";

...

并使用以下命令生成:

protoc \
    --proto_path=. \
    ... \
    proto/api/item.proto
英文:

I assume that you did not add a package specifier to the proto/item.proto file. See this, and this. If you specify a package in the imported file, you should get no errors:

syntax = "proto2";
package item;

message Item {}

You could also not add a package specifier to the imported file, and drop the package from the message name (in proto/api/item.proto):

message ListItemsResponse {
    repeated .Item wallets = 1;
}

As a side note, the protobuf documentation recommends that you always import files relative to the project root. I assume that this method can help in keeping the project manageable, I could not find any technical reason to recommend this. Something like this:

proto/api/item.proto:

syntax = "proto2";
package api.item;

import "proto/item.proto";

...

and generating with

protoc \
    --proto_path=. \
    ... \
    proto/api/item.proto

huangapple
  • 本文由 发表于 2021年9月7日 17:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/69085334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定