2018-05-01 23:45:06 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -ex # Exit on error; debugging enabled.
|
|
|
|
set -o pipefail # Fail a pipe if any sub-command fails.
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
# not makes sure the command passed to it does not exit with a return code of 0.
|
|
|
|
not() {
|
|
|
|
# This is required instead of the earlier (! $COMMAND) because subshells and
|
|
|
|
# pipefail don't work the same on Darwin as in Linux.
|
|
|
|
! "$@"
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
die() {
|
|
|
|
echo "$@" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
fail_on_output() {
|
2020-09-09 18:09:42 +00:00
|
|
|
tee /dev/stderr | not read
|
2019-04-17 17:15:55 +00:00
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
# Check to make sure it's safe to modify the user's git repo.
|
|
|
|
git status --porcelain | fail_on_output
|
|
|
|
|
|
|
|
# Undo any edits made by this script.
|
|
|
|
cleanup() {
|
|
|
|
git reset --hard HEAD
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
2022-01-25 13:15:24 +00:00
|
|
|
PATH="${HOME}/go/bin:${GOROOT}/bin:${PATH}"
|
|
|
|
go version
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
if [[ "$1" = "-install" ]]; then
|
2022-01-25 13:15:24 +00:00
|
|
|
# Install the pinned versions as defined in module tools.
|
|
|
|
pushd ./test/tools
|
|
|
|
go install \
|
|
|
|
golang.org/x/tools/cmd/goimports \
|
|
|
|
honnef.co/go/tools/cmd/staticcheck \
|
|
|
|
github.com/client9/misspell/cmd/misspell
|
|
|
|
popd
|
2019-04-17 17:15:55 +00:00
|
|
|
if [[ -z "${VET_SKIP_PROTO}" ]]; then
|
2023-12-11 11:58:19 +00:00
|
|
|
if [[ "${GITHUB_ACTIONS}" = "true" ]]; then
|
2024-05-07 10:19:58 +00:00
|
|
|
PROTOBUF_VERSION=25.2 # a.k.a. v4.22.0 in pb.go files.
|
2022-01-25 13:15:24 +00:00
|
|
|
PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip
|
|
|
|
pushd /home/runner/go
|
|
|
|
wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME}
|
|
|
|
unzip ${PROTOC_FILENAME}
|
|
|
|
bin/protoc --version
|
|
|
|
popd
|
2020-09-09 18:09:42 +00:00
|
|
|
elif not which protoc > /dev/null; then
|
2018-05-01 23:45:06 +00:00
|
|
|
die "Please install protoc into your path"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
elif [[ "$#" -ne 0 ]]; then
|
|
|
|
die "Unknown argument(s): $*"
|
|
|
|
fi
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
# - Check that generated proto files are up to date.
|
|
|
|
if [[ -z "${VET_SKIP_PROTO}" ]]; then
|
|
|
|
make proto && git status --porcelain 2>&1 | fail_on_output || \
|
|
|
|
(git status; git --no-pager diff; exit 1)
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "${VET_ONLY_PROTO}" ]]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
# - Ensure all source files contain a copyright message.
|
2023-01-11 00:12:59 +00:00
|
|
|
# (Done in two parts because Darwin "git grep" has broken support for compound
|
|
|
|
# exclusion matches.)
|
|
|
|
(grep -L "DO NOT EDIT" $(git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)" -- '*.go') || true) | fail_on_output
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
# - Make sure all tests in grpc and grpc/test use leakcheck via Teardown.
|
2020-09-09 18:09:42 +00:00
|
|
|
not grep 'func Test[^(]' *_test.go
|
|
|
|
not grep 'func Test[^(]' test/*.go
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
# - Check for typos in test function names
|
|
|
|
git grep 'func (s) ' -- "*_test.go" | not grep -v 'func (s) Test'
|
|
|
|
git grep 'func [A-Z]' -- "*_test.go" | not grep -v 'func Test\|Benchmark\|Example'
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
# - Do not import x/net/context.
|
|
|
|
not git grep -l 'x/net/context' -- "*.go"
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2024-05-07 10:19:58 +00:00
|
|
|
# - Do not use time.After except in tests. It has the potential to leak the
|
|
|
|
# timer since there is no way to stop it early.
|
|
|
|
git grep -l 'time.After(' -- "*.go" | not grep -v '_test.go\|test_utils\|testutils'
|
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
# - Do not import math/rand for real library code. Use internal/grpcrand for
|
|
|
|
# thread safety.
|
2023-12-11 11:58:19 +00:00
|
|
|
git grep -l '"math/rand"' -- "*.go" 2>&1 | not grep -v '^examples\|^interop/stress\|grpcrand\|^benchmark\|wrr_test'
|
|
|
|
|
|
|
|
# - Do not use "interface{}"; use "any" instead.
|
2024-05-07 10:19:58 +00:00
|
|
|
git grep -l 'interface{}' -- "*.go" 2>&1 | not grep -v '\.pb\.go\|protoc-gen-go-grpc\|grpc_testing_not_regenerate'
|
2020-09-09 18:09:42 +00:00
|
|
|
|
|
|
|
# - Do not call grpclog directly. Use grpclog.Component instead.
|
2023-01-11 00:12:59 +00:00
|
|
|
git grep -l -e 'grpclog.I' --or -e 'grpclog.W' --or -e 'grpclog.E' --or -e 'grpclog.F' --or -e 'grpclog.V' -- "*.go" | not grep -v '^grpclog/component.go\|^internal/grpctest/tlogger_test.go'
|
2019-04-17 17:15:55 +00:00
|
|
|
|
|
|
|
# - Ensure all ptypes proto packages are renamed when importing.
|
2020-09-09 18:09:42 +00:00
|
|
|
not git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go"
|
2019-04-17 17:15:55 +00:00
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
# - Ensure all usages of grpc_testing package are renamed when importing.
|
|
|
|
not git grep "\(import \|^\s*\)\"google.golang.org/grpc/interop/grpc_testing" -- "*.go"
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
# - Ensure all xds proto imports are renamed to *pb or *grpc.
|
|
|
|
git grep '"github.com/envoyproxy/go-control-plane/envoy' -- '*.go' ':(exclude)*.pb.go' | not grep -v 'pb "\|grpc "'
|
2019-04-17 17:15:55 +00:00
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
misspell -error .
|
2019-04-17 17:15:55 +00:00
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
# - gofmt, goimports, go vet, go mod tidy.
|
2022-01-25 13:15:24 +00:00
|
|
|
# Perform these checks on each module inside gRPC.
|
|
|
|
for MOD_FILE in $(find . -name 'go.mod'); do
|
|
|
|
MOD_DIR=$(dirname ${MOD_FILE})
|
|
|
|
pushd ${MOD_DIR}
|
|
|
|
go vet -all ./... | fail_on_output
|
|
|
|
gofmt -s -d -l . 2>&1 | fail_on_output
|
|
|
|
goimports -l . 2>&1 | not grep -vE "\.pb\.go"
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
go mod tidy -compat=1.19
|
2020-09-09 18:09:42 +00:00
|
|
|
git status --porcelain 2>&1 | fail_on_output || \
|
2018-05-01 23:45:06 +00:00
|
|
|
(git status; git --no-pager diff; exit 1)
|
2022-01-25 13:15:24 +00:00
|
|
|
popd
|
|
|
|
done
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
# - Collection of static analysis checks
|
2020-09-09 18:09:42 +00:00
|
|
|
SC_OUT="$(mktemp)"
|
2023-12-11 11:58:19 +00:00
|
|
|
staticcheck -go 1.19 -checks 'all' ./... > "${SC_OUT}" || true
|
|
|
|
|
|
|
|
# Error for anything other than checks that need exclusions.
|
|
|
|
grep -v "(ST1000)" "${SC_OUT}" | grep -v "(SA1019)" | grep -v "(ST1003)" | not grep -v "(ST1019)\|\(other import of\)"
|
|
|
|
|
|
|
|
# Exclude underscore checks for generated code.
|
2024-05-07 10:19:58 +00:00
|
|
|
grep "(ST1003)" "${SC_OUT}" | not grep -v '\(.pb.go:\)\|\(code_string_test.go:\)\|\(grpc_testing_not_regenerate\)'
|
2023-12-11 11:58:19 +00:00
|
|
|
|
|
|
|
# Error for duplicate imports not including grpc protos.
|
|
|
|
grep "(ST1019)\|\(other import of\)" "${SC_OUT}" | not grep -Fv 'XXXXX PleaseIgnoreUnused
|
|
|
|
channelz/grpc_channelz_v1"
|
|
|
|
go-control-plane/envoy
|
|
|
|
grpclb/grpc_lb_v1"
|
|
|
|
health/grpc_health_v1"
|
|
|
|
interop/grpc_testing"
|
|
|
|
orca/v3"
|
|
|
|
proto/grpc_gcp"
|
|
|
|
proto/grpc_lookup_v1"
|
|
|
|
reflection/grpc_reflection_v1"
|
|
|
|
reflection/grpc_reflection_v1alpha"
|
|
|
|
XXXXX PleaseIgnoreUnused'
|
|
|
|
|
|
|
|
# Error for any package comments not in generated code.
|
|
|
|
grep "(ST1000)" "${SC_OUT}" | not grep -v "\.pb\.go:"
|
|
|
|
|
|
|
|
# Only ignore the following deprecated types/fields/functions and exclude
|
|
|
|
# generated code.
|
|
|
|
grep "(SA1019)" "${SC_OUT}" | not grep -Fv 'XXXXX PleaseIgnoreUnused
|
|
|
|
XXXXX Protobuf related deprecation errors:
|
|
|
|
"github.com/golang/protobuf
|
|
|
|
.pb.go:
|
2024-05-07 10:19:58 +00:00
|
|
|
grpc_testing_not_regenerate
|
2023-12-11 11:58:19 +00:00
|
|
|
: ptypes.
|
|
|
|
proto.RegisterType
|
|
|
|
XXXXX gRPC internal usage deprecation errors:
|
|
|
|
"google.golang.org/grpc
|
|
|
|
: grpc.
|
|
|
|
: v1alpha.
|
|
|
|
: v1alphareflectionpb.
|
|
|
|
BalancerAttributes is deprecated:
|
|
|
|
CredsBundle is deprecated:
|
|
|
|
Metadata is deprecated: use Attributes instead.
|
|
|
|
NewSubConn is deprecated:
|
|
|
|
OverrideServerName is deprecated:
|
|
|
|
RemoveSubConn is deprecated:
|
|
|
|
SecurityVersion is deprecated:
|
2022-01-25 13:15:24 +00:00
|
|
|
Target is deprecated: Use the Target field in the BuildOptions instead.
|
2023-12-11 11:58:19 +00:00
|
|
|
UpdateAddresses is deprecated:
|
|
|
|
UpdateSubConnState is deprecated:
|
|
|
|
balancer.ErrTransientFailure is deprecated:
|
|
|
|
grpc/reflection/v1alpha/reflection.proto
|
2024-05-07 10:19:58 +00:00
|
|
|
SwitchTo is deprecated:
|
2023-12-11 11:58:19 +00:00
|
|
|
XXXXX xDS deprecated fields we support
|
|
|
|
.ExactMatch
|
|
|
|
.PrefixMatch
|
|
|
|
.SafeRegexMatch
|
|
|
|
.SuffixMatch
|
|
|
|
GetContainsMatch
|
|
|
|
GetExactMatch
|
|
|
|
GetMatchSubjectAltNames
|
|
|
|
GetPrefixMatch
|
|
|
|
GetSafeRegexMatch
|
|
|
|
GetSuffixMatch
|
|
|
|
GetTlsCertificateCertificateProviderInstance
|
|
|
|
GetValidationContextCertificateProviderInstance
|
|
|
|
XXXXX PleaseIgnoreUnused'
|
2020-09-09 18:09:42 +00:00
|
|
|
|
|
|
|
echo SUCCESS
|