lint-dependencies.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env bash
  2. # Copyright 2019 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. # Explicitly opt into go modules, even though we're inside a GOPATH directory
  21. export GO111MODULE=on
  22. # Explicitly clear GOFLAGS, since GOFLAGS=-mod=vendor breaks dependency resolution while rebuilding vendor
  23. export GOFLAGS=
  24. # Detect problematic GOPROXY settings that prevent lookup of dependencies
  25. if [[ "${GOPROXY:-}" == "off" ]]; then
  26. kube::log::error "Cannot run with \$GOPROXY=off"
  27. exit 1
  28. fi
  29. kube::golang::verify_go_version
  30. kube::util::require-jq
  31. outdated=$(go list -m -json all | jq -r '
  32. select(.Replace.Version != null) |
  33. select(.Version != .Replace.Version) |
  34. "\(.Path)
  35. pinned: \(.Replace.Version)
  36. preferred: \(.Version)
  37. hack/pin-dependency.sh \(.Path) \(.Version)"
  38. ')
  39. if [[ -n "${outdated}" ]]; then
  40. echo "These modules are pinned to versions different than the minimal preferred version."
  41. echo "That means that without require directives, a different version would be selected."
  42. echo "The command to switch to the minimal preferred version is listed for each module."
  43. echo ""
  44. echo "${outdated}"
  45. fi
  46. unused=$(comm -23 \
  47. <(go mod edit -json | jq -r '.Replace[] | select(.New.Version != null) | .Old.Path' | sort) \
  48. <(go list -m -json all | jq -r .Path | sort))
  49. if [[ -n "${unused}" ]]; then
  50. echo ""
  51. echo "Pinned module versions that aren't actually used:"
  52. echo "${unused}" | xargs -L 1 echo 'GO111MODULE=on go mod edit -dropreplace'
  53. fi
  54. if [[ -n "${unused}${outdated}" ]]; then
  55. exit 1
  56. fi
  57. echo "All pinned dependencies match their preferred version."
  58. exit 0