verify-staticcheck.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env bash
  2. # Copyright 2014 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  19. source "${KUBE_ROOT}/hack/lib/init.sh"
  20. source "${KUBE_ROOT}/hack/lib/util.sh"
  21. kube::golang::verify_go_version
  22. FOCUS="${1:-}"
  23. FOCUS="${FOCUS%/}" # Remove the ending "/"
  24. # See https://staticcheck.io/docs/checks
  25. CHECKS=(
  26. "all"
  27. "-S1*" # Omit code simplifications for now.
  28. "-ST1*" # Mostly stylistic, redundant w/ golint
  29. )
  30. export IFS=','; checks="${CHECKS[*]}"; unset IFS
  31. # Packages to ignore due to bugs in staticcheck
  32. # NOTE: To ignore issues detected a package, add it to the .staticcheck_failures blacklist
  33. IGNORE=(
  34. "vendor/k8s.io/kubectl/pkg/cmd/edit/testdata" # golang/go#24854, dominikh/go-tools#565
  35. )
  36. export IFS='|'; ignore_pattern="^(${IGNORE[*]})\$"; unset IFS
  37. # Ensure that we find the binaries we build before anything else.
  38. export GOBIN="${KUBE_OUTPUT_BINPATH}"
  39. PATH="${GOBIN}:${PATH}"
  40. # Install staticcheck from vendor
  41. echo 'installing staticcheck from vendor'
  42. go install k8s.io/kubernetes/vendor/honnef.co/go/tools/cmd/staticcheck
  43. cd "${KUBE_ROOT}"
  44. # Check that the file is in alphabetical order
  45. failure_file="${KUBE_ROOT}/hack/.staticcheck_failures"
  46. kube::util::check-file-in-alphabetical-order "${failure_file}"
  47. all_packages=()
  48. while IFS='' read -r line; do
  49. # Prepend './' to get staticcheck to treat these as paths, not packages.
  50. all_packages+=("./$line")
  51. done < <( hack/make-rules/helpers/cache_go_dirs.sh "${KUBE_ROOT}/_tmp/all_go_dirs" |
  52. grep "^${FOCUS:-.}" |
  53. grep -vE "(third_party|generated|clientset_generated|/_)" |
  54. grep -vE "$ignore_pattern" )
  55. failing_packages=()
  56. if [[ -z $FOCUS ]]; then # Ignore failing_packages in FOCUS mode
  57. while IFS='' read -r line; do failing_packages+=("$line"); done < <(cat "$failure_file")
  58. fi
  59. errors=()
  60. not_failing=()
  61. while read -r error; do
  62. # Ignore compile errors caused by lack of files due to build tags.
  63. # TODO: Add verification for these directories.
  64. ignore_no_files="^-: build constraints exclude all Go files in .* \(compile\)"
  65. if [[ $error =~ $ignore_no_files ]]; then
  66. continue
  67. fi
  68. file="${error%%:*}"
  69. pkg="$(dirname "$file")"
  70. kube::util::array_contains "$pkg" "${failing_packages[@]}" && in_failing=$? || in_failing=$?
  71. if [[ "${in_failing}" -ne "0" ]]; then
  72. errors+=( "${error}" )
  73. elif [[ "${in_failing}" -eq "0" ]]; then
  74. really_failing+=( "$pkg" )
  75. fi
  76. done < <(staticcheck -checks "${checks}" "${all_packages[@]}" 2>/dev/null || true)
  77. export IFS=$'\n' # Expand ${really_failing[*]} to separate lines
  78. kube::util::read-array really_failing < <(sort -u <<<"${really_failing[*]}")
  79. unset IFS
  80. for pkg in "${failing_packages[@]}"; do
  81. if ! kube::util::array_contains "$pkg" "${really_failing[@]}"; then
  82. not_failing+=( "$pkg" )
  83. fi
  84. done
  85. # Check that all failing_packages actually still exist
  86. gone=()
  87. for p in "${failing_packages[@]}"; do
  88. if ! kube::util::array_contains "./$p" "${all_packages[@]}"; then
  89. gone+=( "$p" )
  90. fi
  91. done
  92. # Check to be sure all the packages that should pass check are.
  93. if [ ${#errors[@]} -eq 0 ]; then
  94. echo 'Congratulations! All Go source files have passed staticcheck.'
  95. else
  96. {
  97. echo "Errors from staticcheck:"
  98. for err in "${errors[@]}"; do
  99. echo "$err"
  100. done
  101. echo
  102. echo 'Please review the above warnings. You can test via:'
  103. echo ' hack/verify-staticcheck.sh <failing package>'
  104. echo 'If the above warnings do not make sense, you can exempt the line or file. See:'
  105. echo ' https://staticcheck.io/docs/#ignoring-problems'
  106. echo
  107. } >&2
  108. exit 1
  109. fi
  110. if [[ ${#not_failing[@]} -gt 0 ]]; then
  111. {
  112. echo "Some packages in hack/.staticcheck_failures are passing staticcheck. Please remove them."
  113. echo
  114. for p in "${not_failing[@]}"; do
  115. echo " $p"
  116. done
  117. echo
  118. } >&2
  119. exit 1
  120. fi
  121. if [[ ${#gone[@]} -gt 0 ]]; then
  122. {
  123. echo "Some packages in hack/.staticcheck_failures do not exist anymore. Please remove them."
  124. echo
  125. for p in "${gone[@]}"; do
  126. echo " $p"
  127. done
  128. echo
  129. } >&2
  130. exit 1
  131. fi