proto-loader unable to load .proto file with dependencies

Tags: , , ,



I’m trying to load .proto files coming from the arduino-cli repo. More specifically, I’m loading the commands.proto that has a dependency on a few other .proto files within the same directory.

In the load options provided to proto-loader, I specified the paths to all these .proto dependencies, yet an error pops up stating that proto messages defined in one of these dependencies are not defined.

Here is my code:

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

The last line causes this error:

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

The BoardDetailsReq message causing the error is defined in boards.proto, which is included with the includeDirs in my options struct. Is this not correct? I must be doing something wrong if this error is happening.

Answer

The purpose of the includeDirs option is to list directories that should be searched for imported files. The file in question imports commands/common.proto, commands/board.proto, etc. So includeDirs should point to the directory that contains those files. In this case, that is RPC_PATH. So, your includeDirs list should just be [RPC_PATH].



Source: stackoverflow