vet.sh 6.1 KB

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