Ver código fonte

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 anos atrás
pai
commit
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))
 			g.P("// skipping weak import ", fd.PackageName(), " ", strconv.Quote(importPath))
 			continue
 			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()
 	g.P()
 	// TODO: may need to worry about uniqueness across plugins
 	// 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
 #	./extension_test
 #	@echo PASS
 #	@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:
 golden:
 	make -B my_test/test.pb.go
 	make -B my_test/test.pb.go
 	diff -w my_test/test.pb.go my_test/test.pb.go.golden
 	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.
 	// Compute the original checksum.
 	goldenSum := sum(t, "my_test/test.pb.go")
 	goldenSum := sum(t, "my_test/test.pb.go")
 	// Run the proto compiler.
 	// 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")
 	newFile := filepath.Join(os.TempDir(), "my_test/test.pb.go")
 	defer os.Remove(newFile)
 	defer os.Remove(newFile)
 	// Compute the new checksum.
 	// Compute the new checksum.

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

@@ -36,8 +36,8 @@ package testdata
 import (
 import (
 	"testing"
 	"testing"
 
 
-	multipb "./multi"
 	mytestpb "./my_test"
 	mytestpb "./my_test"
+	multipb "github.com/golang/protobuf/protoc-gen-go/testdata/multi"
 )
 )
 
 
 func TestLink(t *testing.T) {
 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 proto "github.com/golang/protobuf/proto"
 import fmt "fmt"
 import fmt "fmt"
 import math "math"
 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.
 // Reference imports to suppress errors if they are not otherwise used.
 var _ = proto.Marshal
 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 proto "github.com/golang/protobuf/proto"
 import fmt "fmt"
 import fmt "fmt"
 import math "math"
 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.
 // Reference imports to suppress errors if they are not otherwise used.
 var _ = proto.Marshal
 var _ = proto.Marshal