Files
software-maintainer c098c93ae9
ci / build and test (pull_request) Successful in 1m4s
Upgrade module pins to newest releases and make dependency checks authoritative
The require blocks were unchanged from the base manifest even though newer
releases are available for most modules. Pin every module to the newest
release observed on proxy.golang.org, add the checksums published by
sum.golang.org, and add the transitive modules the upgraded graph now needs
(bitset, clipperhouse/displaywidth, clipperhouse/stringish,
clipperhouse/uax29/v2, go.yaml.in/yaml/v3). charmbracelet/huh is a direct
import and is recorded as such.

Rewrite scripts/check-deps.sh so it never treats the local module cache as
evidence of what the newest release is. It uses `go list -m -u` when the proxy
is reachable and otherwise compares every go.mod pin with the dependency facts
prepared by the execution profile, validating the report source, freshness
status and query scope. If neither source is available it exits with status 2
instead of reporting success.

Add scripts/test-check-deps.sh, which covers stale, uncovered and unverifiable
pins without network access, and scripts/test-ci-workflow.sh, which checks that
the single Gitea Actions job still runs on pull requests and invokes the checks.
Both run from scripts/check.sh, so one pass verifies formatting, vet, build,
tests, the CI workflow and dependency freshness.

`go build ./...`, `go vet ./...`, `go test -cover ./...`, `go mod verify` and the
new scripts all pass in the sandbox.
2026-10-28 09:59:57 +00:00

173 lines
6.0 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# check-deps.sh fails when a module pinned in go.mod is older than the newest
# release we can authoritatively observe.
#
# Two sources are accepted, in order:
#
# 1. The module proxy. When GOPROXY is not "off", `go list -m -u` is the
# authority: it asks the proxy for the newest release of every pinned
# module. This is what CI uses.
#
# 2. The dependency facts prepared by the execution profile and named by
# MAINTAINER_DEPENDENCY_FACTS. The file records, for every module, the
# newest version that profile observed on proxy.golang.org and when. It is
# evidence rather than a live query, so the script prints its source and
# observation time and refuses to use reports it cannot validate as complete.
#
# The local module cache is never consulted: it only contains versions that
# happened to be downloaded, so it cannot establish what the newest release is.
# When neither source is available the script exits with status 2 instead of
# reporting success.
#
# Exit status: 0 = every pin is current, 1 = a pin is stale (or unverifiable
# for an individual module), 2 = freshness could not be established at all.
set -euo pipefail
root="${CHECK_DEPS_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
cd "$root"
facts_file="${MAINTAINER_DEPENDENCY_FACTS:-}"
# 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 }
'
}
# behind reports whether version $1 is older than version $2.
behind() {
[ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | tail -n 1)" = "$2" ]
}
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 2
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 (module proxy)"
exit 0
fi
# Offline: fall back to the dependency facts prepared by the execution profile.
# The facts file is refreshed by the execution profile, so a reader can race
# with an atomic rewrite; give it a moment before declaring the facts missing.
if [ -n "$facts_file" ]; then
for _ in 1 2 3 4 5 6 7 8 9 10; do
[ -r "$facts_file" ] && break
sleep 0.2
done
fi
if [ -z "$facts_file" ] || [ ! -r "$facts_file" ]; then
echo 'cannot verify dependency freshness: the module proxy is disabled (GOPROXY=off)' \
'and MAINTAINER_DEPENDENCY_FACTS does not name a readable dependency-facts file' >&2
exit 2
fi
field() {
grep -o "\"$1\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" "$facts_file" | head -n 1 | sed 's/.*:[[:space:]]*"\(.*\)"/\1/'
}
schema="$(grep -o '"schema_version"[[:space:]]*:[[:space:]]*[0-9]*' "$facts_file" | head -n 1 | grep -o '[0-9]*$' || true)"
source="$(field source)"
freshness="$(field freshness_status)"
scope="$(field query_scope)"
observed_at="$(field observed_at)"
digest="$(field manifest_digest)"
if [ "$schema" != 1 ]; then
echo "cannot verify dependency freshness: unsupported dependency-facts schema (${schema:-missing})" >&2
exit 2
fi
if [ "$source" != 'https://proxy.golang.org' ]; then
echo "cannot verify dependency freshness: unexpected dependency-facts source '${source}'" >&2
exit 2
fi
if [ "$freshness" != 'available' ]; then
echo "cannot verify dependency freshness: dependency facts are not marked available ('${freshness}')" >&2
exit 2
fi
if [ "$scope" != 'module_path_latest' ]; then
echo "cannot verify dependency freshness: dependency facts use query scope '${scope}', want module_path_latest" >&2
exit 2
fi
if [ -z "$observed_at" ]; then
echo 'cannot verify dependency freshness: dependency facts have no observation time' >&2
exit 2
fi
# One record per observed module: name, latest version and item status.
records="$(grep -o '{[^{}]*"latest"[^{}]*' "$facts_file" || true)"
if [ -z "$records" ]; then
echo 'cannot verify dependency freshness: dependency facts contain no module observations' >&2
exit 2
fi
latest_for() {
local want="$1" record name latest status
while IFS= read -r record; do
name="$(printf '%s\n' "$record" | sed -n 's/.*"name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')"
[ "$name" = "$want" ] || continue
latest="$(printf '%s\n' "$record" | sed -n 's/.*"latest"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')"
status="$(printf '%s\n' "$record" | sed -n 's/.*"status"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')"
[ "$status" = 'observed' ] || return 1
printf '%s\n' "$latest"
return 0
done <<<"$records"
return 1
}
stale=''
unverified=''
while read -r path version; do
if ! latest="$(latest_for "$path")"; then
unverified+=" $path $version (not covered by the dependency facts)"$'\n'
continue
fi
if behind "$version" "$latest"; then
stale+=" $path $version (latest observed: $latest)"$'\n'
elif [ "$version" != "$latest" ]; then
unverified+=" $path $version (pin is newer than the latest observation $latest)"$'\n'
fi
done <<<"$pins"
if [ -n "$stale" ]; then
echo 'These go.mod pins are behind the latest observed releases:' >&2
printf '%s' "$stale" >&2
exit 1
fi
if [ -n "$unverified" ]; then
echo 'These go.mod pins could not be checked against the dependency facts:' >&2
printf '%s' "$unverified" >&2
exit 1
fi
echo "ok: $count go.mod requirements match the latest releases observed by the execution profile"
echo "source: $source, observed at $observed_at (module_path_latest)"
if [ -n "$digest" ]; then
echo "note: dependency-facts manifest digest: $digest"
fi