cmd/protoc-gen-go: generate blank imports for unused proto dependencies
Generate Go imports for all packages imported by the .proto source file,
even if they are not referenced.
Change-Id: I116bdf460ab441d205b42606b2b05b315ed68954
Reviewed-on: https://go-review.googlesource.com/136358
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/main.go b/cmd/protoc-gen-go/main.go
index b477fe7..8c73c0c 100644
--- a/cmd/protoc-gen-go/main.go
+++ b/cmd/protoc-gen-go/main.go
@@ -148,15 +148,19 @@
}
func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *File, imp protoreflect.FileImport) {
- if !imp.IsPublic {
- return
- }
impFile, ok := gen.FileByName(imp.Path())
if !ok {
return
}
if impFile.GoImportPath == f.GoImportPath {
- // Don't generate aliases for types in the same Go package.
+ // Don't generate imports or aliases for types in the same Go package.
+ return
+ }
+ // Generate imports for all dependencies, even if they are not
+ // referenced, because other code and tools depend on having the
+ // full transitive closure of protocol buffer types in the binary.
+ g.Import(impFile.GoImportPath)
+ if !imp.IsPublic {
return
}
var enums []*protogen.Enum