google.golang.org/grpc@v1.62.1/vet.sh (about) 1 #!/bin/bash 2 3 set -ex # Exit on error; debugging enabled. 4 set -o pipefail # Fail a pipe if any sub-command fails. 5 6 # not makes sure the command passed to it does not exit with a return code of 0. 7 not() { 8 # This is required instead of the earlier (! $COMMAND) because subshells and 9 # pipefail don't work the same on Darwin as in Linux. 10 ! "$@" 11 } 12 13 die() { 14 echo "$@" >&2 15 exit 1 16 } 17 18 fail_on_output() { 19 tee /dev/stderr | not read 20 } 21 22 # Check to make sure it's safe to modify the user's git repo. 23 git status --porcelain | fail_on_output 24 25 # Undo any edits made by this script. 26 cleanup() { 27 git reset --hard HEAD 28 } 29 trap cleanup EXIT 30 31 PATH="${HOME}/go/bin:${GOROOT}/bin:${PATH}" 32 go version 33 34 if [[ "$1" = "-install" ]]; then 35 # Install the pinned versions as defined in module tools. 36 pushd ./test/tools 37 go install \ 38 golang.org/x/tools/cmd/goimports \ 39 honnef.co/go/tools/cmd/staticcheck \ 40 github.com/client9/misspell/cmd/misspell 41 popd 42 if [[ -z "${VET_SKIP_PROTO}" ]]; then 43 if [[ "${GITHUB_ACTIONS}" = "true" ]]; then 44 PROTOBUF_VERSION=25.2 # a.k.a. v4.22.0 in pb.go files. 45 PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip 46 pushd /home/runner/go 47 wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME} 48 unzip ${PROTOC_FILENAME} 49 bin/protoc --version 50 popd 51 elif not which protoc > /dev/null; then 52 die "Please install protoc into your path" 53 fi 54 fi 55 exit 0 56 elif [[ "$#" -ne 0 ]]; then 57 die "Unknown argument(s): $*" 58 fi 59 60 # - Check that generated proto files are up to date. 61 if [[ -z "${VET_SKIP_PROTO}" ]]; then 62 make proto && git status --porcelain 2>&1 | fail_on_output || \ 63 (git status; git --no-pager diff; exit 1) 64 fi 65 66 if [[ -n "${VET_ONLY_PROTO}" ]]; then 67 exit 0 68 fi 69 70 # - Ensure all source files contain a copyright message. 71 # (Done in two parts because Darwin "git grep" has broken support for compound 72 # exclusion matches.) 73 (grep -L "DO NOT EDIT" $(git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)" -- '*.go') || true) | fail_on_output 74 75 # - Make sure all tests in grpc and grpc/test use leakcheck via Teardown. 76 not grep 'func Test[^(]' *_test.go 77 not grep 'func Test[^(]' test/*.go 78 79 # - Check for typos in test function names 80 git grep 'func (s) ' -- "*_test.go" | not grep -v 'func (s) Test' 81 git grep 'func [A-Z]' -- "*_test.go" | not grep -v 'func Test\|Benchmark\|Example' 82 83 # - Do not import x/net/context. 84 not git grep -l 'x/net/context' -- "*.go" 85 86 # - Do not import math/rand for real library code. Use internal/grpcrand for 87 # thread safety. 88 git grep -l '"math/rand"' -- "*.go" 2>&1 | not grep -v '^examples\|^interop/stress\|grpcrand\|^benchmark\|wrr_test' 89 90 # - Do not use "interface{}"; use "any" instead. 91 git grep -l 'interface{}' -- "*.go" 2>&1 | not grep -v '\.pb\.go\|protoc-gen-go-grpc\|grpc_testing_not_regenerate' 92 93 # - Do not call grpclog directly. Use grpclog.Component instead. 94 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' 95 96 # - Ensure all ptypes proto packages are renamed when importing. 97 not git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go" 98 99 # - Ensure all usages of grpc_testing package are renamed when importing. 100 not git grep "\(import \|^\s*\)\"google.golang.org/grpc/interop/grpc_testing" -- "*.go" 101 102 # - Ensure all xds proto imports are renamed to *pb or *grpc. 103 git grep '"github.com/envoyproxy/go-control-plane/envoy' -- '*.go' ':(exclude)*.pb.go' | not grep -v 'pb "\|grpc "' 104 105 misspell -error . 106 107 # - gofmt, goimports, go vet, go mod tidy. 108 # Perform these checks on each module inside gRPC. 109 for MOD_FILE in $(find . -name 'go.mod'); do 110 MOD_DIR=$(dirname ${MOD_FILE}) 111 pushd ${MOD_DIR} 112 go vet -all ./... | fail_on_output 113 gofmt -s -d -l . 2>&1 | fail_on_output 114 goimports -l . 2>&1 | not grep -vE "\.pb\.go" 115 116 go mod tidy -compat=1.19 117 git status --porcelain 2>&1 | fail_on_output || \ 118 (git status; git --no-pager diff; exit 1) 119 popd 120 done 121 122 # - Collection of static analysis checks 123 SC_OUT="$(mktemp)" 124 staticcheck -go 1.19 -checks 'all' ./... > "${SC_OUT}" || true 125 126 # Error for anything other than checks that need exclusions. 127 grep -v "(ST1000)" "${SC_OUT}" | grep -v "(SA1019)" | grep -v "(ST1003)" | not grep -v "(ST1019)\|\(other import of\)" 128 129 # Exclude underscore checks for generated code. 130 grep "(ST1003)" "${SC_OUT}" | not grep -v '\(.pb.go:\)\|\(code_string_test.go:\)\|\(grpc_testing_not_regenerate\)' 131 132 # Error for duplicate imports not including grpc protos. 133 grep "(ST1019)\|\(other import of\)" "${SC_OUT}" | not grep -Fv 'XXXXX PleaseIgnoreUnused 134 channelz/grpc_channelz_v1" 135 go-control-plane/envoy 136 grpclb/grpc_lb_v1" 137 health/grpc_health_v1" 138 interop/grpc_testing" 139 orca/v3" 140 proto/grpc_gcp" 141 proto/grpc_lookup_v1" 142 reflection/grpc_reflection_v1" 143 reflection/grpc_reflection_v1alpha" 144 XXXXX PleaseIgnoreUnused' 145 146 # Error for any package comments not in generated code. 147 grep "(ST1000)" "${SC_OUT}" | not grep -v "\.pb\.go:" 148 149 # Only ignore the following deprecated types/fields/functions and exclude 150 # generated code. 151 grep "(SA1019)" "${SC_OUT}" | not grep -Fv 'XXXXX PleaseIgnoreUnused 152 XXXXX Protobuf related deprecation errors: 153 "github.com/golang/protobuf 154 .pb.go: 155 grpc_testing_not_regenerate 156 : ptypes. 157 proto.RegisterType 158 XXXXX gRPC internal usage deprecation errors: 159 "google.golang.org/grpc 160 : grpc. 161 : v1alpha. 162 : v1alphareflectionpb. 163 BalancerAttributes is deprecated: 164 CredsBundle is deprecated: 165 Metadata is deprecated: use Attributes instead. 166 NewSubConn is deprecated: 167 OverrideServerName is deprecated: 168 RemoveSubConn is deprecated: 169 SecurityVersion is deprecated: 170 Target is deprecated: Use the Target field in the BuildOptions instead. 171 UpdateAddresses is deprecated: 172 UpdateSubConnState is deprecated: 173 balancer.ErrTransientFailure is deprecated: 174 grpc/reflection/v1alpha/reflection.proto 175 XXXXX xDS deprecated fields we support 176 .ExactMatch 177 .PrefixMatch 178 .SafeRegexMatch 179 .SuffixMatch 180 GetContainsMatch 181 GetExactMatch 182 GetMatchSubjectAltNames 183 GetPrefixMatch 184 GetSafeRegexMatch 185 GetSuffixMatch 186 GetTlsCertificateCertificateProviderInstance 187 GetValidationContextCertificateProviderInstance 188 XXXXX PleaseIgnoreUnused' 189 190 echo SUCCESS