Closed deviator closed 5 years ago
From proto3 documentation
The protocol compiler searches for imported files in a set of directories specified on the protocol compiler command line using the -I/--proto_path flag. If no flag was given, it looks in the directory in which the compiler was invoked. In general you should set the --proto_path flag to the root of your project and use fully qualified names for all imports.
Will this work?
ppp.proto
syntax = "proto3";
package msg;
import public "msg/one.proto"; // <-- msg/ prefix
message Data {
repeated OneData one = 1;
}
Will this work?
No =(
msg/one.proto: File not found.
ppp.proto: Import "msg/one.proto" was not found or had errors.
ppp.proto:7:14: "OneData" is not defined.
Solution: use package name same as name of directory what contains .proto
files.
My first try:
├── proto
│ ├── one.proto
│ └── ppp.proto
└── source
├── app.d
└── msg
├── one.d
└── ppp.d
need to change to:
├── msg
│ ├── one.proto
│ └── ppp.proto
└── source
├── app.d
└── msg
├── one.d
└── ppp.d
and at ppp.proto
change import string to import public "msg/one.proto";
and change build command to
protoc --plugin=protobuf-d/build/protoc-gen-d --d_out=source msg/ppp.proto
@dcarp while it indeed seems to be discouraged to have a different directory structure than the package structure, it works perfectly fine for other languages. E.g. the java output of the above example is something like the following:
├── msg
│ ├── One.java
│ └── Ppp.java
msg/Ppp.java
:
package msg;
public final class Ppp {
private Ppp() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
public interface DataOrBuilder extends
// @@protoc_insertion_point(interface_extends:msg.Data)
com.google.protobuf.MessageOrBuilder {
/**
* <code>repeated .msg.OneData one = 1;</code>
*/
java.util.List<msg.One.OneData>
getOneList();
/* ... */
I also think that it is (unfortunately) quite common practice to do it like this. At least at my work place it is done like that. So I think that we should support this.
I have 2 files:
one.proto
ppp.proto
For generate I use this command
and get in
source/msg
2 output filesThen I compile I get error message: