From a63bb2baad455783302217d969ec0649fbdaa65d Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Fri, 20 Mar 2020 14:20:30 -0700 Subject: [PATCH] internal/cmd/generate-alias: fix generator for lacking go_package options In https://golang.org/cl/219598, we removed patching the well-known types with an explicit go_package option and instead relied on M flags. The lack of updated go_package options broke generate-alias since it appears as if the import public is trying to alias Go identifiers within the same Go package (thus generating nothing). We fix generate-alias by replicating the same approach here, where we construct the M flag mapping and pass it to protoc-gen-go. --- go.mod | 2 +- go.sum | 4 ++- internal/cmd/generate-alias/main.go | 52 ++++++++++++++++++----------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index 9fb4ade071..2ce329e06e 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.9 require ( github.com/google/go-cmp v0.4.0 - google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967 + google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892 ) diff --git a/go.sum b/go.sum index 3d19e0b2be..b4226a0a1e 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,7 @@ github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= @@ -10,5 +11,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967 h1:DwkfSP6tZMxKX50J0dBSqEgJvJdFYP1Gvzbjtvkmrug= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892 h1:LQPEPzH8usKk01hUU5qdXb17Tgjr/BLZhHy1h90Vd7U= +google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= diff --git a/internal/cmd/generate-alias/main.go b/internal/cmd/generate-alias/main.go index dc25e1a041..6c16e6dc63 100644 --- a/internal/cmd/generate-alias/main.go +++ b/internal/cmd/generate-alias/main.go @@ -38,48 +38,60 @@ func main() { // Set of generated proto packages to forward to v2. files := []struct { - goPkg string - pbDesc protoreflect.FileDescriptor + oldGoPkg string + newGoPkg string + pbDesc protoreflect.FileDescriptor }{{ - goPkg: "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor", - pbDesc: descriptorpb.File_google_protobuf_descriptor_proto, + oldGoPkg: "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor", + newGoPkg: "google.golang.org/protobuf/types/descriptorpb", + pbDesc: descriptorpb.File_google_protobuf_descriptor_proto, }, { - goPkg: "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go", - pbDesc: pluginpb.File_google_protobuf_compiler_plugin_proto, + oldGoPkg: "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go", + newGoPkg: "google.golang.org/protobuf/types/pluginpb", + pbDesc: pluginpb.File_google_protobuf_compiler_plugin_proto, }, { - goPkg: "github.com/golang/protobuf/ptypes/any;any", - pbDesc: anypb.File_google_protobuf_any_proto, + oldGoPkg: "github.com/golang/protobuf/ptypes/any;any", + newGoPkg: "google.golang.org/protobuf/types/known/anypb", + pbDesc: anypb.File_google_protobuf_any_proto, }, { - goPkg: "github.com/golang/protobuf/ptypes/duration;duration", - pbDesc: durationpb.File_google_protobuf_duration_proto, + oldGoPkg: "github.com/golang/protobuf/ptypes/duration;duration", + newGoPkg: "google.golang.org/protobuf/types/known/durationpb", + pbDesc: durationpb.File_google_protobuf_duration_proto, }, { - goPkg: "github.com/golang/protobuf/ptypes/timestamp;timestamp", - pbDesc: timestamppb.File_google_protobuf_timestamp_proto, + oldGoPkg: "github.com/golang/protobuf/ptypes/timestamp;timestamp", + newGoPkg: "google.golang.org/protobuf/types/known/timestamppb", + pbDesc: timestamppb.File_google_protobuf_timestamp_proto, }, { - goPkg: "github.com/golang/protobuf/ptypes/wrappers;wrappers", - pbDesc: wrapperspb.File_google_protobuf_wrappers_proto, + oldGoPkg: "github.com/golang/protobuf/ptypes/wrappers;wrappers", + newGoPkg: "google.golang.org/protobuf/types/known/wrapperspb", + pbDesc: wrapperspb.File_google_protobuf_wrappers_proto, }, { - goPkg: "github.com/golang/protobuf/ptypes/struct;structpb", - pbDesc: structpb.File_google_protobuf_struct_proto, + oldGoPkg: "github.com/golang/protobuf/ptypes/struct;structpb", + newGoPkg: "google.golang.org/protobuf/types/known/structpb", + pbDesc: structpb.File_google_protobuf_struct_proto, }, { - goPkg: "github.com/golang/protobuf/ptypes/empty;empty", - pbDesc: emptypb.File_google_protobuf_empty_proto, + oldGoPkg: "github.com/golang/protobuf/ptypes/empty;empty", + newGoPkg: "google.golang.org/protobuf/types/known/emptypb", + pbDesc: emptypb.File_google_protobuf_empty_proto, }} // For each package, construct a proto file that public imports the package. var req pluginpb.CodeGeneratorRequest + var flags []string for _, file := range files { - pkgPath := file.goPkg[:strings.IndexByte(file.goPkg, ';')] + pkgPath := file.oldGoPkg[:strings.IndexByte(file.oldGoPkg, ';')] fd := &descriptorpb.FileDescriptorProto{ Name: proto.String(pkgPath + "/" + path.Base(pkgPath) + ".proto"), Syntax: proto.String(file.pbDesc.Syntax().String()), Dependency: []string{file.pbDesc.Path()}, PublicDependency: []int32{0}, - Options: &descriptorpb.FileOptions{GoPackage: proto.String(file.goPkg)}, + Options: &descriptorpb.FileOptions{GoPackage: proto.String(file.oldGoPkg)}, } req.ProtoFile = append(req.ProtoFile, protodesc.ToFileDescriptorProto(file.pbDesc), fd) req.FileToGenerate = append(req.FileToGenerate, fd.GetName()) + flags = append(flags, "M"+file.pbDesc.Path()+"="+file.newGoPkg) } + req.Parameter = proto.String(strings.Join(flags, ",")) // Use the internal logic of protoc-gen-go to generate the files. gen, err := protogen.Options{}.New(&req)