diff --git a/protoc-gen-go/generator/generator.go b/protoc-gen-go/generator/generator.go index 5b74811b11..4289c58422 100644 --- a/protoc-gen-go/generator/generator.go +++ b/protoc-gen-go/generator/generator.go @@ -1161,26 +1161,25 @@ func (g *Generator) generateImports() { continue } filename := goFileName(s) + // By default, import path is the dirname of the Go filename. + importPath := path.Dir(filename) if substitution, ok := g.ImportMap[s]; ok { - filename = substitution - } - filename = g.ImportPrefix + filename - if strings.HasSuffix(filename, ".go") { - filename = filename[0 : len(filename)-3] + importPath = substitution } + importPath = g.ImportPrefix + importPath // Skip weak imports. if g.weak(int32(i)) { - g.P("// skipping weak import ", fd.PackageName(), " ", strconv.Quote(filename)) + 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(filename)) + 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(filename)) + g.P("// discarding unused import ", fd.PackageName(), " ", strconv.Quote(importPath)) } } g.P() diff --git a/protoc-gen-go/plugin/Makefile b/protoc-gen-go/plugin/Makefile index 86d1d27331..8a49a29d51 100644 --- a/protoc-gen-go/plugin/Makefile +++ b/protoc-gen-go/plugin/Makefile @@ -35,9 +35,9 @@ regenerate: echo WARNING! THIS RULE IS PROBABLY NOT RIGHT FOR YOUR INSTALLATION cd $(HOME)/src/protobuf/src && \ - protoc --go_out=. ./google/protobuf/compiler/plugin.proto && \ - cat ./google/protobuf/compiler/plugin.pb.go | \ - sed '/^import/s;google/protobuf/descriptor.pb;github.com/golang/protobuf/protoc-gen-go/descriptor;' > $(GOPATH)/src/github.com/golang/protobuf/protoc-gen-go/plugin/plugin.pb.go + protoc --go_out=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. \ + ./google/protobuf/compiler/plugin.proto && \ + cat ./google/protobuf/compiler/plugin.pb.go > $(GOPATH)/src/github.com/golang/protobuf/protoc-gen-go/plugin/plugin.pb.go restore: cp plugin.pb.golden plugin.pb.go diff --git a/protoc-gen-go/testdata/my_test/test.pb.go b/protoc-gen-go/testdata/my_test/test.pb.go index 566588d4fe..4500414934 100644 --- a/protoc-gen-go/testdata/my_test/test.pb.go +++ b/protoc-gen-go/testdata/my_test/test.pb.go @@ -23,7 +23,7 @@ package my_test import proto "github.com/golang/protobuf/proto" import math "math" -// discarding unused import multitest2 "multi/multi1.pb" +// discarding unused import multitest2 "multi" // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/protoc-gen-go/testdata/my_test/test.pb.go.golden b/protoc-gen-go/testdata/my_test/test.pb.go.golden index 566588d4fe..4500414934 100644 --- a/protoc-gen-go/testdata/my_test/test.pb.go.golden +++ b/protoc-gen-go/testdata/my_test/test.pb.go.golden @@ -23,7 +23,7 @@ package my_test import proto "github.com/golang/protobuf/proto" import math "math" -// discarding unused import multitest2 "multi/multi1.pb" +// discarding unused import multitest2 "multi" // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal