diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index f204853d..7a28ae64 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -17,9 +17,9 @@ import ( "unicode" "unicode/utf8" - "github.com/golang/protobuf/proto" "github.com/golang/protobuf/v2/internal/descfield" "github.com/golang/protobuf/v2/internal/encoding/tag" + "github.com/golang/protobuf/v2/proto" "github.com/golang/protobuf/v2/protogen" "github.com/golang/protobuf/v2/reflect/protoreflect" @@ -228,11 +228,21 @@ func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp } func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) { + // TODO: Replace this with v2 Clone. + descProto := new(descriptorpb.FileDescriptorProto) + b, err := proto.Marshal(f.Proto) + if err != nil { + gen.Error(err) + return + } + if err := proto.Unmarshal(b, descProto); err != nil { + gen.Error(err) + return + } + // Trim the source_code_info from the descriptor. - // Marshal and gzip it. - descProto := proto.Clone(f.Proto).(*descriptorpb.FileDescriptorProto) descProto.SourceCodeInfo = nil - b, err := proto.Marshal(descProto) + b, err = proto.MarshalOptions{Deterministic: true}.Marshal(descProto) if err != nil { gen.Error(err) return diff --git a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta index c9669934..8b284633 100644 --- a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta +++ b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta @@ -1 +1 @@ -annotation: annotation: annotation: annotation: annotation: \ No newline at end of file +annotation:{path:5 path:0 source_file:"annotations/annotations.proto" begin:318 end:337} annotation:{path:5 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:354 end:401} annotation:{path:4 path:0 source_file:"annotations/annotations.proto" begin:1562 end:1584} annotation:{path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:1595 end:1615} annotation:{path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:3170 end:3193} \ No newline at end of file diff --git a/protogen/protogen.go b/protogen/protogen.go index cf1e8090..23bc69a2 100644 --- a/protogen/protogen.go +++ b/protogen/protogen.go @@ -28,9 +28,10 @@ import ( "strconv" "strings" - "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/v2/encoding/textpb" "github.com/golang/protobuf/v2/internal/descfield" "github.com/golang/protobuf/v2/internal/scalar" + "github.com/golang/protobuf/v2/proto" "github.com/golang/protobuf/v2/reflect/protodesc" "github.com/golang/protobuf/v2/reflect/protoreflect" "github.com/golang/protobuf/v2/reflect/protoregistry" @@ -1109,7 +1110,11 @@ func (g *GeneratedFile) metaFile(content []byte) (string, error) { } } - return strings.TrimSpace(proto.CompactTextString(info)), nil + b, err := textpb.MarshalOptions{Compact: true}.Marshal(info) + if err != nil { + return "", err + } + return string(b), nil } type pathType int