Reputation: 99
I have seen there already similar problems published, I could not resolve the problem with the solutions proposed.
I am struggeling to reference a proto in another proto file.
I have the .proto file example.proto that is referencing A.proto in the dependency folder, example.proto is placed next to the dependency folder.
A.proto
syntax = "proto3";
package dependency;
message AttributeA {
// Some attribute.
string body = 1;
}
example.proto
syntax = "proto3";
import "src/proto_test/dependency/A.proto";
package example;
message CheckRequest {
// The request attributes.
int32 id = 1;
dependency.AttributeA att = 2;
}
The server file looks as following:
Server
const grpc = require("@grpc/grpc-js");
const PROTO_PATH = "./src/proto_test/example.proto";
var protoLoader = require("@grpc/proto-loader");
const options = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs: [path.join(__dirname,"/proto_test/dependency")]
};
var packageDefinition = protoLoader.loadSync(PROTO_PATH, options);
const proto = grpc.loadPackageDefinition(packageDefinition);
const server = new grpc.Server();
server.bindAsync(
"127.0.0.1:50054",
grpc.ServerCredentials.createInsecure(),
(error, port) => {
console.log("Server running at http://127.0.0.1:50054");
server.start();
}
);
Error
Error: no such Type or Enum 'dependency.AttributeA' in Type .example.CheckRequest#
Is there anything wrong with this code?
Upvotes: 5
Views: 4470
Reputation: 99
I do not know exactly why, but this code is working:
example.proto
syntax = "proto3";
import "dependency/A.proto";
package example;
message CheckRequest {
// The request attributes.
int32 id = 1;
dependency.AttributeA att = 2;
}
Server
const grpc = require("@grpc/grpc-js");
const PROTO_PATH = "example.proto";
var protoLoader = require("@grpc/proto-loader");
var path = require('path')
const options = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs: [
path.join(__dirname,"/proto_test/dependency"),
path.join(__dirname,"/proto_test")]
};
var packageDefinition = protoLoader.loadSync(PROTO_PATH, options);
console.log(packageDefinition);
const proto = grpc.loadPackageDefinition(packageDefinition);
const server = new grpc.Server();
server.bindAsync(
"127.0.0.1:50054",
grpc.ServerCredentials.createInsecure(),
(error, port) => {
console.log("Server running at http://127.0.0.1:50054");
server.start();
}
);
Upvotes: 3