#!/usr/bin/env bash # Freshness of the EXTERNAL dependencies (crates.io) declared by the project. `cargo outdated` compares the # Cargo.lock version to the latest published one, but not the requirement written in Cargo.toml: a # requirement `os_info = "3.8"` with a lock up to date at 3.15 goes unnoticed, and a consumer of the # crate may end up with a version seven minor versions old. # # For each direct dependency `name = "x.y"` or `name = { version = "x.y", … }` (excluding `path`, `git`, # `registry`, `workspace`), in [dependencies], [dev-dependencies], [build-dependencies], their # `target.*` variants and [workspace.dependencies], it checks: # 1. the requirement accepts the latest published version (for a 0.x, `0.5` does not accept 0.6); # 2. for a version ≥ 1, the required minor version is not behind the latest # (`tokio = "1.35"` while 1.52 exists). A 0.x is not checked at this level: its # patch number does not count; # 3. Cargo.lock is on the latest version that the requirement accepts. # Requirements that are not written `x`, `x.y` or `x.y.z` (`=1.2.3`, `~1.2`, `>=1`, ranges) are # explicit choices: ignored. # # deps-check-external.sh check (read-only, except for the network) # deps-check-external.sh bump raises the outdated requirements then `cargo update -p` # # Variables: DEPS_CHECK_EXTERNAL=0 disables the check (workspace too large to be kept up # to date all at once); DEPS_CHECK_EXTERNAL_IGNORE="crate1 crate2" sets crates aside (to be justified in # the Makefile: for example a public dependency that must move up with the neighbouring crates); CRATES_IO_INDEX (default https://index.crates.io); DEPS_CHECK_OFFLINE=1 ignores # the absence of network (warning). # shellcheck disable=SC2154 # manifests comes from deps-lib.sh set -euo pipefail . "$(dirname "$0")/common.sh" . "$(dirname "$0")/deps-lib.sh" mode="${1:-check}" case "$mode" in check | bump) ;; *) err "usage: deps-check-external.sh [check|bump]" exit 2 ;; esac if [ "${DEPS_CHECK_EXTERNAL:-1}" = 0 ]; then info " (external dependencies: check disabled by DEPS_CHECK_EXTERNAL=0)" exit 0 fi [ -f "$CARGO_TOML" ] || exit 0 crates_index="${CRATES_IO_INDEX:-https://index.crates.io}" crates_index="${crates_index%/}" fail=0 bad() { err " ✗ $*" fail=1 } # External declarations of a manifest: "keycraterequirement" (the crate differs from the key # when the dependency is renamed by `package = "…"`). declarations() { awk ' /^\[/ { dep = ($0 ~ /^\[(workspace\.|target\..*\.)?(dev-|build-)?dependencies\]$/); next } !dep { next } /^[ \t]*#/ { next } { if (match($0, /^[A-Za-z0-9_-]+[ \t]*=/) == 0) next key = $0; sub(/[ \t]*=.*/, "", key) rest = $0; sub(/^[A-Za-z0-9_-]+[ \t]*=[ \t]*/, "", rest) crate = key if (rest ~ /^"/) { req = rest; sub(/^"/, "", req); sub(/".*/, "", req) } else if (rest ~ /^\{/) { if (rest ~ /(path|git|registry|workspace)[ \t]*=/) next if (match(rest, /version[ \t]*=[ \t]*"[^"]*"/) == 0) next req = substr(rest, RSTART, RLENGTH) sub(/^version[ \t]*=[ \t]*"/, "", req); sub(/"$/, "", req) if (match(rest, /package[ \t]*=[ \t]*"[^"]*"/)) { crate = substr(rest, RSTART, RLENGTH) sub(/^package[ \t]*=[ \t]*"/, "", crate); sub(/"$/, "", crate) } } else next print key "\t" crate "\t" req } ' "$1" } # Replaces the requirement of the dependency `key` (in a dependencies section) of manifest $1 with $3. rewrite_req() { awk -v n="$2" -v r="$3" ' /^\[/ { dep = ($0 ~ /dependencies\]$/) } dep && $0 ~ "^" n "[ \t]*=" { if ($0 ~ /\{/) sub(/version[ \t]*=[ \t]*"[^"]*"/, "version = \"" r "\"") else sub(/"[^"]*"/, "\"" r "\"") } { print } ' "$1" >"$1.tmp" && mv "$1.tmp" "$1" } # Requirement to write for version $1: x.y (≥ 1) or 0.y. floor_for() { echo "$1" | cut -d. -f1,2; } tmp=$(mktemp -d) trap 'rm -r "$tmp"' EXIT info "→ Freshness of the external dependencies (crates.io)..." count=0 offline=0 updates="" for manifest in "${manifests[@]}"; do while IFS=$'\t' read -r key crate req; do [ -n "$crate" ] || continue # Dependency deliberately set aside (DEPS_CHECK_EXTERNAL_IGNORE, with its reason in the Makefile). case " ${DEPS_CHECK_EXTERNAL_IGNORE:-} " in *" $crate "*) continue ;; esac # Explicit requirement (=, ~, >=, range): a choice, not checked (and no network request). case "${req#^}" in '' | *[!0-9.]*) continue ;; esac count=$((count + 1)) if [ ! -f "$tmp/$crate" ]; then rc=0 found=$(latest_published "$crate" "$crates_index") || rc=$? printf '%s %s\n' "$rc" "$found" >"$tmp/$crate" fi read -r rc latest <"$tmp/$crate" if [ "$rc" -eq 2 ]; then offline=1 continue fi if [ "$rc" -eq 1 ] || [ -z "$latest" ]; then warn " ⚠ $crate ($manifest): not found on crates.io, not checked" continue fi acc=0 req_accepts "$req" "$latest" || acc=$? if [ "$acc" -eq 1 ]; then # Newer than the latest published (pre-release, yank): nothing to raise. [ "$(printf '%s\n%s\n' "$req" "$latest" | sort -V | tail -n 1)" = "$latest" ] || continue if [ "$mode" = bump ]; then new=$(floor_for "$latest") rewrite_req "$manifest" "$key" "$new" info " $crate ($manifest): \"$req\" → \"$new\" (latest version: $latest)" updates+="$crate@$latest " else bad "$crate ($manifest): the requirement \"$req\" does not accept the latest version $latest → \"$(floor_for "$latest")\" (make deps-bump)" fi continue fi # The requirement accepts the latest version: is the required minor (≥ 1.0) behind? IFS=. read -r rmaj rmin _ <<<"${req#^}" IFS=. read -r _ lmin _ <<<"$latest" if [ "$rmaj" != 0 ] && [ -n "$rmin" ] && [ "$rmin" -lt "$lmin" ]; then if [ "$mode" = bump ]; then new=$(floor_for "$latest") rewrite_req "$manifest" "$key" "$new" info " $crate ($manifest): \"$req\" → \"$new\" (latest version: $latest)" updates+="$crate@$latest " else bad "$crate ($manifest): the requirement \"$req\" is behind the latest version $latest → \"$(floor_for "$latest")\" (make deps-bump)" fi fi # Cargo.lock: on the latest version that the requirement accepts. if [ -f Cargo.lock ]; then locked=$(lock_entries | awk -v n="$crate" '$1 == n && index($3, "crates.io-index") { print $2 }' | sort -V | tail -n 1) if [ -n "$locked" ] && [ "$locked" != "$latest" ] && [ "$(printf '%s\n%s\n' "$locked" "$latest" | sort -V | tail -n 1)" = "$latest" ]; then if [ "$mode" = bump ]; then updates+="$crate@$latest " else bad "$crate: Cargo.lock is on $locked, the latest version is $latest → cargo update $(lock_update_args "$crate" "$latest" crates.io-index) (make deps-bump)" fi fi fi done < <(declarations "$manifest") done if [ "$offline" -eq 1 ]; then if [ "${DEPS_CHECK_OFFLINE:-}" = 1 ]; then warn " ⚠ crates.io unreachable: freshness of the external dependencies not verified (DEPS_CHECK_OFFLINE=1)" else bad "crates.io unreachable ($crates_index): freshness cannot be verified (DEPS_CHECK_OFFLINE=1 to ignore)" fi fi if [ "$mode" = bump ] && [ -n "$updates" ]; then # shellcheck disable=SC2086 # list of words lock_update crates.io-index $updates fi if [ "$fail" -ne 0 ]; then err "✗ External dependencies out of date (see above)" exit 1 fi ok "✓ External dependencies up to date ($count checked)"