Cotton code
146 lines · 7.2 KB · ShellRaw
#!/usr/bin/env bash# THE RUSTC WRAPPER, which is not sccache itself.##   path="$(bash scripts/ci/sccache-shim.sh <real-sccache> <shim-dir>)"#   export RUSTC_WRAPPER="$path"## Writes `<shim-dir>/sccache` — three lines that `unset CARGO_TARGET_DIR` and# `exec` the real binary — and prints its path. Nothing else.## WHY IT EXISTS. sccache's Rust cache key hashes EVERY environment variable# whose name begins with `CARGO_`, with four exceptions and only four:# `CARGO_MAKEFLAGS`, `CARGO_REGISTRIES_*`, `CARGO_BUILD_JOBS` and# `CARGO_ENCODED_RUSTFLAGS` (`src/compiler/rust.rs`, `generate_hash_key`,# v0.17.0). `CARGO_TARGET_DIR` is not one of them, and both places Cotton wraps# rustc set it to a DIFFERENT directory every time:##   the CI lane   one target directory per head BRANCH (#404, and it must stay#                 that way) — run 181 ended `Cache hits rate (Rust) 0.00 %` over#                 1,419 compilations, cumulative, with 41 GB of cache on disk#   rust-verify   one scratch per WORKTREE, and a session is a worktree, so the#                 expensive case — a task's first `pnpm rust:verify` — was the#                 one case the cache could never serve## It reads as "the cache is cold" and it never warms. That is why it survived a# month: every symptom of a busted key looks exactly like a first run.## The irony is that sccache goes out of its way to be target-directory# independent — `--out-dir`, `-L` and `--extern` are all EXCLUDED from the# argument hash, and externs are hashed by file CONTENTS. Measured, same crate,# same source, `CARGO_INCREMENTAL=0`, 2026-09-24:##   cargo build --target-dir A ; --target-dir A again (wiped)      2 hits#   cargo build --target-dir A ; --target-dir B                    2 hits#   CARGO_TARGET_DIR=C cargo build ; CARGO_TARGET_DIR=D            0 hits## The variable, not the directory. And rustc never reads it — cargo does, and by# the time the wrapper runs cargo has already turned it into `--out-dir`. So the# wrapper drops it and every caller keeps exporting it for everything else: the# sweep, the free-disk guard, the `du -sh` at the end of a job, rust-verify's# own budget.## TWO PROPERTIES ARE LOAD-BEARING, and the cargo-cache test# holds both by running this:##   THE FILE STEM STAYS `sccache`. `cc-rs` prefixes the C compiler with#   `$RUSTC_WRAPPER` on its own, but ONLY when that variable's file stem is one#   of `sccache`/`cachepot`/`buildcache`/`kache` (`rustc_wrapper_fallback`,#   src/lib.rs) — which is why the lane's C/C++ and assembler hit rates read#   100% while no `CC` is set anywhere. Call this file `cotton-sccache` and#   SQLite's amalgamation stops being cached, silently.##   IT EXECS THE REAL BINARY BY ABSOLUTE PATH. The shim's directory goes on#   `$PATH` ahead of everything, so a bare `sccache` here finds the shim and#   recurses until the machine runs out of processes.## AND IT IS `bash`, NOT `sh` — MEASURED, THE HARD WAY. A POSIX shell keeps only# environment variables whose names are valid shell identifiers and DROPS the# rest on the way to `exec`. Cargo sets one that is not:# `CARGO_BIN_EXE_cotton-session-host`, which `cotton-session-host`'s integration# test reads with `env!`. With `#!/bin/sh` the lane went red on Linux and stayed# green on the Macs, because Ubuntu's `/bin/sh` is dash and macOS's is bash —# run 190, `error: environment variable CARGO_BIN_EXE_cotton-session-host not# defined at compile time`, from a change that touched no Rust at all.##   /bin/sh (macOS, bash)   the hyphenated variable survives#   dash                    it is GONE#   bash                    it survives#   zsh                     it is GONE## So: bash, and a guard that runs the generated shim with a hyphenated variable# set and fails if the child does not see it. The guard runs on the Linux lane# too, which is the half that would otherwise have to fail as a Rust compile# error three steps later.## WHAT IT DOES NOT FIX, and that is fine. sccache also hashes the compilation's# CWD, and cargo runs each rustc from the PACKAGE's own root: the registry# directory for a dependency (identical everywhere), the checkout for a# workspace crate (a throwaway path on CI, a worktree locally). So the ~500# crates.io dependencies — the whole of a cold build's cost — are cacheable, and# Cotton's own crates are not. Those are the ones that changed anyway.set -euo pipefailreal="${1-}"dir="${2-}"if [ -z "$real" ] || [ -z "$dir" ]; then  echo "usage: sccache-shim.sh <real-sccache-binary> <shim-dir>" >&2  exit 2ficase "$real" in  /*) ;;  *)    # A relative name would be resolved against $PATH at exec time, and the shim    # is FIRST on $PATH. Refuse rather than build a fork bomb.    echo "sccache-shim: the real binary must be an absolute path, not '$real'" >&2    exit 2    ;;esacif [ ! -x "$real" ]; then  echo "sccache-shim: '$real' is not an executable" >&2  exit 2fimkdir -p "$dir"# REPLACED, NEVER REWRITTEN IN PLACE. On the Linux host every cargo job (rust,# rust-lint, android: one per build slot, up to three at once) generates this# same `$HOME/.cache/cottondb/rust/shim/sccache` while the others are running# cargo through it. `cat >` on the live path truncates a file other jobs are# exec'ing hundreds of times a minute: an exec during the write fails with# ETXTBSY ("Text file busy"), and one that lands between the truncate and the# write runs an empty or half-written script, which compiles nothing and exits# 0. So: write a private temp file beside it, and `mv -f` it over the path —# rename(2) is atomic, and a running exec keeps the inode it opened.tmp="$(mktemp "$dir/.sccache.XXXXXX")"trap 'rm -f "$tmp"' EXITcat > "$tmp" <<SHIM#!/usr/bin/env bash# Generated by scripts/ci/sccache-shim.sh — do not edit; it is rewritten each run.# CARGO_TARGET_DIR varies per branch here and sccache hashes every CARGO_*# variable but four, so leaving it set makes every branch a cold cache. rustc# does not read it; cargo already turned it into --out-dir.## BASH, NOT \`sh\`, AND THAT IS NOT A PREFERENCE. See the block comment in the# generator: dash drops every inherited variable whose name is not a valid# shell identifier, and cargo sets \`CARGO_BIN_EXE_cotton-session-host\`.unset CARGO_TARGET_DIR# THE TOOLCHAIN'S OWN LIBRARIES, PUT BACK. On macOS rustc strips a linked# binary's debug info with the toolchain's \`rust-objcopy\`, which loads# \`libLLVM.dylib\` from the toolchain's \`lib\` through the library path the# rustup proxy sets (DYLD_FALLBACK_LIBRARY_PATH). System Integrity Protection# drops every DYLD_* variable on the way into this script (\`/usr/bin/env\`# and bash are protected), so without this every stripped build script and# proc macro warned "stripping debug info with \`rust-objcopy\` failed:# signal: 6 (SIGABRT)" (verify-main run 4496, mac job, 20 Oct 2026) and kept# its debug info. cargo names the toolchain's rustc as the first argument;# Linux ignores the variable.case "\${1-}" in  */bin/rustc) export DYLD_FALLBACK_LIBRARY_PATH="\${1%/bin/rustc}/lib\${DYLD_FALLBACK_LIBRARY_PATH:+:\$DYLD_FALLBACK_LIBRARY_PATH}" ;;esacexec "$real" "\$@"SHIMchmod 0755 "$tmp"mv -f "$tmp" "$dir/sccache"trap - EXITprintf '%s\n' "$dir/sccache"