Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
FGYFFFF opened this issue Jun 27, 2024 · 2 comments

Comments

@FGYFFFF
Copy link

FGYFFFF commented Jun 27, 2024

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 err symbol "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

@jhump
Copy link
Member

jhump commented Jul 1, 2024

@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
Copy link
Member

jhump commented Jul 1, 2024

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.

@FGYFFFF FGYFFFF closed this as completed Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants