diff --git a/src/build/pods.ts b/src/build/pods.ts index 4f4945367..9693cc3ee 100644 --- a/src/build/pods.ts +++ b/src/build/pods.ts @@ -289,12 +289,14 @@ export const STEP_WRAPPER = `${COPY_SOURCE}; t=$1; shift; exec timeout -k 10 "$t /** * A step that reads the compile cache: rustc runs through sccache, and the cache's hit counts end the step's output. sccache * keys a compile on every CARGO_* variable, and the CI gate builds with CARGO_TERM_COLOR set; the wrapper gives that value to - * rustc alone, because cargo would colour the step's output and artifacts with it. An image without sccache builds uncached. + * rustc alone, because cargo would colour the step's output and artifacts with it. An image without sccache or bash builds uncached. The wrapper is a bash script because dash drops environment variables whose names contain a hyphen, such as CARGO_BIN_EXE_. */ export const CACHE_STEP_WRAPPER = [ COPY_SOURCE, "if command -v sccache >/dev/null 2>&1; then", - ` printf '%s\\n' '#!/bin/sh' 'exec env "CARGO_TERM_COLOR=$AUTOCRAFT_CI_TERM_COLOR" sccache "$@"' > /tmp/.rustc-wrapper && chmod 755 /tmp/.rustc-wrapper && export RUSTC_WRAPPER=/tmp/.rustc-wrapper`, + ' if b=$(command -v bash); then', + ` printf '%s\\n' "#!$b" 'exec env "CARGO_TERM_COLOR=$AUTOCRAFT_CI_TERM_COLOR" sccache "$@"' > /tmp/.rustc-wrapper && chmod 755 /tmp/.rustc-wrapper && export RUSTC_WRAPPER=/tmp/.rustc-wrapper`, + ' else echo "the toolchain image has no bash: the compile cache wrapper cannot run, so this step builds without the compile cache" >&2; fi', 'else echo "the toolchain image has no sccache: this step builds without the compile cache" >&2; fi', 't=$1; shift; timeout -k 10 "$t" "$@"; s=$?', 'if [ -n "${RUSTC_WRAPPER:-}" ]; then echo "compile cache (sccache, read-only):"; timeout 60 sccache --show-stats; fi', @@ -303,14 +305,17 @@ export const CACHE_STEP_WRAPPER = [ /** * A warm producer's build step: as CACHE_STEP_WRAPPER, with compile-cache writes. It records `rustc -vV` for the warm * manifest. The step runs with CARGO_NET_OFFLINE (the gate's warm-command does not say --offline); the rustc wrapper drops - * it, because sccache keys on CARGO_* variables and the CI runners whose entries the build shares do not set it. + * it, because sccache keys on CARGO_* variables and the CI runners whose entries the build shares do not set it. The wrapper is a + * bash script because dash drops environment variables whose names contain a hyphen, such as CARGO_BIN_EXE_. */ export const WARM_STEP_WRAPPER = [ COPY_SOURCE, "rustc -vV > /meta/rustc.txt 2>/dev/null || true", 'if [ -z "${SCCACHE_WEBDAV_ENDPOINT:-}" ]; then echo "the CI gate names no cache version: this step builds without the compile cache" >&2', "elif command -v sccache >/dev/null 2>&1; then", - ` printf '%s\n' '#!/bin/sh' 'exec env -u CARGO_NET_OFFLINE "CARGO_TERM_COLOR=$AUTOCRAFT_CI_TERM_COLOR" sccache "$@"' > /tmp/.rustc-wrapper && chmod 755 /tmp/.rustc-wrapper && export RUSTC_WRAPPER=/tmp/.rustc-wrapper`, + ' if b=$(command -v bash); then', + ` printf '%s\n' "#!$b" 'exec env -u CARGO_NET_OFFLINE "CARGO_TERM_COLOR=$AUTOCRAFT_CI_TERM_COLOR" sccache "$@"' > /tmp/.rustc-wrapper && chmod 755 /tmp/.rustc-wrapper && export RUSTC_WRAPPER=/tmp/.rustc-wrapper`, + ' else echo "the toolchain image has no bash: the compile cache wrapper cannot run, so this step builds without the compile cache" >&2; fi', 'else echo "the toolchain image has no sccache: this step builds without the compile cache" >&2; fi', 't=$1; shift; timeout -k 10 "$t" "$@"; s=$?', 'if [ -n "${RUSTC_WRAPPER:-}" ]; then echo "compile cache (sccache, read-write):"; timeout 60 sccache --show-stats; fi', diff --git a/test/rustc-wrapper.test.ts b/test/rustc-wrapper.test.ts new file mode 100644 index 000000000..c7e7be2e9 --- /dev/null +++ b/test/rustc-wrapper.test.ts @@ -0,0 +1,28 @@ +// The rustc wrapper the cache steps write must pass every variable cargo sets, including names with a hyphen +// (CARGO_BIN_EXE_), which dash drops before it execs. +import assert from "node:assert/strict"; +import { execFileSync } from "node:child_process"; +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { test } from "node:test"; +import { CACHE_STEP_WRAPPER, WARM_STEP_WRAPPER } from "../src/build/pods.ts"; + +for (const [name, script] of [["cache", CACHE_STEP_WRAPPER], ["warm", WARM_STEP_WRAPPER]] as const) { + test(`${name} step wrapper passes hyphenated environment variables to sccache`, () => { + const dir = mkdtempSync(join(tmpdir(), "rustc-wrapper-")); + try { + writeFileSync(join(dir, "sccache"), '#!/bin/bash\n[ "$1" = --show-stats ] || env\n'); + chmodSync(join(dir, "sccache"), 0o755); + // The first line copies the source tree from a pod mount; the rest is the wrapper under test. + const body = script.split("\n").slice(1).join("\n"); + const out = execFileSync("sh", ["-c", body, "step", "30", "sh", "-c", 'exec env CARGO_BIN_EXE_my-bin=x "$RUSTC_WRAPPER" rustc'], { + env: { PATH: `${dir}:${process.env.PATH}`, SCCACHE_WEBDAV_ENDPOINT: "http://cache.invalid", AUTOCRAFT_CI_TERM_COLOR: "always" }, + encoding: "utf8", + }); + assert.match(out, /^CARGO_BIN_EXE_my-bin=x$/m); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); +}