Files
stamp/scripts/check-deps.sh
T
software-maintainer 050936c6fd Make dependency checks offline-safe and guard the CI workflow with tests
scripts/check-deps.sh previously ran `go list -m all`, which fails with
"module lookup disabled by GOPROXY=off" whenever the module cache does not
hold the full module graph, even though the build only needs part of it.
Read the pins with `go mod edit -json` instead: the online branch asks the
module proxy about only the pinned modules, and the offline branch compares
those pins with the newest versions in the module cache. Neither path loads
the whole graph, and an empty cache is reported instead of failing.

Run the dependency check from the single check job that already gates pull
requests, so one action covers both the code/tests and dependency freshness.

Add internal/ci tests that parse .gitea/workflows/ci.yml, fail if the
workflow stops running on pull requests or stops invoking the check scripts,
and fail if those scripts are not executable. Document the two-step job and
the new mise tasks.
2026-10-28 08:57:59 +00:00

125 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# check-deps.sh fails when a module required by go.mod is not the newest
# version available.
#
# When the module proxy is reachable, `go list -m -u` is authoritative: it
# asks the proxy for the newest release of each pinned module. When the proxy
# is disabled (offline builds, sandboxes), the script compares the pins with
# the newest versions recorded in the local module cache instead.
#
# The script never loads the full module graph, so it works even when the cache
# only holds the modules needed by the build.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
# requirements prints "<path> <version>" for every module required by go.mod.
requirements() {
go mod edit -json | awk -F'"' '
/^\t"Require": \[/ { in_require = 1 }
in_require && /^\t\],?$/ { in_require = 0 }
in_require && /"Path":/ { path = $4 }
in_require && /"Version":/ { print path, $4 }
'
}
# escape_path applies the case encoding used by the module cache for paths with
# 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"
}
# newest prints the newest version in the stdin version list. Version lists in
# the module cache are unsorted and include pre-releases, so sort them with
# `sort -V` and then prefer a release to its own pre-releases
# (v1.2.0 outranks v1.2.0-rc1, while v1.2.1-0.20240101 outranks v1.2.0).
newest() {
local versions latest base
versions="$(grep -E '^v[0-9]' || true)"
if [ -z "$versions" ]; then
return 1
fi
latest="$(printf '%s\n' "$versions" | sort -V | tail -n 1)"
base="${latest%%-*}"
if printf '%s\n' "$versions" | grep -qxF "$base"; then
latest="$base"
fi
printf '%s\n' "$latest"
}
pins="$(requirements)"
if [ -z "$pins" ]; then
echo 'go.mod requires no modules' >&2
exit 1
fi
count="$(printf '%s\n' "$pins" | wc -l | tr -d ' ')"
if [ "$(go env GOPROXY)" != "off" ]; then
mods=()
while read -r path version; do
mods+=("$path@$version")
done <<<"$pins"
if ! listing="$(go list -m -u -f '{{.Path}} {{.Version}} {{if .Update}}{{.Update.Version}}{{end}}' "${mods[@]}")"; then
echo 'could not query the module proxy for newer versions' >&2
exit 1
fi
updates="$(printf '%s\n' "$listing" | awk 'NF == 3')"
if [ -n "$updates" ]; then
echo 'These modules have newer versions available:' >&2
printf '%s\n' "$updates" >&2
exit 1
fi
echo "ok: $count go.mod requirements are up to date"
exit 0
fi
cache="$(go env GOMODCACHE)/cache/download"
checked=0
unchecked=0
stale=''
while read -r path version; do
list="$cache/$(escape_path "$path")/@v/list"
if [ ! -f "$list" ]; then
unchecked=$((unchecked + 1))
continue
fi
checked=$((checked + 1))
latest="$(newest <"$list")"
if [ "$latest" = "$version" ]; then
continue
fi
# Only complain when the cached version really is newer than the pin; a
# pin that is newer than everything cached is simply not verifiable here.
if [ "$(printf '%s\n%s\n' "$version" "$latest" | sort -V | tail -n 1)" = "$latest" ]; then
stale+=" $path $version (newest cached: $latest)"$'\n'
fi
done <<<"$pins"
if [ -n "$stale" ]; then
echo 'These go.mod pins are behind the newest cached version:' >&2
printf '%s' "$stale" >&2
exit 1
fi
if [ "$checked" -eq 0 ]; then
echo "note: GOPROXY=off and $cache is empty; dependency freshness was not checked" >&2
exit 0
fi
if [ "$unchecked" -ne 0 ]; then
echo "note: $unchecked of $count go.mod requirements are missing from the module cache" >&2
fi
echo "ok: $checked go.mod requirements match the newest versions in the module cache"