Cotton code
179 lines · 7.5 KB · TypeScriptRaw
// A rustup that cannot ANSWER is not a toolchain answering "no".//// `scripts/build-xcframework.sh` used to check its two iOS triples with// `rustup target list --installed 2>/dev/null`. When rustup cannot resolve a// toolchain at the repo root — what the build Mac hit, worked around with// `RUSTUP_TOOLCHAIN` in PR #430 — that command exits non-zero, prints its// reason on the stderr the redirect throws away, and produces an empty list.// No line matches, so the script told the reader to `rustup target add` two// targets that were already installed, and rustup's own explanation was gone.//// And a rustup that answers SHORT is not a toolchain answering "no" either.// Measured on run #158: a Mac ran the `ios` job's// `rustup target add aarch64-apple-ios aarch64-apple-ios-sim` while the `swift`// job listed the installed targets against the same `~/.rustup`. rustup builds// that list by reading `lib/rustlib`, so the listing came back exit 0 with a// PARTIAL list and the guard failed the job over `aarch64-apple-darwin` — the// machine's own host triple, installed for months. A miss is therefore RE-READ// before it is believed, and every rustup call takes// `scripts/lib/rustup-lock.sh`.//// The check lives in `scripts/lib/rust-targets.sh` so it can be sourced and// run here with a stub `rustup` on PATH — the script itself cannot be run end// to end without a Mac (it takes the build lock, runs the disk guard, cargo and// xcodebuild).import { spawnSync } from "node:child_process";import {  chmodSync,  existsSync,  mkdtempSync,  readFileSync,  writeFileSync,} from "node:fs";import { tmpdir } from "node:os";import { dirname, join, resolve } from "node:path";import { fileURLToPath } from "node:url";import { beforeAll, describe, expect, it } from "vitest";const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");const SCRIPT = join(REPO_ROOT, "scripts", "build-xcframework.sh");const SNIPPET = join(REPO_ROOT, "scripts", "lib", "rust-targets.sh");const LOCK = join(REPO_ROOT, "scripts", "lib", "rustup-lock.sh");/** Read a script this guard is about, failing by NAME when it has moved. */function readScript(path: string): string {  expect(    existsSync(path),    `${path} is missing — this guard reads it by path`  ).toBe(true);  return readFileSync(path, "utf8");}let stubDir: string;/** Put a `rustup` on PATH that behaves as told, and run the check under it. */function checkWith(  rustup: string,  triples: string[]): { status: number; stderr: string } {  writeFileSync(join(stubDir, "rustup"), rustup);  chmodSync(join(stubDir, "rustup"), 0o755);  // `set -euo pipefail` as the real script has it: the check has to explain  // itself rather than being killed by a failing command substitution.  const line = `set -euo pipefail; RUST_TARGETS_LOG_PREFIX='[xcframework]'; . '${SNIPPET}'; require_rust_targets ${triples.join(" ")}`;  // `spawnSync`, not `execFileSync`: on the SUCCESS path execFileSync returns  // stdout, so a helper that called the result `stderr` could never see a line  // the check wrote while passing — and "it re-read, then passed" is exactly  // such a line.  const result = spawnSync("/bin/bash", ["-c", line], {    encoding: "utf8",    env: {      ...process.env,      PATH: `${stubDir}:${process.env.PATH ?? ""}`,      // The lock's real home is `~/.rustup.lock`; a guard never reaches for      // it, and the re-read sleeps are collapsed so a failing case costs      // milliseconds rather than the seconds a real lane can afford.      COTTON_RUSTUP_LOCK: join(stubDir, "rustup.lock"),      RUST_TARGETS_RETRY_SLEEP_S: "0",    },  });  return { status: result.status ?? -1, stderr: result.stderr ?? "" };}beforeAll(() => {  stubDir = mkdtempSync(join(tmpdir(), "xcframework-rustup-"));});describe("the iOS target check", () => {  it("keeps rustup's stderr instead of discarding it", () => {    // The shape half: the old `2>/dev/null` is what made the failure silent, so    // no rustup call in either file may carry it again.    for (const path of [SCRIPT, SNIPPET, LOCK]) {      for (const line of readScript(path).split("\n")) {        if (line.trimStart().startsWith("#")) continue; // a comment may quote the old form        if (line.includes("rustup ")) expect(line).not.toContain("2>/dev/null");      }    }  });  it("is the check the build script actually runs", () => {    // Otherwise this guard tests a file nothing sources.    const script = readScript(SCRIPT);    expect(script).toContain("scripts/lib/rust-targets.sh");    expect(script).toContain("require_rust_targets");  });  it("says rustup could not answer, quotes its line, and names RUSTUP_TOOLCHAIN", () => {    const { status, stderr } = checkWith(      "#!/bin/sh\necho 'error: toolchain is not installed' >&2\nexit 1\n",      ["aarch64-apple-ios", "aarch64-apple-ios-sim"]    );    expect(status).toBe(1);    expect(stderr).toContain("rustup could not answer");    expect(stderr).toContain("error: toolchain is not installed");    expect(stderr).toContain("RUSTUP_TOOLCHAIN=<name>");    // And NOT the answer it does not have: the targets may well be installed.    expect(stderr).not.toContain("the active toolchain has no");  });  it("still says to add the targets when rustup answered and they are absent", () => {    const { status, stderr } = checkWith(      "#!/bin/sh\necho x86_64-unknown-linux-gnu\n",      ["aarch64-apple-ios"]    );    expect(status).toBe(1);    expect(stderr).toContain("the active toolchain has no aarch64-apple-ios");    expect(stderr).not.toContain("rustup could not answer");  });  it("names the triples that are MISSING, not a fixed pair", () => {    // The old remedy read `rustup target add aarch64-apple-ios    // aarch64-apple-ios-sim` whatever was absent, which on the Mac lane meant    // being told to install two iOS targets over a missing host triple.    const { status, stderr } = checkWith(      "#!/bin/sh\necho aarch64-apple-ios\n",      ["aarch64-apple-ios", "aarch64-apple-darwin"]    );    expect(status).toBe(1);    expect(stderr).toContain("rustup target add aarch64-apple-darwin");    // and does not name one that IS installed    expect(stderr).not.toContain("add aarch64-apple-ios ");  });  it("RE-READS a miss before believing it, because a torn list exits 0", () => {    // Run #158's failure, reproduced: the first listing is short (a    // `rustup target add` was writing `lib/rustlib` beside it), the second is    // whole. The old check failed the job on the first.    const counter = join(stubDir, "calls");    const { status, stderr } = checkWith(      `#!/bin/shn=$(cat ${counter} 2>/dev/null || echo 0)echo $((n+1)) > ${counter}if [ "$n" = "0" ]; then echo aarch64-apple-ios; else echo aarch64-apple-darwin; echo aarch64-apple-ios; fi`,      ["aarch64-apple-darwin", "aarch64-apple-ios"]    );    expect(status).toBe(0);    expect(stderr).toContain("asking again (1 of");    expect(readFileSync(counter, "utf8").trim()).toBe("2");  });  it("gives up after the re-reads rather than waiting for ever", () => {    const { status, stderr } = checkWith(      "#!/bin/sh\necho aarch64-apple-ios\n",      ["aarch64-apple-darwin"]    );    expect(status).toBe(1);    expect(stderr).toContain("after 3 re-reads");  });  it("passes silently when rustup lists both triples", () => {    const { status, stderr } = checkWith(      "#!/bin/sh\necho aarch64-apple-ios\necho aarch64-apple-ios-sim\n",      ["aarch64-apple-ios", "aarch64-apple-ios-sim"]    );    expect(status).toBe(0);    expect(stderr).toBe("");  });});