#!/usr/bin/env bash # NODE AND PNPM FOR THE MAC LANES — PINNED, VERIFIED, SELF-PROVISIONED. # # bash scripts/ci/setup-pnpm.sh # # Puts a `node` of the pinned version and the `pnpm` that `package.json`'s # `packageManager` names on `$GITHUB_PATH` (and on THIS shell's PATH, so the # version lines at the bottom run), so every `pnpm …` step after it in the same # job resolves. Run by `verify-main.yml`'s `ios` job (the one Mac job that reads # node_modules) BEFORE its `pnpm install`; the `swift` job runs nothing under # node and does not call this. `setup-sccache.sh`'s shape, for the same reason # it has that shape: ONE copy of a pinned download with its published checksum # beside it. # # WHY A MAC LANE HAS TO PROVISION ITS OWN NODE. The Linux CI box carries node # 22 and pnpm (corepack) on the runner user's PATH, put there by the tinmen # repo's provisioning, and `verify-main.yml` says so at the top. The Macs are not # provisioned by anything: the runner is `CottonDB.app/Contents/Helpers/ # forgejo-runner`, supervised by the app, and a job's environment is the LOGIN # environment (`cotton-client/src/ci/runner.rs` — `env_clear()` plus what the # login shell exports). That PATH is `/opt/homebrew/bin:/usr/local/bin:/usr/ # bin:/bin:…` — it carries git, and it carries node and pnpm only on a Mac where # somebody ran `brew install` by hand. Every Cotton PR before #880 was docs-only, # so the `ios` lane had never reached its `pnpm install` on any Mac; the first # one that did (run 26) died `pnpm: command not found`, and the fix was a hand # `brew install node@22 pnpm` on three machines — undocumented, and gone on the # fourth. This script is the durable form: the lane fetches what it needs into # its own HOME, once per machine, and the runbook's PATH contract # (the procedure “Hold a repository's bytes in Cotton, and move its origin off the git host” § "What a job's PATH is") stops depending on # who last sat at the keyboard. # # TWO PINS, TWO SOURCES OF TRUTH, NEITHER RESTATED HERE. # - pnpm's version is READ from `package.json`'s `packageManager`, because a # pnpm pinned twice is a pnpm that drifts: the install on the Linux box # honours that field through corepack, and this script honours the same # field. Only its checksum lives here, keyed by the version it was taken # for — a bumped `packageManager` with a stale checksum FAILS rather than # fetching something this file has never checked. # - node's exact version is pinned here, and its MAJOR is held equal to # `.nvmrc` by `scripts/__tests__/setup-pnpm.test.ts`; `.nvmrc` names a major # only (`22`) because that is what nvm reads, and a lane needs an exact # asset to hash. # # WHAT "USABLE" MEANS: the exact pinned versions, nothing looser. A Homebrew # `node@22` on a developer's Mac at some other 22.x is stepped over rather than # used, exactly as `setup-sccache.sh` steps over a Homebrew sccache: a lane # whose node changed because somebody ran `brew upgrade` is a lane whose green # means less. On the Linux box both pins match what is installed, so this # script is a no-op there (and is not run there anyway). # # HOW THE TWO ARE VERIFIED. # - node: the sha256 published in `https://nodejs.org/dist/v/SHASUMS256.txt` # beside the asset, checked against a fresh download before it was written # down. # - pnpm: the npm registry's own `dist.integrity` for the package tarball # (`sha512-…`, base64), written here as the same digest in hex so that # `shasum -a 512 -c -` can read it. Decode the registry's base64 and it is # this string. The npm tarball rather than the `pnpm-macos-arm64` standalone # binary because the standalone bundles its own node and a lane running two # nodes is a lane whose `node -e` and `pnpm` disagree about `process.version`. # # A hash that does not match is a hard failure, never a fallback. set -euo pipefail # A hand run outside CI has neither file; `set -u` would die on the redirect # rather than on anything this script is about. GITHUB_ENV="${GITHUB_ENV:-/dev/null}" GITHUB_PATH="${GITHUB_PATH:-/dev/null}" repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" node_version=22.23.1 # `packageManager` is `pnpm@` (a `+sha…` suffix, if one is ever added, # is dropped: the checksum table below is this script's own). `node` may not be # on PATH yet — that is the situation this script exists for — so the field is # read with sed, not with node. pnpm_version="$(sed -nE 's/^[[:space:]]*"packageManager":[[:space:]]*"pnpm@([0-9][^+"]*)(\+[^"]*)?".*$/\1/p' "$repo_root/package.json")" if [ -z "$pnpm_version" ]; then echo "::error::scripts/ci/setup-pnpm.sh: package.json has no \`\"packageManager\": \"pnpm@\"\` — that field is the pin this script honours." >&2 exit 1 fi # ONE CHECKSUM PER PINNED PNPM. A version this table does not name is refused # with the sentence, because fetching an unchecked tarball is not a fallback. case "$pnpm_version" in 10.11.0) pnpm_sha512=6540583f41cc5f628eb3d9773ecee802f4f9ef9923cc45b69890fb47991d4b092964694ec3a4f738a420c918a333062c8b925d312f42e4f0c263eb603551f977 ;; *) echo "::error::scripts/ci/setup-pnpm.sh has no checksum for pnpm $pnpm_version (package.json's packageManager). Add the registry's dist.integrity for that version, in hex, to the table in this script before bumping the pin — an unchecked download is not a fallback." >&2 exit 1 ;; esac # ONE NODE ASSET PER LANE. The platform is read rather than passed, so a job # that calls this script gets the right binary by running on the right machine. case "$(uname -s)/$(uname -m)" in Darwin/arm64) # THE CHECK MACS. Apple silicon only — every machine carrying # `cotton-checks-mac` is M-series. node_asset="node-v$node_version-darwin-arm64" node_sha256=ef28d8fab2c0e4314522d4bb1b7173270aa3937e93b92cb7de79c112ac1fa953 ;; Linux/x86_64) # The Linux box is provisioned and never takes the fetch branch; the pin # is here so a hand run there answers "already the pinned version" rather # than "no asset for this platform". node_asset="node-v$node_version-linux-x64" node_sha256=7a8cb04b4a1df4eaf432125324b81b29a088e73570a23259a8de1c65d07fc129 ;; *) echo "::error::scripts/ci/setup-pnpm.sh has no pinned node $node_version asset for $(uname -s)/$(uname -m). Add the asset and its published sha256 here before wiring a lane onto that machine — an unpinned download is not a fallback." >&2 exit 1 ;; esac root="$HOME/.cache/cottondb/node" bin="$root/bin" node_home="$root/$node_asset" pnpm_home="$root/pnpm-$pnpm_version" checksum() { # macOS ships no `sha256sum`/`sha512sum`. `shasum -a N -c -` reads exactly # the same " " line and exits non-zero on a mismatch, so the hard # failure is the same one on both lanes — there is no path past a bad hash. local bits="$1" sum="$2" file="$3" if command -v "sha${bits}sum" >/dev/null 2>&1; then echo "$sum $file" | "sha${bits}sum" -c - else echo "$sum $file" | shasum -a "$bits" -c - fi } node_usable() { command -v "$1" >/dev/null 2>&1 || return 1 [ "$("$1" --version 2>/dev/null)" = "v$node_version" ] } pnpm_usable() { command -v "$1" >/dev/null 2>&1 || return 1 [ "$("$1" --version 2>/dev/null)" = "$pnpm_version" ] } tmp="" # An `if`, not `[ … ] &&`: under `set -e` a false test as the trap's last # command turns a clean exit 0 into an exit 1. cleanup() { if [ -n "$tmp" ]; then rm -rf "$tmp"; fi; } trap cleanup EXIT # NODE. if node_usable node && pnpm_usable pnpm; then echo "node/pnpm: $(command -v node) is already node $node_version and $(command -v pnpm) is already pnpm $pnpm_version" exit 0 fi # ONE PROVISIONER AT A TIME PER MACHINE. Four jobs start on the Linux box at # once and two `ios`/`swift` jobs can share a Mac, and every one of them runs # this script against the SAME `$root` in the same HOME. Unlocked, two of them # raced `rm -rf "$node_home"` + `tar` (one job's node half-extracted under the # other's feet) and `ln -sfn` (which unlinks and then links, so the second # job's link landed on the first's: `ln: …/node/bin/node: File exists` — # PR #1026's `rust` job, 2026-09-27, one of four rounds a Rust-only change # needed to land). The lock is a directory, the same shape as # `scripts/lib/rustup-lock.sh`, because macOS ships no `flock`; a holder that # died leaves its pid behind and the next comer clears it. A lock with NO pid # file is a holder killed between `mkdir` and writing its pid — the write is # the next statement, so a pidless lock older than `COTTON_SETUP_LOCK_ORPHAN_S` # (30) is a crash's residue and is cleared too; a younger one is a holder # mid-write and is waited on. Without that, a pidless lock held every job on # the machine for the whole wait and then failed them all. NOT sourced from # lib/, deliberately: this script is copied alone into a scratch tree by its # own guard and must keep working there. lock_age_s() { # Seconds since the directory was made; GNU stat and BSD stat spell it apart. local born born="$(stat -c %Y "$1" 2>/dev/null || stat -f %m "$1" 2>/dev/null || echo 0)" echo $(( $(date +%s) - born )) } with_setup_lock() { local lock="$root.lock" wait_s="${COTTON_SETUP_LOCK_WAIT_S:-300}" orphan_s="${COTTON_SETUP_LOCK_ORPHAN_S:-30}" deadline holder status mkdir -p "$(dirname "$lock")" deadline=$(( $(date +%s) + wait_s )) while ! mkdir "$lock" 2>/dev/null; do holder="" # The holder may release between the test and the read: a pid file that # vanished is a lock let go, so the read answers empty and the loop tries # `mkdir` again — under `set -e` a failed `cat` killed the job (run 4387). if [ -f "$lock/pid" ]; then holder="$(cat "$lock/pid" 2>/dev/null)" || holder=""; fi if [ -n "$holder" ] && ! kill -0 "$holder" 2>/dev/null; then rm -rf "$lock" continue fi if [ -z "$holder" ] && [ -d "$lock" ] && [ "$(lock_age_s "$lock")" -ge "$orphan_s" ]; then echo "setup-pnpm.sh: clearing a lock at $lock that has had no owner for ${orphan_s}s — a provisioner died before writing its pid" rm -rf "$lock" continue fi if [ "$(date +%s)" -ge "$deadline" ]; then echo "::error::setup-pnpm.sh: waited ${wait_s}s on ${holder:-an ownerless} lock at $lock — another job's provisioning is stuck; clear the directory by hand." >&2 return 1 fi sleep 1 done echo "$$" >"$lock/pid" "$@" status=$? rm -rf "$lock" return $status } # A link that is REPLACED, never unlinked-then-made: `ln -s` into a private # name, then one `mv -f`, which is a rename and therefore atomic — a second # provisioner that slipped past the lock could only ever see the old link or # the new one, never a gap it then fails to fill. link_into_bin() { local name="$1" target="$2" staged="$bin/.$1.$$" ln -sfn "$target" "$staged" mv -f "$staged" "$bin/$name" } provision() { local staged mkdir -p "$bin" if node_usable "$node_home/bin/node"; then echo "node: the lane's own copy at $node_home is the pinned $node_version" else echo "node: no pinned $node_version on this host — fetching it into $node_home" tmp="${tmp:-$(mktemp -d)}" curl -fsSL --proto '=https' --tlsv1.2 -o "$tmp/node.tar.gz" \ "https://nodejs.org/dist/v$node_version/$node_asset.tar.gz" checksum 256 "$node_sha256" "$tmp/node.tar.gz" # UNPACKED BESIDE, THEN RENAMED INTO PLACE. A `tar` straight into `$node_home` # that a killed job leaves half-done is a `$node_home` whose `bin/node` may # answer the pinned version while `lib/` is missing — usable by the check # above and broken for npm. The tarball unpacks into a private sibling and # ONE `mv` (a rename, atomic) makes it `$node_home`: it is whole or absent. staged="$root/.$node_asset.$$" rm -rf "$staged" mkdir -p "$staged" tar xzf "$tmp/node.tar.gz" -C "$staged" node_usable "$staged/$node_asset/bin/node" rm -rf "$node_home" mv "$staged/$node_asset" "$node_home" rm -rf "$staged" fi # The lane's bin holds ONE node, as a link, so a re-pin replaces it rather than # leaving two versions for PATH order to choose between. link_into_bin node "$node_home/bin/node" link_into_bin npm "$node_home/bin/npm" link_into_bin npx "$node_home/bin/npx" # PNPM — the npm tarball, run by the node above. if [ -f "$pnpm_home/package/bin/pnpm.cjs" ] && \ [ "$("$bin/node" "$pnpm_home/package/bin/pnpm.cjs" --version 2>/dev/null)" = "$pnpm_version" ]; then echo "pnpm: the lane's own copy at $pnpm_home is the pinned $pnpm_version" else echo "pnpm: no pinned $pnpm_version on this host — fetching it into $pnpm_home" tmp="${tmp:-$(mktemp -d)}" curl -fsSL --proto '=https' --tlsv1.2 -o "$tmp/pnpm.tgz" \ "https://registry.npmjs.org/pnpm/-/pnpm-$pnpm_version.tgz" checksum 512 "$pnpm_sha512" "$tmp/pnpm.tgz" # The same shape as node: whole or absent, never half-unpacked at its name. staged="$root/.pnpm-$pnpm_version.$$" rm -rf "$staged" mkdir -p "$staged" tar xzf "$tmp/pnpm.tgz" -C "$staged" [ -f "$staged/package/bin/pnpm.cjs" ] rm -rf "$pnpm_home" mv "$staged" "$pnpm_home" fi # A shim rather than a symlink: `pnpm.cjs`'s shebang is `/usr/bin/env node`, # and the whole point is not to trust what PATH's `node` is. Written whole # into a private name and renamed into place, for the reason `link_into_bin` # gives: a job that reads the shim mid-write runs half a shim. cat > "$bin/.pnpm.$$" <> "$GITHUB_PATH" export PATH="$bin:$PATH" node_usable node || { echo "::error::setup-pnpm.sh: node on PATH is $(node --version 2>&1), not v$node_version" >&2; exit 1; } pnpm_usable pnpm || { echo "::error::setup-pnpm.sh: pnpm on PATH is $(pnpm --version 2>&1), not $pnpm_version" >&2; exit 1; } echo "node/pnpm: node $(node --version) and pnpm $(pnpm --version) at $bin, for the rest of this job" # The coordinator installs the device floor policy before enabling Mac lanes. # Future workflow steps use it even when they invoke cargo/swift/xcodebuild # directly, outside the machine-lock wrapper. if [ "$(uname -s)" = Darwin ] && [ -d "$HOME/.cotton-agent/mac-build-policy/bin" ]; then export PATH="$HOME/.cotton-agent/mac-build-policy/bin:$PATH" [ -z "${GITHUB_PATH:-}" ] || printf '%s\n' "$HOME/.cotton-agent/mac-build-policy/bin" >> "$GITHUB_PATH" fi