Skip to content

Commit 2402d76

Browse files
committed
Add compatibility markers to proto generated code.
This asserts at build time that the generated code is compatible with the proto package that it is linked to.
1 parent 4df416c commit 2402d76

File tree

8 files changed

+42
-0
lines changed

8 files changed

+42
-0
lines changed

jsonpb/jsonpb_test_proto/more_test_objects.pb.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/lib.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,3 +887,7 @@ func isProto3Zero(v reflect.Value) bool {
887887
}
888888
return false
889889
}
890+
891+
// ProtoPackageIsVersion1 is referenced from generated protocol buffer files
892+
// to assert that that code is compatible with this version of the proto package.
893+
const ProtoPackageIsVersion1 = true

proto/testdata/test.pb.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc-gen-go/descriptor/descriptor.pb.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc-gen-go/generator/generator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ import (
5858
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
5959
)
6060

61+
// generatedCodeVersion indicates a version of the generated code.
62+
// It is incremented whenever an incompatibility between the generated code and
63+
// proto package is introduced; the generated code references
64+
// a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion).
65+
const generatedCodeVersion = 1
66+
6167
// A Plugin provides functionality to add to the output during Go code generation,
6268
// such as to produce RPC stubs.
6369
type Plugin interface {
@@ -1122,6 +1128,14 @@ func (g *Generator) generate(file *FileDescriptor) {
11221128
g.file = g.FileOf(file.FileDescriptorProto)
11231129
g.usedPackages = make(map[string]bool)
11241130

1131+
if g.file.index == 0 {
1132+
// For one file in the package, assert version compatibility.
1133+
g.P("// This is a compile-time assertion to ensure that this generated file")
1134+
g.P("// is compatible with the proto package it is being compiled against.")
1135+
g.P("const _ = ", g.Pkg["proto"], ".ProtoPackageIsVersion", generatedCodeVersion)
1136+
g.P()
1137+
}
1138+
11251139
for _, td := range g.file.imp {
11261140
g.generateImported(td)
11271141
}

protoc-gen-go/plugin/plugin.pb.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc-gen-go/testdata/my_test/test.pb.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc-gen-go/testdata/my_test/test.pb.go.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ var _ = proto.Marshal
3131
var _ = fmt.Errorf
3232
var _ = math.Inf
3333

34+
// This is a compile-time assertion to ensure that this generated file
35+
// is compatible with the proto package it is being compiled against.
36+
const _ = proto.ProtoPackageIsVersion1
37+
3438
type HatType int32
3539

3640
const (

0 commit comments

Comments
 (0)