Skip to content

Commit b67a9c7

Browse files
committed
Fix protobuf regeneration script. (#5291)
release/v1.2-specific change: use dgo/v2. This fixes the make regenerate command that should create the generated protobuf code. 1. make regenerate fails to run (error log attached in PR) 2. The protobuf dependencies are unintentionally updated to the latest version. 3. It was referencing protobuf definitions from GOPATH. So, if a package was not updated to the right version (e.g., updating github.com/dgraph-io/badger to the latest master), then there could be errors like the following when referencing the wrong version of a proto: pb.proto:285:18: "badgerpb2.KV" is not defined. pb.proto:297:11: "badgerpb2.KV" is not defined. pb.proto:545:5: "badgerpb2.KVList" is not defined. pb.proto:537:60: "badgerpb2.KVList" is not defined. pb.proto:28:1: warning: Import github.com/dgraph-io/badger/pb/pb.proto is unused. Changes * Include proto path /usr/local/include in protoc command to fix (1). * Update depcheck.sh to check that this directory exists. Error out with installation instructions otherwise if it's missing. * Install protobuf libraries from the versions in go.mod, and use go install instead of go get so the protobuf versions aren't updated automatically to fix (2). * Copy to temporary directory for proper import paths from go mod versions to remove any dependencies on $GOPATH to fix (3). * protos from proto library * proto from badger * proto from dgo * Regenerate protos
1 parent c9cbd13 commit b67a9c7

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

protos/Makefile

+24-9
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
# limitations under the License.
1515
#
1616

17-
PROTO_PATH := ${GOPATH}/src:.
18-
PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/dgraph-io/dgraph
19-
PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/dgraph-io/dgo/protos
20-
PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/dgraph-io/badger/pb
17+
DGO_PATH := github.com/dgraph-io/dgo/v2
18+
BADGER_PATH := github.com/dgraph-io/badger/v2
19+
GOGO_PATH := github.com/gogo/protobuf
20+
21+
TMPDIR := $(shell mktemp -d)
22+
PROTO_PATH := ${TMPDIR}/src:.
23+
PROTO_PATH := ${PROTO_PATH}:${TMPDIR}/src/${DGO_PATH}/protos
24+
PROTO_PATH := ${PROTO_PATH}:${TMPDIR}/src/${BADGER_PATH}/pb
2125

2226
.PHONY: help
2327
help:
@@ -30,14 +34,25 @@ clean:
3034
.PHONY: check
3135
check:
3236
@./depcheck.sh && \
33-
(echo "Upgrading proto libraries before regenerating." ; \
34-
go get -u github.com/golang/protobuf/protoc-gen-go ; \
35-
go get -u github.com/gogo/protobuf/protoc-gen-gofast)
37+
(echo "Installing proto libraries to versions in go.mod." ; \
38+
go install github.com/golang/protobuf/protoc-gen-go ; \
39+
go install github.com/gogo/protobuf/protoc-gen-gofast)
40+
41+
.PHONY: copy-protos
42+
copy-protos:
43+
@mkdir -p ${TMPDIR}/src/${DGO_PATH}/protos
44+
@mkdir -p ${TMPDIR}/src/${BADGER_PATH}/pb
45+
@mkdir -p ${TMPDIR}/src/${GOGO_PATH}/gogoproto
46+
@cp $(shell go list -m -f "{{.Dir}}" ${BADGER_PATH})/pb/pb.proto ${TMPDIR}/src/${BADGER_PATH}/pb/pb.proto
47+
@cp $(shell go list -m -f "{{.Dir}}" ${DGO_PATH})/protos/api.proto ${TMPDIR}/src/${DGO_PATH}/protos/api.proto
48+
@cp $(shell go list -m -f "{{.Dir}}" ${GOGO_PATH})/gogoproto/gogo.proto ${TMPDIR}/src/${GOGO_PATH}/gogoproto/gogo.proto
3649

3750
.PHONY: regenerate
38-
regenerate: check clean
51+
regenerate: copy-protos check clean
3952
@protoc \
53+
--proto_path=/usr/local/include \
4054
--proto_path=${PROTO_PATH} \
41-
--gofast_out=plugins=grpc,Mapi.proto=github.com/dgraph-io/dgo/v2/protos/api:pb \
55+
--gofast_out=plugins=grpc,Mapi.proto=${DGO_PATH}/protos/api:pb \
4256
pb.proto
57+
@rm -rf ${TMPDIR}
4358
@echo Done.

protos/depcheck.sh

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44

55
readonly PROTOCMINVER="3.6.1"
66

7-
which protoc &>/dev/null || (echo "Error: protoc not found" ; exit 1)
7+
which protoc &>/dev/null || (echo "Error: protoc not found" >&2; exit 1)
88

99
PROTOCVER=`protoc --version | awk '{printf $2}'`
1010

@@ -14,22 +14,34 @@ function CompareSemVer() {
1414
local minver=(${1//./ })
1515
local curver=(${2//./ })
1616

17-
echo "Checking for semantic version $1 or newer"
17+
echo -n "Checking protoc for semantic version $1 or newer... "
1818

1919
for i in 0 1 2; do
2020
if [ ${minver[$i]} -gt ${curver[$i]} ]; then
21-
echo "Error: version $2 is lower than the required version $1"
21+
echo "FAIL" >&2
22+
echo "Error: version $2 is lower than the required version $1" >&2
2223
exit 1
2324
elif [ ${curver[$i]} -gt ${minver[$i]} ]; then
2425
break
2526
fi
2627
done
2728
}
2829

29-
CompareSemVer $PROTOCMINVER $PROTOCVER
30+
function CheckProtobufIncludes() {
31+
echo -n "Checking for directory /usr/local/include/google/protobuf... "
32+
if [ ! -d /usr/local/include/google/protobuf ]; then
33+
echo "FAIL" >&2
34+
echo "Missing protobuf types in /usr/local/include/google/protobuf: directory not found" >&2
35+
echo "Download and install protoc and the protobuf types from protobuf releases page:" >&2
36+
echo "https://github.com/protocolbuffers/protobuf/releases/" >&2
37+
exit 1
38+
fi
39+
}
3040

31-
# TODO: check proto api versions
41+
CompareSemVer $PROTOCMINVER $PROTOCVER
42+
echo "OK"
3243

44+
CheckProtobufIncludes
3345
echo "OK"
3446

3547
exit 0

0 commit comments

Comments
 (0)