bufbuild / protocompile

A parsing/linking engine for protobuf; the guts for a pure Go replacement of protoc.
Apache License 2.0
211 stars 17 forks source link

Question: compile always add default "google/protobuf/descriptor.proto" to parse. #320

Open FGYFFFF opened 1 week ago

FGYFFFF commented 1 week ago

Description of the problem

Expected results i do not want to the error symbol "google.protobuf.FileDescriptorSet" already defined at google/protobuf/descriptor.proto:57:9

jhump commented 6 days ago

@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.

jhump commented 6 days ago

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.