Skip to content

Commit

Permalink
Make generated proto filename unique. (#3351)
Browse files Browse the repository at this point in the history
According to https://protobuf.dev/reference/go/faq/#namespace-conflict:
Starting with v1.26.0 of the google.golang.org/protobuf module, a hard error will be reported when a Go program starts up that has multiple conflicting protobuf names linked into it.

The above causes errors when linking generated code from multiple
services with identical names.
  • Loading branch information
raphael authored Sep 10, 2023
1 parent 286aa67 commit 73bdf21
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion grpc/codegen/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
Expand All @@ -14,6 +15,9 @@ import (
const (
// ProtoVersion is the protocol buffer version used to generate .proto files
ProtoVersion = "proto3"

// ProtoPrefix is the prefix added to the proto package name.
ProtoPrefix = "goagen"
)

// ProtoFiles returns a *.proto file for each gRPC service.
Expand All @@ -28,7 +32,16 @@ func ProtoFiles(genpkg string, root *expr.RootExpr) []*codegen.File {
func protoFile(genpkg string, svc *expr.GRPCServiceExpr) *codegen.File {
data := GRPCServices.Get(svc.Name())
svcName := data.Service.PathName
path := filepath.Join(codegen.Gendir, "grpc", svcName, pbPkgName, "goadesign_goagen_"+svcName+".proto")
parts := strings.Split(genpkg, "/")
var repoName string
if len(parts) > 1 {
repoName = parts[len(parts)-2]
} else {
repoName = parts[0]
}
// the filename is used by protoc to set the namespace so try to make it unique
fname := fmt.Sprintf("%s_%s_%s.proto", ProtoPrefix, repoName, svcName)
path := filepath.Join(codegen.Gendir, "grpc", svcName, pbPkgName, fname)

sections := []*codegen.SectionTemplate{
// header comments
Expand Down

0 comments on commit 73bdf21

Please sign in to comment.