8000 internal/cmd/generate-alias: fix for lacking go_package options (#1061) · golang/protobuf@7592abe · GitHub
[go: up one dir, main page]

Skip to content

Commit 7592abe

Browse files
authored
internal/cmd/generate-alias: fix for lacking go_package options (#1061)
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.
1 parent b860323 commit 7592abe

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.9
44

55
require (
66
github.com/google/go-cmp v0.4.0
7-
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967
7+
google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892
88
)

go.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
22
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
33
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
4+
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
45
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
56
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
67
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
1011
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
1112
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
1213
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
13-
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967 h1:DwkfSP6tZMxKX50J0dBSqEgJvJdFYP1Gvzbjtvkmrug=
1414
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
15+
google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892 h1:LQPEPzH8usKk01hUU5qdXb17Tgjr/BLZhHy1h90Vd7U=
16+
google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=

internal/cmd/generate-alias/main.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,48 +38,60 @@ func main() {
3838

3939
// Set of generated proto packages to forward to v2.
4040
files := []struct {
41-
goPkg string
42-
pbDesc protoreflect.FileDescriptor
41+
oldGoPkg string
42+
newGoPkg string
43+
pbDesc protoreflect.FileDescriptor
4344
}{{
44-
goPkg: "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor",
45-
pbDesc: descriptorpb.File_google_protobuf_descriptor_proto,
45+
oldGoPkg: "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor",
46+
newGoPkg: "google.golang.org/protobuf/types/descriptorpb",
47+
pbDesc: descriptorpb.File_google_protobuf_descriptor_proto,
4648
}, {
47-
goPkg: "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go",
48-
pbDesc: pluginpb.File_google_protobuf_compiler_plugin_proto,
49+
oldGoPkg: "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go",
50+
newGoPkg: "google.golang.org/protobuf/types/pluginpb",
51+
pbDesc: pluginpb.File_google_protobuf_compiler_plugin_proto,
4952
}, {
50-
goPkg: "github.com/golang/protobuf/ptypes/any;any",
51-
pbDesc: anypb.File_google_protobuf_any_proto,
53+
oldGoPkg: "github.com/golang/protobuf/ptypes/any;any",
54+
newGoPkg: "google.golang.org/protobuf/types/known/anypb",
55+
pbDesc: anypb.File_google_protobuf_any_proto,
5256
}, {
53-
goPkg: "github.com/golang/protobuf/ptypes/duration;duration",
54-
pbDesc: durationpb.File_google_protobuf_duration_proto,
57+
oldGoPkg: "github.com/golang/protobuf/ptypes/duration;duration",
58+
newGoPkg: "google.golang.org/protobuf/types/known/durationpb",
59+
pbDesc: durationpb.File_google_protobuf_duration_proto,
5560
}, {
56-
goPkg: "github.com/golang/protobuf/ptypes/timestamp;timestamp",
57-
pbDesc: timestamppb.File_google_protobuf_timestamp_proto,
61+
oldGoPkg: "github.com/golang/protobuf/ptypes/timestamp;timestamp",
62+
newGoPkg: "google.golang.org/protobuf/types/known/timestamppb",
63+
pbDesc: timestamppb.File_google_protobuf_timestamp_proto,
5864
}, {
59-
goPkg: "github.com/golang/protobuf/ptypes/wrappers;wrappers",
60-
pbDesc: wrapperspb.File_google_protobuf_wrappers_proto,
65+
oldGoPkg: "github.com/golang/protobuf/ptypes/wrappers;wrappers",
66+
newGoPkg: "google.golang.org/protobuf/types/known/wrapperspb",
67+
pbDesc: wrapperspb.File_google_protobuf_wrappers_proto,
6168
}, {
62-
goPkg: "github.com/golang/protobuf/ptypes/struct;structpb",
63-
pbDesc: structpb.File_google_protobuf_struct_proto,
69+
oldGoPkg: "github.com/golang/protobuf/ptypes/struct;structpb",
70+
newGoPkg: "google.golang.org/protobuf/types/known/structpb",
71+
pbDesc: structpb.File_google_protobuf_struct_proto,
6472
}, {
65-
goPkg: "github.com/golang/protobuf/ptypes/empty;empty",
66-
pbDesc: emptypb.File_google_protobuf_empty_proto,
73+
oldGoPkg: "github.com/golang/protobuf/ptypes/empty;empty",
74+
newGoPkg: "google.golang.org/protobuf/types/known/emptypb",
75+
pbDesc: emptypb.File_google_protobuf_empty_proto,
6776
}}
6877

6978
// For each package, construct a proto file that public imports the package.
7079
var req pluginpb.CodeGeneratorRequest
80+
var flags []string
7181
for _, file := range files {
72-
pkgPath := file.goPkg[:strings.IndexByte(file.goPkg, ';')]
82+
pkgPath := file.oldGoPkg[:strings.IndexByte(file.oldGoPkg, ';')]
7383
fd := &descriptorpb.FileDescriptorProto{
7484
Name: proto.String(pkgPath + "/" + path.Base(pkgPath) + ".proto"),
7585
Syntax: proto.String(file.pbDesc.Syntax().String()),
7686
Dependency: []string{file.pbDesc.Path()},
7787
PublicDependency: []int32{0},
78-
Options: &descriptorpb.FileOptions{GoPackage: proto.String(file.goPkg)},
88+
Options: &descriptorpb.FileOptions{GoPackage: proto.String(file.oldGoPkg)},
7989
}
8090
req.ProtoFile = append(req.ProtoFile, protodesc.ToFileDescriptorProto(file.pbDesc), fd)
8191
req.FileToGenerate = append(req.FileToGenerate, fd.GetName())
92+
flags = append(flags, "M"+file.pbDesc.Path()+"="+file.newGoPkg)
8293
}
94+
req.Parameter = proto.String(strings.Join(flags, ","))
8395

8496
// Use the internal logic of protoc-gen-go to generate the files.
8597
gen, err := protogen.Options{}.New(&req)

0 commit comments

Comments
 (0)
0