我正在尝试加载来自arduino-cli存储库的 .proto 文件。更具体地说,我正在加载commands.proto,它依赖于同一目录中的一些其他 .proto 文件。
在提供给 proto-loader 的加载选项中,我指定了所有这些 .proto 依赖项的路径,但会弹出一个错误,指出未定义在这些依赖项之一中定义的 proto 消息。
这是我的代码:
const grpcLib = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const pathLib = require("path");
const RPC_PATH = pathLib.join(__dirname, "arduino-cli/rpc")
var PROTO_PATH = pathLib.join(RPC_PATH, "/commands/commands.proto");
const options = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs:
[
pathLib.join(RPC_PATH, "/commands/common.proto"),
pathLib.join(RPC_PATH, "/commands/board.proto"),
pathLib.join(RPC_PATH, "/commands/compile.proto"),
pathLib.join(RPC_PATH, "/commands/core.proto"),
pathLib.join(RPC_PATH, "/commands/upload.proto"),
pathLib.join(RPC_PATH, "/commands/libs.proto"),
]
}
const packageDefinition = protoLoader.loadSync(PROTO_PATH, options);
最后一行导致此错误:
/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/namespace.js:382
throw Error("no such type: " + path);
^
Error: no such type: BoardDetailsReq
at Service.lookupType (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/namespace.js:382:15)
at Method.resolve (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/method.js:147:44)
at Service.resolveAll (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/service.js:111:20)
at Namespace.resolveAll (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/namespace.js:307:25)
at Namespace.resolveAll (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/namespace.js:307:25)
at Namespace.resolveAll (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/namespace.js:307:25)
at Namespace.resolveAll (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/namespace.js:307:25)
at Root.resolveAll (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/namespace.js:307:25)
at Root.resolveAll (/Users/Herve/node_modules/@grpc/proto-loader/node_modules/protobufjs/src/root.js:258:43)
at Object.loadSync (/Users/Herve/node_modules/@grpc/proto-loader/build/src/index.js:218:16)
BoardDetailsReq
导致错误的消息在board.proto中定义,它包含includeDirs
在我的options
结构中。这不正确吗?如果发生此错误,我一定是做错了什么。