google.golang.org/grpc@v1.72.2/scripts/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 source "$(dirname $0)/common.sh" 7 8 # Check to make sure it's safe to modify the user's git repo. 9 git status --porcelain | fail_on_output 10 11 # Undo any edits made by this script. 12 cleanup() { 13 git reset --hard HEAD 14 } 15 trap cleanup EXIT 16 17 if [ -n "${GOROOT}" ]; then 18 PATH="${GOROOT}/bin:${PATH}" 19 fi 20 PATH="${HOME}/go/bin:${PATH}" 21 go version 22 23 if [[ "$1" = "-install" ]]; then 24 # Install the pinned versions as defined in module tools. 25 pushd ./test/tools 26 go install \ 27 golang.org/x/tools/cmd/goimports \ 28 honnef.co/go/tools/cmd/staticcheck \ 29 github.com/client9/misspell/cmd/misspell \ 30 github.com/mgechev/revive 31 popd 32 exit 0 33 elif [[ "$#" -ne 0 ]]; then 34 die "Unknown argument(s): $*" 35 fi 36 37 # - Ensure all source files contain a copyright message. 38 # (Done in two parts because Darwin "git grep" has broken support for compound 39 # exclusion matches.) 40 (grep -L "DO NOT EDIT" $(git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)" -- '*.go') || true) | fail_on_output 41 42 # - Make sure all tests in grpc and grpc/test use leakcheck via Teardown. 43 not grep 'func Test[^(]' -- *_test.go 44 not grep 'func Test[^(]' -- test/*.go 45 46 # - Check for typos in test function names 47 git grep 'func (s) ' -- "*_test.go" | not grep -v 'func (s) Test' 48 git grep 'func [A-Z]' -- "*_test.go" | not grep -v 'func Test\|Benchmark\|Example' 49 50 # - Do not use time.After except in tests. It has the potential to leak the 51 # timer since there is no way to stop it early. 52 git grep -l 'time.After(' -- "*.go" | not grep -v '_test.go\|soak_tests\|testutils' 53 54 # - Do not use "interface{}"; use "any" instead. 55 git grep -l 'interface{}' -- "*.go" 2>&1 | not grep -v '\.pb\.go\|protoc-gen-go-grpc\|grpc_testing_not_regenerated' 56 57 # - Do not call grpclog directly. Use grpclog.Component instead. 58 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\|^internal/grpclog/prefix_logger.go' 59 60 # - Ensure that the deprecated protobuf dependency is not used. 61 not git grep "\"github.com/golang/protobuf/*" -- "*.go" ':(exclude)testdata/grpc_testing_not_regenerated/*' 62 63 # - Ensure all usages of grpc_testing package are renamed when importing. 64 not git grep "\(import \|^\s*\)\"google.golang.org/grpc/interop/grpc_testing" -- "*.go" 65 66 # - Ensure that no trailing spaces are found. 67 not git grep '[[:blank:]]$' 68 69 # - Ensure that all files have a terminating newline. 70 git ls-files | not xargs -I {} sh -c '[ -n "$(tail -c 1 "{}" 2>/dev/null)" ] && echo "{}: No terminating new line found"' | fail_on_output 71 72 # - Ensure that no tabs are found in markdown files. 73 not git grep $'\t' -- '*.md' 74 75 # - Ensure all xds proto imports are renamed to *pb or *grpc. 76 git grep '"github.com/envoyproxy/go-control-plane/envoy' -- '*.go' ':(exclude)*.pb.go' | not grep -v 'pb "\|grpc "' 77 78 # - Ensure all context usages are done with timeout. 79 # Context tests under benchmark are excluded as they are testing the performance of context.Background() and context.TODO(). 80 git grep -e 'context.Background()' --or -e 'context.TODO()' -- "*_test.go" | grep -v "benchmark/primitives/context_test.go" | grep -v 'context.WithTimeout(' | not grep -v 'context.WithCancel(' 81 82 # Disallow usage of net.ParseIP in favour of netip.ParseAddr as the former 83 # can't parse link local IPv6 addresses. 84 not git grep 'net.ParseIP' -- '*.go' 85 86 misspell -error . 87 88 # Get the absolute path to revive.toml relative to the script location 89 REVIVE_CONFIG_PATH="$(dirname "$(realpath "$0")")/revive.toml" 90 91 # - gofmt, goimports, go vet, go mod tidy. 92 # Perform these checks on each module inside gRPC. 93 for MOD_FILE in $(find . -name 'go.mod'); do 94 MOD_DIR=$(dirname ${MOD_FILE}) 95 pushd ${MOD_DIR} 96 go vet -all ./... | fail_on_output 97 gofmt -s -d -l . 2>&1 | fail_on_output 98 goimports -l . 2>&1 | not grep -vE "\.pb\.go" 99 100 go mod tidy -compat=1.23 101 git status --porcelain 2>&1 | fail_on_output || \ 102 (git status; git --no-pager diff; exit 1) 103 104 # Error for violation of enabled lint rules in config excluding generated code. 105 revive \ 106 -set_exit_status=1 \ 107 -exclude "testdata/grpc_testing_not_regenerated/" \ 108 -exclude "**/*.pb.go" \ 109 -formatter plain \ 110 -config "${REVIVE_CONFIG_PATH}" \ 111 ./... 112 113 # - Collection of static analysis checks 114 SC_OUT="$(mktemp)" 115 # By default, Staticcheck targets the Go version declared in go.mod via the go 116 # directive. For Go 1.21 and newer, that directive specifies the minimum 117 # required version of Go. 118 # If a version is provided to Staticcheck using the -go flag, and the go 119 # toolchain version is higher than the one in go.mod, Staticcheck will report 120 # errors for usages of new language features in the std lib code. 121 staticcheck -checks 'all' ./... >"${SC_OUT}" || true 122 123 # Error for anything other than checks that need exclusions. 124 noret_grep -v "(ST1000)" "${SC_OUT}" | noret_grep -v "(SA1019)" | noret_grep -v "(ST1003)" | noret_grep -v "(ST1019)\|\(other import of\)" | not grep -v "(SA4000)" 125 126 # Exclude underscore checks for generated code. 127 noret_grep "(ST1003)" "${SC_OUT}" | not grep -v '\(.pb.go:\)\|\(code_string_test.go:\)\|\(grpc_testing_not_regenerated\)' 128 129 # Error for duplicate imports not including grpc protos. 130 noret_grep "(ST1019)\|\(other import of\)" "${SC_OUT}" | not grep -Fv 'XXXXX PleaseIgnoreUnused 131 channelz/grpc_channelz_v1" 132 go-control-plane/envoy 133 grpclb/grpc_lb_v1" 134 health/grpc_health_v1" 135 interop/grpc_testing" 136 orca/v3" 137 proto/grpc_gcp" 138 proto/grpc_lookup_v1" 139 examples/features/proto/echo" 140 reflection/grpc_reflection_v1" 141 reflection/grpc_reflection_v1alpha" 142 XXXXX PleaseIgnoreUnused' 143 144 # Error for any package comments not in generated code. 145 noret_grep "(ST1000)" "${SC_OUT}" | not grep -v "\.pb\.go:" 146 147 # Ignore a false positive when operands have side affects. 148 # TODO(https://github.com/dominikh/go-tools/issues/54): Remove this once the issue is fixed in staticcheck. 149 noret_grep "(SA4000)" "${SC_OUT}" | not grep -v -e "crl.go:[0-9]\+:[0-9]\+: identical expressions on the left and right side of the '||' operator (SA4000)" 150 151 # Usage of the deprecated Logger interface from prefix_logger.go is the only 152 # allowed one. If any other files use the deprecated interface, this check 153 # will fails. Also, note that this same deprecation notice is also added to 154 # the list of ignored notices down below to allow for the usage in 155 # prefix_logger.go to not case vet failure. 156 noret_grep "(SA1019)" "${SC_OUT}" | noret_grep "internal.Logger is deprecated:" | not grep -v -e "grpclog/logger.go" 157 158 # Only ignore the following deprecated types/fields/functions and exclude 159 # generated code. 160 noret_grep "(SA1019)" "${SC_OUT}" | not grep -Fv 'XXXXX PleaseIgnoreUnused 161 XXXXX Protobuf related deprecation errors: 162 "github.com/golang/protobuf 163 .pb.go: 164 grpc_testing_not_regenerated 165 : ptypes. 166 proto.RegisterType 167 XXXXX gRPC internal usage deprecation errors: 168 "google.golang.org/grpc 169 : grpc. 170 : v1alpha. 171 : v1alphareflectionpb. 172 BalancerAttributes is deprecated: 173 CredsBundle is deprecated: 174 GetMetadata is deprecated: 175 internal.Logger is deprecated: 176 Metadata is deprecated: use Attributes instead. 177 NewAddress is deprecated: 178 NewSubConn is deprecated: 179 OverrideServerName is deprecated: 180 RemoveSubConn is deprecated: 181 SecurityVersion is deprecated: 182 Target is deprecated: Use the Target field in the BuildOptions instead. 183 UpdateAddresses is deprecated: 184 UpdateSubConnState is deprecated: 185 balancer.ErrTransientFailure is deprecated: 186 grpc/reflection/v1alpha/reflection.proto 187 SwitchTo is deprecated: 188 XXXXX xDS deprecated fields we support 189 .ExactMatch 190 .PrefixMatch 191 .SafeRegexMatch 192 .SuffixMatch 193 GetContainsMatch 194 GetExactMatch 195 GetMatchSubjectAltNames 196 GetPrefixMatch 197 GetSafeRegexMatch 198 GetSuffixMatch 199 GetTlsCertificateCertificateProviderInstance 200 GetValidationContextCertificateProviderInstance 201 XXXXX PleaseIgnoreUnused' 202 popd 203 done 204 205 echo SUCCESS