Cotton code
96 lines · 4.3 KB · ShellRaw
#!/usr/bin/env bash# `require_rust_targets <triple>…` — the toolchain carries these targets, or the# caller stops with a message that says which of the three things went wrong.## THIS IS A SOURCED SNIPPET, NOT A COMMAND. It lives on its own so# `scripts/__tests__/build-xcframework-rustup.test.ts` can run the check with a# stub `rustup` on PATH without running a build.## THE FIRST BUG IT EXISTS FOR: the check used to read `rustup target list# --installed 2>/dev/null`, which throws rustup's own explanation away. When# rustup cannot resolve a toolchain at the repo root — the build Mac hit exactly# that, and PR #430 worked around it with `RUSTUP_TOOLCHAIN` — the command# fails, its output is empty, no line matches, and the reader is told to install# iOS targets that are already installed. A failure to ASK is not an answer of# "no".## THE SECOND, measured on run #158: `rustup target list --installed` can exit 0# with a PARTIAL list. rustup builds it by reading the toolchain's `lib/rustlib`# directory, and two of this workflow's jobs run on one Mac against one# `~/.rustup` — `ios` adding the two iOS triples while `swift` was listing them.# The listing came back short and the guard failed a job over# `aarch64-apple-darwin`, the machine's OWN host triple, which had been# installed for months. A SHORT answer is not a small answer; it is a different# question, and it looks exactly like a real absence.## Two things follow, and the second is why the first is not enough. Every rustup# call here takes `scripts/lib/rustup-lock.sh`, which orders this repository's# readers against its own writers. And a MISS IS RE-READ before it is believed:# a target that is genuinely absent stays absent and costs a few seconds on a# path that is about to fail anyway, while a torn read heals on the next look.# Nothing else can tell those two apart from the outside.# The tag every line carries, so a caller's log reads as one voice.RUST_TARGETS_LOG_PREFIX="${RUST_TARGETS_LOG_PREFIX:-[rust-targets]}"# How many times a miss is re-read, and how long between looks. Small numbers,# because a `rustup target add` of two triples is seconds, not minutes.RUST_TARGETS_RETRIES="${RUST_TARGETS_RETRIES:-3}"RUST_TARGETS_RETRY_SLEEP_S="${RUST_TARGETS_RETRY_SLEEP_S:-2}"# shellcheck source=rustup-lock.sh. "${BASH_SOURCE[0]%/*}/rustup-lock.sh"# Ask rustup once. Prints the list on stdout; returns rustup's own status and# leaves its stderr in the file named by $1, because a failure to ask has to be# quotable._rust_targets_list() {  with_rustup_lock rustup target list --installed 2>"$1"}require_rust_targets() {  local installed="" status=0 stderr_file line triple missing attempt=0  stderr_file="$(mktemp)"  while :; do    status=0    # `|| status=$?` and not a bare assignment: under `set -e` a failing command    # substitution in a plain assignment kills the shell before this can explain    # anything, and the whole point here is the explaining.    installed="$(_rust_targets_list "$stderr_file")" || status=$?    if [ "$status" -ne 0 ]; then      echo "$RUST_TARGETS_LOG_PREFIX rustup could not answer which targets are installed (exit $status) — this says nothing about whether they are." >&2      while IFS= read -r line; do        [ -n "$line" ] || continue        echo "$RUST_TARGETS_LOG_PREFIX   rustup: $line" >&2      done <"$stderr_file"      echo "$RUST_TARGETS_LOG_PREFIX   if no toolchain resolves here, name one: RUSTUP_TOOLCHAIN=<name> $0" >&2      rm -f "$stderr_file"      return 1    fi    missing=()    for triple in "$@"; do      printf '%s\n' "$installed" | grep -qx "$triple" || missing+=("$triple")    done    [ "${#missing[@]}" -eq 0 ] && break    if [ "$attempt" -ge "$RUST_TARGETS_RETRIES" ]; then      break    fi    attempt=$(( attempt + 1 ))    echo "$RUST_TARGETS_LOG_PREFIX ${missing[*]} did not appear in the list — a rustup writing beside us answers 0 with a partial one, so asking again ($attempt of $RUST_TARGETS_RETRIES)." >&2    sleep "$RUST_TARGETS_RETRY_SLEEP_S"  done  rm -f "$stderr_file"  if [ "${#missing[@]}" -ne 0 ]; then    echo "$RUST_TARGETS_LOG_PREFIX the active toolchain has no ${missing[*]} target(s), after $RUST_TARGETS_RETRIES re-reads." >&2    echo "$RUST_TARGETS_LOG_PREFIX   rustup target add ${missing[*]}" >&2    return 1  fi}