Question: compile always add default "google/protobuf/descriptor.proto" to parse.
Closed this issue · 2 comments
Description of the problem
- I put the copy of "google/protobuf/xxx.proto" in my own repository.
common
├── google
│ └── protobuf
│ ├── any.proto
│ ├── api.proto
│ ├── compiler
│ │ └── plugin.proto
│ ├── descriptor.proto
│ ├── duration.proto
│ ├── empty.proto
│ ├── field_mask.proto
│ ├── source_context.proto
│ ├── struct.proto
│ ├── timestamp.proto
│ ├── type.proto
│ └── wrappers.proto
-
At the same time "google/protobuf/descriptor.proto" is also in "/usr/local/include".
-
the
Compiler.Compile()
will return errsymbol "google.protobuf.FileDescriptorSet" already defined at google/protobuf/descriptor.proto:57:9
rs := &WrapSourceResolver{
SourceResolver: &protocompile.SourceResolver{
ImportPaths: append(includeDirs, "/usr/local/include"),
},
AbsFiles: map[string]bool{},
}
c := protocompile.Compiler{
Resolver: rs,
MaxParallelism: 1,
SourceInfoMode: protocompile.SourceInfoExtraComments,
}
- I checked the source code and this line parses "google/protobuf/descriptor.proto" under the import Path by default. In actuality, I'm not using "google/protobuf/descriptor.proto" in my every proto file, but "common/google/protobuf/descriptor.proto".
Expected results
i do not want to the error symbol "google.protobuf.FileDescriptorSet" already defined at google/protobuf/descriptor.proto:57:9
@FGYFFFF, the problem is the way you are importing it, with the "common/" prefix. In general, a file should always be imported using its canonical import path, which in this case is "google/protobuf/descriptor.proto".
You can read more here: https://buf.build/docs/reference/protobuf-files-and-packages#file-paths
So a work-around here is to strip the "common" prefix from your source imports and add <include-dir>/common
as one of the import paths.
An alternate work-around, if you insist on keeping the "common" prefix in your imports, would be to handle this in your WrapSourceResolver
implementation: when it is invoked with a path of "google/protobuf/descriptor.proto", have it immediately return a "file not found" error instead of delegating to its SourceResolver
field.
To be clear, the "google/protobuf/descriptor.proto" file that is included in the compile is the one provided by the compiler's configured source resolver. This is tentatively included in every compile so that the compiler knows what version of options messages to use when interpreting options, even in files that do not explicitly import any version of the file. So if you do not provide any such file, it will not be included in the compile.
For the files that actually a import a different path (the one with the "common/" prefix), the correct version of options messages will be used since the actual transitive dependency graph of a file is consulted first when looking for the options messages definitions.