diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index 5e6b7b76..46d26a24 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -200,7 +200,7 @@ func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp gen.Error(err) return } - genForward := func(tok token.Token, name string) { + genForward := func(tok token.Token, name string, expr ast.Expr) { // Don't import unexported symbols. r, _ := utf8.DecodeRuneInString(name) if !unicode.IsUpper(r) { @@ -210,6 +210,13 @@ func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp if name == impFile.GoDescriptorIdent.GoName { return } + // Don't import decls referencing a symbol defined in another package. + // i.e., don't import decls which are themselves public imports: + // + // type T = somepackage.T + if _, ok := expr.(*ast.SelectorExpr); ok { + return + } g.P(tok, " ", name, " = ", impFile.GoImportPath.Ident(name)) } g.P("// Symbols defined in public import of ", imp.Path()) @@ -220,10 +227,14 @@ func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp for _, spec := range decl.Specs { switch spec := spec.(type) { case *ast.TypeSpec: - genForward(decl.Tok, spec.Name.Name) + genForward(decl.Tok, spec.Name.Name, spec.Type) case *ast.ValueSpec: - for _, name := range spec.Names { - genForward(decl.Tok, name.Name) + for i, name := range spec.Names { + var expr ast.Expr + if i < len(spec.Values) { + expr = spec.Values[i] + } + genForward(decl.Tok, name.Name, expr) } case *ast.ImportSpec: default: diff --git a/cmd/protoc-gen-go/testdata/import_public/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/a.pb.go index 70151ad1..8814c7dd 100644 --- a/cmd/protoc-gen-go/testdata/import_public/a.pb.go +++ b/cmd/protoc-gen-go/testdata/import_public/a.pb.go @@ -20,7 +20,6 @@ const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package // Symbols defined in public import of import_public/sub/a.proto -type Sub2Message = sub.Sub2Message type E = sub.E const E_ZERO = sub.E_ZERO