Cotton code
119 lines · 4.7 KB · TypeScriptRaw
/** * A SHELL WITH NO `rustc` IS A PATH PROBLEM, NOT A MISSING TOOLCHAIN. * * rustup installs into `~/.cargo/bin` and appends a line to `~/.zshrc` and * `~/.profile`. A login shell reads that line; a non-interactive one does not — * and every way heavy work is really started here is the second kind: an * agent's tool call, a `bash -c`, a `with-build-lock.sh` child, a LaunchAgent. * On 16 Sep 2026 the first attempt at the 0.4.7 Mac release died in TWELVE * SECONDS with `cargo: command not found`, after the lane had already taken the * machine-wide build lock. * * `scripts/lib/cargo-path.sh` is the fix and this runs it, for real, with a * stubbed PATH and a stubbed HOME — the same shape as * `build-xcframework-rustup.test.ts` beside it, and for the same reason: the * scripts that call it cannot be run end to end without a Mac. */import { spawnSync } from "node:child_process";import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterAll, describe, expect, it } from "vitest";const ROOT = join(__dirname, "..", "..");const SNIPPET = join(ROOT, "scripts", "lib", "cargo-path.sh");const XCFRAMEWORK = join(ROOT, "scripts", "build-xcframework.sh");const temps: string[] = [];afterAll(() => {  for (const dir of temps.splice(0)) rmSync(dir, { recursive: true, force: true });});function read(path: string): string {  expect(existsSync(path), `${path} is missing — this guard reads it by path`).toBe(true);  return readFileSync(path, "utf8");}interface Mac {  /** Is there a `rustc` on the PATH the caller already has? */  onPath?: boolean;  /** Is there one under `$HOME/.cargo/bin`, where rustup puts it? */  inCargoHome?: boolean;}interface Run {  status: number;  stdout: string;  stderr: string;  out: string;}function stub(dir: string, name: string, body: string): void {  mkdirSync(dir, { recursive: true });  writeFileSync(join(dir, name), `#!/bin/sh\n${body}\n`);  chmodSync(join(dir, name), 0o755);}/** * Source the snippet, call it, then print what `rustc` resolves to afterwards. * * PATH is built from scratch — `/usr/bin:/bin` plus whatever this Mac is * supposed to have — so the developer's own `~/.cargo/bin` cannot answer the * question for the test. */function ensure(mac: Mac): Run {  const home = mkdtempSync(join(tmpdir(), "cargo-path-"));  temps.push(home);  const onPathDir = join(home, "bin");  mkdirSync(onPathDir, { recursive: true });  if (mac.onPath) stub(onPathDir, "rustc", "echo 'rustc 1.90.0 (the one already on PATH)'");  if (mac.inCargoHome) stub(join(home, ".cargo", "bin"), "rustc", "echo 'rustc 1.90.0 (rustup)'");  const line = `set -euo pipefail; . ${JSON.stringify(SNIPPET)}; ensure_cargo_on_path; command -v rustc`;  const result = spawnSync("/bin/bash", ["-c", line], {    encoding: "utf8",    env: { PATH: `${onPathDir}:/usr/bin:/bin`, HOME: home },  });  const stdout = result.stdout ?? "";  const stderr = result.stderr ?? "";  return { status: result.status ?? -1, stdout, stderr, out: `${stdout}\n${stderr}` };}describe("ensure_cargo_on_path", () => {  it("prepends ~/.cargo/bin when the shell cannot see rustc, and says so on stderr", () => {    const run = ensure({ onPath: false, inCargoHome: true });    expect(run.status, run.out).toBe(0);    expect(run.stdout.trim()).toMatch(/\.cargo\/bin\/rustc$/);    // Never a silent repair: a PATH that is wrong here is usually wrong for    // whatever the person runs next.    expect(run.stderr).toContain("no rustc on PATH");    expect(run.stderr).toContain(".cargo/bin");  });  it("leaves a shell that already resolves rustc completely alone", () => {    // This is why a lane can call it unconditionally: CI and every login shell    // pay nothing and hear nothing.    const run = ensure({ onPath: true, inCargoHome: true });    expect(run.status, run.out).toBe(0);    expect(run.stdout.trim()).toMatch(/\/bin\/rustc$/);    expect(run.stdout.trim()).not.toContain(".cargo");    expect(run.stderr).toBe("");  });  it("refuses, naming rustup, when the Mac really has no toolchain", () => {    const run = ensure({ onPath: false, inCargoHome: false });    expect(run.status).not.toBe(0);    expect(run.stderr).toContain("no Rust toolchain");    expect(run.stderr).toContain("rustup.rs");  });});describe("the lanes that need a compiler ask first", () => {  it("the XCFramework build asks before it looks for rustup — which lives there too", () => {    const body = read(XCFRAMEWORK);    expect(body).toContain("scripts/lib/cargo-path.sh");    expect(body.indexOf("ensure_cargo_on_path")).toBeLessThan(body.indexOf("require_rust_targets"));  });});