Home > database >  protoc file not found error in generating for dart
protoc file not found error in generating for dart

Time:01-19

i wanted to generate the protos for dart by

protoc --proto_path=pb/proto --dart_out=lib/api/grpc/pb/gen --plugin=path/to/plugin/.pub-cachce/bin pb/proto/*.proto 

but it says

path/to/project/pb/proto/user.proto: File not found.
path/to/project/pb/proto/product.proto: File not found.
badget.proto:5:1: Import "path/to/project/pb/proto/user.proto" was not found or had errors.
badget.proto:6:1: Import "path/to/project/pb/proto/product.proto" was not found or had errors.
badget.proto:24:3: "packagename.pb.user.Actor" is not defined.
badget.proto:37:3: "packagename.pb.product.Product" is not defined.
badget.proto:65:22: "packagename.pb.user.Actor" is not defined.

!!!i actieved the plugin before that !!! the path to project is not compelete path (its started by the the path configured in setting)

protos=> badgte.proto:

syntax = "proto3";

package packagename.pb.badget;

import "path/to/project/pb/proto/user.proto";
import "path/to/project/pb/proto/product.proto";
import "google/protobuf/timestamp.proto";

blah blah blah;

user.proto:

syntax = "proto3";

package packagename.pb.user;

blah blah blah;

product.proto:

syntax = "proto3";

package packagename.pb.product;

blah blah blah;

CodePudding user response:

You may need to rearrange things so that the path specified in --proto_path is the parent directory of the paths you use for imports.

Paths in import statements are relative to what you've provided with --proto_path. So for --proto_path=pb/proto and the statement import "path/to/project/pb/proto/user.proto", protoc will be looking for a file "pb/proto/path/to/project/pb/proto/user.proto" which is probably not what you intend.

You might want to consider something like protoc --proto_path=path/to/project, so that your import statements are shorter/more portable:

import "pb/proto/user.proto";

Also, check this to see if it applies to your situation.

Finally, there are some typos in your post (".pub-cachce", "badget" vs. "badgte", etc.) so you may want to check on those as well, in case the typos are present in your actual code.

CodePudding user response:

Try:

.
└── proto
    └── mycompany
        └── mypackage
            ├── badge.proto
            ├── product.proto
            └── user.proto

I'm using mycompany and mypackage to provide an arbitrary nested package example.

When you define package paths, dots (.) separated components. This should be reflected in the folder (!) path on your file system but with e.g. / (Linux) separators. See below for why ../proto is omitted.

The import's similarly reflect the folder path (again ../proto is omitted).

user.proto:

syntax = "proto3";

package mycompany.mypackagename;

import "mycompany/mypackage/badge.proto";
import "mycompany/mypackage/product.proto";

import "google/protobuf/timestamp.proto";

message User {
    google.protobuf.Timestamp time = 1;
    mycompany.mypackage.Badge badge = 2;
    mycompany.mypackage.Product product = 3;
}

product.proto:

syntax = "proto3";

package mycompany.mypackage;

message Product {}

badge.proto:

syntax = "proto3";

package mycompany.mypackage;

message Badge {}

See how the package reflects the folder structure beneath ${PWD}/proto? Then, proto_path "anchors" the folder at the base of our package. We must define the absolute path to the root of our protos using proto_pathbut thereafter,packageandimport` references are relative (!) to it.

protoc \
--proto_path=${PWD}/proto \
--python_out=out \
${PWD}/proto/mycompany/mypackage/*.proto

I don't have the Dart plugin installed but the principle is the same:

.
├── out
│   └── mycompany
│       └── mypackage
│           ├── badge_pb2.py
│           ├── product_pb2.py
│           └── user_pb2.py
└── proto
    └── mycompany
        └── mypackage
            ├── badge.proto
            ├── product.proto
            └── user.proto
  • Related