#!/usr/bin/env bash # The test runner that survives ONE stale artifact in the shared target # directory, loudly, and never more than once. # # scripts/ci/cargo-test-with-one-retry.sh test [cargo test args…] # scripts/ci/cargo-test-with-one-retry.sh nextest run [nextest args…] # # It wraps whatever cargo subcommand it is given — `cargo test` and # `cargo nextest run` both — because the failure below is a property of the # shared target directory, not of the runner reading it. # # THE ONE FAILURE THIS RETRIES, and nothing else: # # error: test failed, to rerun pass `-p cotton-server --test peers_live_upgrade` # Caused by: could not execute process `…/target/debug/deps/peers_live_upgrade-b3e99d5da8516162` (never executed) # Caused by: No such file or directory (os error 2) # # cargo believed a test binary fresh and the file was gone — the shared cargo # target directory under the runner's HOME, left half-written by a job that was # cancelled mid-cargo (PR #396, runs 1868/1869 on 2026-09-08; the runbook's # "The shared cargo cache after a cancelled run" carries the whole sequence). # `with-cargo-lock.sh` narrows the window; this is what recovers when a stale # artifact gets through anyway. # # THE RULES: # - "could not execute process" AND "No such file or directory" together, or # no retry. A test that FAILED ran; a binary that cannot be executed for any # other reason (a permission bit, a wrong architecture) is a real problem # and must be read, not papered over. # - The offending crate's artifacts are removed first — `cargo clean -p`, # named from cargo's own "to rerun pass" line — so the retry rebuilds # rather than trusting the same fingerprint twice. That removal happens # ONLY under the cargo lock: `with-cargo-lock.sh` puts its path in # COTTON_CARGO_LOCK_HELD, and without it this script refuses to clean and # refuses to retry. Deleting artifacts from a directory another job is # building into is the failure this whole thing exists to prevent, and a # wrapper that did it unlocked would be causing it. # - ONE retry. A second failure is a real failure and exits with its status. # - Everything it does is printed. A green run that quietly repaired itself # teaches nobody anything. set -uo pipefail if [ "${COTTON_PARTITION_BUILD_TARGETS:-0}" = 1 ]; then # Only the dedicated guards-main package jobs opt in. The target planner # calls this same retry boundary with the flag cleared for every phase. unset COTTON_PARTITION_BUILD_TARGETS exec python3 "$(dirname "${BASH_SOURCE[0]}")/cargo-test-target-shard.py" "$@" fi log() { echo "[cargo-retry] $*" >&2; } run_cargo() { cargo "$@" 2>&1 | tee "$LOG" return "${PIPESTATUS[0]}" } LOG="$(mktemp -t cargo-test-retry.XXXXXX)" trap 'rm -f "$LOG"' EXIT run_cargo "$@" status=$? [ "$status" -eq 0 ] && exit 0 # TWO WORDINGS FOR ONE FAULT. cargo says "could not execute process"; nextest, # which execs the binaries itself, says "failed to execute". Either, AND the # io error, or no retry — a test that FAILED ran, and a binary that cannot be # executed for another reason (a permission bit, a wrong architecture) must be # read, not papered over. if ! grep -qE 'could not execute process|failed to execute' "$LOG" || ! grep -q 'No such file or directory' "$LOG"; then log "failed with status $status — a real failure, not a missing test binary. Not retrying." exit "$status" fi if [ -z "${COTTON_CARGO_LOCK_HELD:-}" ]; then log "the shared target directory looks stale, but this is NOT running under" log "scripts/ci/with-cargo-lock.sh — refusing to clean or retry. Removing" log "artifacts beside another job's build is the fault, not the fix." exit "$status" fi # cargo prints "to rerun pass `-p `"; nextest prints no such line, so the # crate stays empty, `cargo clean -p` is skipped and the retry simply re-runs — # which is already what this script does for an unnamed crate. crate="$(sed -n 's/.*to rerun pass `-p \([^ `]*\).*/\1/p' "$LOG" | head -1)" binary="$(sed -n -e 's/.*could not execute process `\([^`]*\)`.*/\1/p' -e 's/.*failed to execute `\([^`]*\)`.*/\1/p' "$LOG" | head -1)" log "cargo could not execute a test binary that it believed was fresh:" log " crate: ${crate:-}" log " binary: ${binary:-}" log "This is the shared cargo cache after a cancelled run — see the procedure “Work with the forge — tokens, the pnpm forge surface, workflows and runs”." if [ -n "$crate" ]; then log "removing the crate's artifacts: cargo clean -p $crate" cargo clean -p "$crate" || log "cargo clean -p $crate exited non-zero — continuing to the retry anyway" fi # Belt for the case where cargo named no crate (or `clean -p` left the uplifted # copy): delete the deps entries for that exact test binary name. Guarded on # `/deps/` so a malformed line can never turn this into an rm of anything else. case "$binary" in */deps/*) stem="${binary%-*}" log "removing the stale deps entries: ${stem}-*" rm -f "$stem"-* ;; esac log "RETRYING ONCE. If this fails, it is a real failure." run_cargo "$@" status=$? if [ "$status" -ne 0 ]; then log "the retry failed too (status $status) — a real failure. No further retry." fi exit "$status"