Просмотр исходного кода

Always import non-weak imported proto packages.

This is required so we get the transitive closure of proto types.

This has negligible impact on generated binary sizes.
David Symonds 10 лет назад
Родитель
Сommit
a1dfa5ef89

+ 8 - 9
protoc-gen-go/generator/generator.go

@@ -1271,15 +1271,14 @@ func (g *Generator) generateImports() {
 			g.P("// skipping weak import ", fd.PackageName(), " ", strconv.Quote(importPath))
 			continue
 		}
-		if _, ok := g.usedPackages[fd.PackageName()]; ok {
-			g.P("import ", fd.PackageName(), " ", strconv.Quote(importPath))
-		} else {
-			// TODO: Re-enable this when we are more feature-complete.
-			// For instance, some protos use foreign field extensions, which we don't support.
-			// Until then, this is just annoying spam.
-			//log.Printf("protoc-gen-go: discarding unused import from %v: %v", *g.file.Name, s)
-			g.P("// discarding unused import ", fd.PackageName(), " ", strconv.Quote(importPath))
-		}
+		// We need to import all the dependencies, even if we don't reference them,
+		// because other code and tools depend on having the full transitive closure
+		// of protocol buffer types in the binary.
+		pname := fd.PackageName()
+		if _, ok := g.usedPackages[pname]; !ok {
+			pname = "_"
+		}
+		g.P("import ", pname, " ", strconv.Quote(importPath))
 	}
 	g.P()
 	// TODO: may need to worry about uniqueness across plugins

+ 3 - 0
protoc-gen-go/testdata/Makefile

@@ -40,6 +40,9 @@ test:	golden testbuild
 #	./extension_test
 #	@echo PASS
 
+my_test/test.pb.go: my_test/test.proto
+	protoc --go_out=Mmulti/multi1.proto=github.com/golang/protobuf/protoc-gen-go/testdata/multi:. $<
+
 golden:
 	make -B my_test/test.pb.go
 	diff -w my_test/test.pb.go my_test/test.pb.go.golden

+ 1 - 1
protoc-gen-go/testdata/golden_test.go

@@ -73,7 +73,7 @@ func TestGolden(t *testing.T) {
 	// Compute the original checksum.
 	goldenSum := sum(t, "my_test/test.pb.go")
 	// Run the proto compiler.
-	run(t, "protoc", "--go_out="+os.TempDir(), "my_test/test.proto")
+	run(t, "protoc", "--go_out=Mmulti/multi1.proto=github.com/golang/protobuf/protoc-gen-go/testdata/multi:"+os.TempDir(), "my_test/test.proto")
 	newFile := filepath.Join(os.TempDir(), "my_test/test.pb.go")
 	defer os.Remove(newFile)
 	// Compute the new checksum.

+ 1 - 1
protoc-gen-go/testdata/main_test.go

@@ -36,8 +36,8 @@ package testdata
 import (
 	"testing"
 
-	multipb "./multi"
 	mytestpb "./my_test"
+	multipb "github.com/golang/protobuf/protoc-gen-go/testdata/multi"
 )
 
 func TestLink(t *testing.T) {

+ 1 - 2
protoc-gen-go/testdata/my_test/test.pb.go

@@ -24,8 +24,7 @@ package my_test
 import proto "github.com/golang/protobuf/proto"
 import fmt "fmt"
 import math "math"
-
-// discarding unused import multitest2 "multi"
+import _ "github.com/golang/protobuf/protoc-gen-go/testdata/multi"
 
 // Reference imports to suppress errors if they are not otherwise used.
 var _ = proto.Marshal

+ 1 - 2
protoc-gen-go/testdata/my_test/test.pb.go.golden

@@ -24,8 +24,7 @@ package my_test
 import proto "github.com/golang/protobuf/proto"
 import fmt "fmt"
 import math "math"
-
-// discarding unused import multitest2 "multi"
+import _ "github.com/golang/protobuf/protoc-gen-go/testdata/multi"
 
 // Reference imports to suppress errors if they are not otherwise used.
 var _ = proto.Marshal