从不同的文件导入原始

发布于 2025-02-11 18:46:18 字数 495 浏览 0 评论 0原文

我将所有原始文件都保存在一个文件夹中。概述:

protos
|
|__auth
|
|__some_other

内部auth目录I有auth_service.proto user.proto

等。

syntax="proto3";

package auth;

import "auth/user.proto"; // Import "auth/user.proto" was not found or had errors.

message SomeMessage {
  User user = 1; // user is not defined
}

syntax="proto3";

package auth;

message User{}

I maintain all my proto files in one folder. An overview:

protos
|
|__auth
|
|__some_other

Inside auth directory I have auth_service.proto user.proto etc.

But I am having issues with importing definitions form user.proto

In auth_service.proto:

syntax="proto3";

package auth;

import "auth/user.proto"; // Import "auth/user.proto" was not found or had errors.

message SomeMessage {
  User user = 1; // user is not defined
}

user.proto has the same package name:

syntax="proto3";

package auth;

message User{}

Protoc version: libprotoc : 3.19.4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2025-02-18 18:46:18

这可能很棘手。

考虑一下它的一种方法是,您需要确定导入路径的根,然后原始导入是相对于它的。

假设$ {PWD}Protos的父。

示例#1:

如果您使用$ {pwd}作为proto_path

protoc \
--proto_path=${PWD} \
--go_out=${PWD} \
  ${PWD}/protos/auth/auth_service.proto \
  ${PWD}/protos/auth/user.proto

导入应为导入“ protos/auth/auth/user.proto”;

示例#2:

如果您使用Protos作为proto_path

protoc \
--proto_path=${PWD}/protos \
--go_out=${PWD} \
  ${PWD}/protos/auth/auth_service.proto \
  ${PWD}/protos/auth/user.proto

导入应为导入“ auth/user.proto”;

注释在所有情况下,proto文件参考必须由(之一)proto_path之一

settings.json

"protoc": {
    "path": "${workspaceRoot}/protoc-21.2-linux-x86_64/bin/protoc",
    "compile_on_save": true,
    "options": [
        "--proto_path=${workspaceRoot}/protoc-21.2-linux-x86_64/include",
        "--proto_path=${workspaceRoot}/protos",
        "--go_out=${workspaceRoot}"
    ]
}

This can be tricky.

One way to think about it is that you need to determine the root of the import path and then proto imports are relative to it.

Assuming ${PWD} is the parent of protos.

Example #1:

If you use ${PWD} as the proto_path:

protoc \
--proto_path=${PWD} \
--go_out=${PWD} \
  ${PWD}/protos/auth/auth_service.proto \
  ${PWD}/protos/auth/user.proto

Then the import should be import "protos/auth/user.proto";

Example #2:

If you use protos as the proto_path:

protoc \
--proto_path=${PWD}/protos \
--go_out=${PWD} \
  ${PWD}/protos/auth/auth_service.proto \
  ${PWD}/protos/auth/user.proto

Then the import should be import "auth/user.proto";.

NOTE In all cases, the proto file references must be prefixed by (one of) a proto_path.

settings.json:

"protoc": {
    "path": "${workspaceRoot}/protoc-21.2-linux-x86_64/bin/protoc",
    "compile_on_save": true,
    "options": [
        "--proto_path=${workspaceRoot}/protoc-21.2-linux-x86_64/include",
        "--proto_path=${workspaceRoot}/protos",
        "--go_out=${workspaceRoot}"
    ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文