#!/usr/bin/env bash # # check-deps.sh verifies that every module pinned in go.mod is the newest # version available. # # When the module proxy is reachable, `go list -m -u` is authoritative and any # module with a newer release fails the check. When the proxy is disabled (for # example in an offline build), the script falls back to comparing each pin # with the newest version present in the local module cache, which still # catches pins that were left behind after a newer version was downloaded. set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")/.." echo '==> module graph is loadable' go list -m -e all >/dev/null echo '==> modules are up to date' updates="$(go list -m -u -e -f '{{.Path}} {{.Version}} {{if .Update}}{{.Update.Version}}{{end}}' all | awk 'NF == 3')" if [ -n "$updates" ]; then echo 'These modules have newer versions available:' >&2 printf '%s\n' "$updates" >&2 exit 1 fi if [ "$(go env GOPROXY)" != "off" ]; then exit 0 fi # The module proxy is disabled, so `go list -m -u` cannot report updates. # Compare the pins in go.mod with the versions in the local module cache. cache="$(go env GOMODCACHE)/cache/download" # escape_path applies the case encoding used by the module cache for paths that # contain upper-case letters (for example BurntSushi becomes !burnt!sushi). escape_path() { local path="$1" out='' char i for ((i = 0; i < ${#path}; i++)); do char="${path:i:1}" if [[ $char == [A-Z] ]]; then out+="!${char,,}" else out+="$char" fi done printf '%s' "$out" } checked=0 stale='' while read -r path version; do list="$cache/$(escape_path "$path")/@v/list" if [ ! -f "$list" ]; then continue fi checked=$((checked + 1)) latest="$(sort -V "$list" | tail -n 1)" if [ "$latest" != "$version" ]; then stale+="$path $version (newest in cache: $latest)"$'\n' fi done < <(go mod edit -json | awk -F'"' '/"Path":/ { path = $4 } /"Version":/ { print path, $4 }') if [ -n "$stale" ]; then echo 'These go.mod pins are not the newest cached versions:' >&2 printf '%s' "$stale" >&2 exit 1 fi if [ "$checked" -eq 0 ]; then echo 'note: proxy disabled and module cache is empty; freshness not checked' >&2 else echo "all $checked go.mod pins match the newest versions in the module cache" fi