From 40a08052f9262635c95780ff6d0d203b34f3754d Mon Sep 17 00:00:00 2001 From: Damien Neil Date: Mon, 29 Oct 2018 09:07:41 -0700 Subject: [PATCH] cmd/protoc-gen-go: don't import _ weak imports Don't generate a blank import for unused weak imports. Full support for weak imports would involve not importing the package at all. This just avoids generating an import when we don't need one. Change-Id: I7e8491f415dc8333a2837db5225256b959921be2 Reviewed-on: https://go-review.googlesource.com/c/145497 Reviewed-by: Joe Tsai --- cmd/protoc-gen-go/internal_gengo/main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index ed0d07fc..89d40113 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -136,10 +136,12 @@ func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp // Don't generate imports or aliases for types in the same Go package. return } - // Generate imports for all dependencies, even if they are not + // Generate imports for all non-weak 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.IsWeak { + g.Import(impFile.GoImportPath) + } if !imp.IsPublic { return }