Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(cli): the node suites rebuild a Core build older than its sources (#8567)

Christopher Chedeau3139560abed4
diff --git a/.changeset/core-build-freshness.md b/.changeset/core-build-freshness.md
new file mode 100644
index 0000000000..2fd1d1a8f2
--- /dev/null
+++ b/.changeset/core-build-freshness.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[chore] The CLI's test setup rebuilds Core when its build is older than Core's sources or translations. It does so before any test starts, so a reused checkout no longer tests the CLI against an old Core build. A test waiting on another's build reads it only once that build is finished. One checkout's build no longer holds up another's tests. A run that cannot rebuild a stale Core, while an earlier run in the checkout uses it, stops with an error naming that run.
+
+@vjeux
diff --git a/packages/cli/clients/cli/commands/ensure-core-built.mjs b/packages/cli/clients/cli/commands/ensure-core-built.mjs
index 3523060cd2..e929eea4af 100644
--- a/packages/cli/clients/cli/commands/ensure-core-built.mjs
+++ b/packages/cli/clients/cli/commands/ensure-core-built.mjs
@@ -1,23 +1,18 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
/**
- * @file Shared test helper: build @astryxdesign/core once, race-free.
+ * @file Shared test helper: builds @astryxdesign/core once per run, race-free.
+ * @input Core's sources, translations and build scripts; this checkout's lock
+ * and run leases in the system temp directory.
+ * @output A packages/core/dist whose last output is newer than every source.
+ * @position Called by the node global setup and the build-theme suites.
*
- * `astryx theme build` imports the compiled @astryxdesign/core/theme entry
- * (there is no in-CLI fallback generator), so any test exercising it needs a
- * built core. The CI `test` job runs `pnpm test` without a prior core build,
- * and Vitest runs test files in parallel worker forks. When two build-theme
- * suites each ran `if (!exists) pnpm -F @astryxdesign/core build` in their own
- * beforeAll, both workers saw dist missing and launched concurrent builds that
- * collided on the shared packages/core/dist: one worker cleaned dist while the
- * other was mid-write, failing nondeterministically ("Could not resolve
- * dist/index.js" / "ENOTEMPTY rmdir dist/hooks").
- *
- * This serializes the build behind a filesystem lock so exactly one worker
- * builds and the rest wait for it to finish before reading dist.
+ * A Core build deletes dist first. Only the global setup rebuilds a stale
+ * build, before the workers start, and only after earlier runs end.
*/
import {execFileSync} from 'node:child_process';
+import {createHash} from 'node:crypto';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
@@ -27,12 +22,153 @@ const REPO_ROOT = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../../../../..',
);
-const CORE_THEME_ENTRY = path.join(
+// The build's last output. `build:css` writes it after the compiled modules,
+// their declarations and the declaration extensions. So a build that was
+// interrupted or failed before it finished has no marker, and reads as missing.
+// (`dist/theme/index.js`, which the build-theme suites import, comes about
+// 11 s into a 38 s build.)
+export const CORE_BUILD_MARKER = path.join(
REPO_ROOT,
- 'packages/core/dist/theme/index.js',
+ 'packages/core/dist/astryx.css',
);
-// A lock directory (mkdir is atomic across processes) guards the build.
-const LOCK_DIR = path.join(os.tmpdir(), 'astryx-core-build.lock');
+// What the build compiles, and the scripts and config it compiles with: a file
+// here newer than the marker means dist is stale. A checkout rewrites the files
+// it changes, so they read as newer. The translations live outside src, in
+// locales. The scripts are the ones Core's `build*` scripts run that write
+// output (not its checks).
+export const CORE_SOURCES = [
+ path.join(REPO_ROOT, 'packages/core/src'),
+ path.join(REPO_ROOT, 'packages/core/locales'),
+ path.join(REPO_ROOT, 'packages/core/package.json'),
+ path.join(REPO_ROOT, 'packages/core/babel.config.json'),
+ path.join(REPO_ROOT, 'packages/core/babel-plugin-add-extensions.cjs'),
+ path.join(REPO_ROOT, 'packages/core/tsconfig.build.json'),
+ path.join(REPO_ROOT, 'tsconfig.json'),
+ path.join(REPO_ROOT, 'packages/core/scripts/build-pseudo-locale.mjs'),
+ path.join(REPO_ROOT, 'scripts/generate-i18n-runtime.mjs'),
+ path.join(REPO_ROOT, 'scripts/i18n-english-fallback.mjs'),
+ path.join(REPO_ROOT, 'scripts/add-declaration-extensions.mjs'),
+ path.join(REPO_ROOT, 'scripts/build-css.mjs'),
+ path.join(REPO_ROOT, 'scripts/lib/stylex-generated-variable.mjs'),
+ path.join(REPO_ROOT, 'scripts/stylex-module-resolution.cjs'),
+];
+// Git-ignored output generated from those sources, by the build and again by
+// the Vitest global setup (internal/test-utils/src/globalSetup.ts). Rewritten
+// after a build, it would read as newer than that build.
+export const CORE_GENERATED = [
+ path.join(REPO_ROOT, 'packages/core/src/i18n/generated-english'),
+ path.join(REPO_ROOT, 'packages/core/src/i18n/generated-locales'),
+ path.join(REPO_ROOT, 'packages/core/locales/pseudo.json'),
+];
+/**
+ * The lock directory (mkdir is atomic across processes) that guards the Core
+ * build of the checkout at `repoRoot`: one per checkout.
+ * @param {string} repoRoot
+ * @returns {string}
+ */
+export function coreBuildLockDir(repoRoot) {
+ const key = createHash('sha256')
+ .update(path.resolve(repoRoot))
+ .digest('hex')
+ .slice(0, 16);
+ return path.join(os.tmpdir(), `astryx-core-build-${key}.lock`);
+}
+const LOCK_DIR = coreBuildLockDir(REPO_ROOT);
+
+/**
+ * The directory where each node test run of the checkout at `repoRoot` holds
+ * a lease for as long as it runs. Each run has one file, named by its process
+ * id and holding the time it started.
+ * @param {string} repoRoot
+ */
+export function coreRunLeaseDir(repoRoot) {
+ return coreBuildLockDir(repoRoot).replace(/\.lock$/, '.runs');
+}
+const RUN_LEASE_DIR = coreRunLeaseDir(REPO_ROOT);
+
+/** @param {number} pid */
+function processIsAlive(pid) {
+ try {
+ process.kill(pid, 0);
+ return true;
+ } catch (error) {
+ const {code} = /** @type {NodeJS.ErrnoException} */ (error);
+ // ESRCH: no such process, so it has ended. EPERM: it runs as another user.
+ if (code === 'ESRCH') return false;
+ if (code === 'EPERM') return true;
+ throw error;
+ }
+}
+
+/**
+ * Registers this process as a running test run of the checkout, and returns
+ * the function that ends its lease (the global setup's teardown).
+ * @param {{leaseDir?: string, pid?: number, now?: number}} [options]
+ */
+export function registerTestRun({
+ leaseDir = RUN_LEASE_DIR,
+ pid = process.pid,
+ now = Date.now(),
+} = {}) {
+ fs.mkdirSync(leaseDir, {recursive: true});
+ const file = path.join(leaseDir, String(pid));
+ fs.writeFileSync(file, String(now));
+ return () => fs.rmSync(file, {force: true});
+}
+
+/**
+ * The runs in `leaseDir` that started before this one (by start time, then
+ * process id) and are still alive. Those may be reading the build now. A run
+ * that started later waits for this one instead, so two runs never wait for
+ * each other. A lease whose process is gone is removed.
+ * @param {string} leaseDir
+ * @param {{pid?: number, isAlive?: (pid: number) => boolean}} [options]
+ */
+export function earlierLiveRuns(
+ leaseDir,
+ {pid = process.pid, isAlive = processIsAlive} = {},
+) {
+ /** @type {string[]} */
+ let names;
+ try {
+ names = fs.readdirSync(leaseDir);
+ } catch (error) {
+ // No run has registered in this checkout yet.
+ if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT') {
+ return [];
+ }
+ throw error;
+ }
+ /** @param {string} name */
+ const startOf = name => {
+ try {
+ return Number(fs.readFileSync(path.join(leaseDir, name), 'utf8'));
+ } catch (error) {
+ // The run ended, and its lease went, between the listing and this read.
+ if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT') {
+ return NaN;
+ }
+ throw error;
+ }
+ };
+ const own = startOf(String(pid));
+ const ownStart = Number.isFinite(own) ? own : Infinity;
+ /** @type {number[]} */
+ const earlier = [];
+ for (const name of names) {
+ const other = Number(name);
+ if (!Number.isInteger(other) || other === pid) continue;
+ if (!isAlive(other)) {
+ fs.rmSync(path.join(leaseDir, name), {force: true});
+ continue;
+ }
+ const start = startOf(name);
+ if (start < ownStart || (start === ownStart && other < pid)) {
+ earlier.push(other);
+ }
+ }
+ return earlier;
+}
const BUILD_TIMEOUT_MS = 180_000;
// A lock older than this is assumed abandoned by a crashed/killed worker.
@@ -44,6 +180,56 @@ function sleepSync(ms) {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
}
+/**
+ * Whether `entry` is missing or older than any file under `roots` (files or
+ * directories; `node_modules`, `dist` and the paths in `generated` are
+ * skipped).
+ * @param {string} entry
+ * @param {readonly string[]} roots
+ * @param {readonly string[]} [generated]
+ * @returns {boolean}
+ */
+export function coreBuildIsStale(entry, roots, generated = []) {
+ let built;
+ try {
+ built = fs.statSync(entry).mtimeMs;
+ } catch (error) {
+ // Not built yet.
+ if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT') {
+ return true;
+ }
+ throw error;
+ }
+ const skipped = new Set(generated.map(p => path.resolve(p)));
+ const pending = [...roots];
+ while (pending.length > 0) {
+ const current = /** @type {string} */ (pending.pop());
+ if (skipped.has(path.resolve(current))) {
+ continue;
+ }
+ let stat;
+ try {
+ stat = fs.statSync(current);
+ } catch (error) {
+ // Removed while the walk ran.
+ if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT') {
+ continue;
+ }
+ throw error;
+ }
+ if (stat.isDirectory()) {
+ for (const name of fs.readdirSync(current)) {
+ if (name !== 'node_modules' && name !== 'dist') {
+ pending.push(path.join(current, name));
+ }
+ }
+ } else if (stat.mtimeMs > built) {
+ return true;
+ }
+ }
+ return false;
+}
+
function buildCore() {
execFileSync('pnpm', ['-F', '@astryxdesign/core', 'build'], {
cwd: REPO_ROOT,
@@ -52,28 +238,46 @@ function buildCore() {
});
}
-function lockIsStale() {
+/**
+ * @param {string} lockDir
+ * @param {number} staleLockMs
+ */
+function lockIsStale(lockDir, staleLockMs) {
try {
- return Date.now() - fs.statSync(LOCK_DIR).mtimeMs > STALE_LOCK_MS;
+ return Date.now() - fs.statSync(lockDir).mtimeMs > staleLockMs;
} catch {
// Vanished between the exists check and stat — treat as released.
return false;
}
}
-/** Try to acquire the lock. Returns true if this worker now holds it. */
-function tryAcquire() {
+/**
+ * Whether another worker holds the lock: it is building, and dist is not
+ * whole yet.
+ * @param {string} lockDir
+ * @param {number} staleLockMs
+ */
+function lockIsHeld(lockDir, staleLockMs) {
+ return fs.existsSync(lockDir) && !lockIsStale(lockDir, staleLockMs);
+}
+
+/**
+ * Try to acquire the lock. Returns true if this worker now holds it.
+ * @param {string} lockDir
+ * @param {number} staleLockMs
+ */
+function tryAcquire(lockDir, staleLockMs) {
try {
- fs.mkdirSync(LOCK_DIR);
+ fs.mkdirSync(lockDir);
return true;
} catch (err) {
if (/** @type {NodeJS.ErrnoException} */ (err).code !== 'EEXIST') {
throw err;
}
- if (lockIsStale()) {
- fs.rmSync(LOCK_DIR, {recursive: true, force: true});
+ if (lockIsStale(lockDir, staleLockMs)) {
+ fs.rmSync(lockDir, {recursive: true, force: true});
try {
- fs.mkdirSync(LOCK_DIR);
+ fs.mkdirSync(lockDir);
return true;
} catch (retryErr) {
if (/** @type {NodeJS.ErrnoException} */ (retryErr).code !== 'EEXIST') {
@@ -86,35 +290,187 @@ function tryAcquire() {
}
/**
- * Ensure packages/core/dist is built exactly once, even when called
- * concurrently from parallel Vitest workers. Safe to call from every
- * build-theme suite's beforeAll.
+ * Run `build` unless `isBuilt()`, in exactly one process at a time: the one
+ * holding `lockDir`. Every other caller returns only once no process holds
+ * the lock and the build is current, so none reads a build still being
+ * written.
+ * @param {{isBuilt: () => boolean, build: () => void, lockDir: string, staleLockMs?: number, pollMs?: number}} options
*/
-export function ensureCoreBuilt() {
- if (fs.existsSync(CORE_THEME_ENTRY)) {
- return;
- }
-
- const deadline = Date.now() + STALE_LOCK_MS;
+export function ensureBuiltOnce({
+ isBuilt,
+ build,
+ lockDir,
+ staleLockMs = STALE_LOCK_MS,
+ pollMs = POLL_MS,
+}) {
+ const deadline = Date.now() + staleLockMs;
while (Date.now() < deadline) {
- if (fs.existsSync(CORE_THEME_ENTRY)) {
+ if (!lockIsHeld(lockDir, staleLockMs) && isBuilt()) {
return;
}
- if (tryAcquire()) {
+ if (tryAcquire(lockDir, staleLockMs)) {
try {
- if (!fs.existsSync(CORE_THEME_ENTRY)) {
- buildCore();
+ if (!isBuilt()) {
+ build();
}
} finally {
- fs.rmSync(LOCK_DIR, {recursive: true, force: true});
+ fs.rmSync(lockDir, {recursive: true, force: true});
}
return;
}
- // Another worker is building; wait for it to finish and release the lock.
- sleepSync(POLL_MS);
+ // Another worker is building; wait for it to release the lock.
+ sleepSync(pollMs);
}
- // Waited past the stale threshold without the artifact appearing — build it
+ // Waited past the stale threshold without the lock coming free — build it
// ourselves rather than let the suite fail on a missing entry.
- buildCore();
+ build();
+}
+
+/**
+ * Build Core when `entry` is missing, or also when it is older than
+ * `roots` with `rebuildStale`, in one process at a time.
+ * @param {{entry: string, roots: readonly string[], generated?: readonly string[], build: () => void, lockDir: string, rebuildStale?: boolean, staleLockMs?: number, pollMs?: number}} options
+ */
+export function ensureCoreBuiltAt({
+ entry,
+ roots,
+ generated = [],
+ build,
+ lockDir,
+ rebuildStale = false,
+ staleLockMs,
+ pollMs = POLL_MS,
+}) {
+ ensureBuiltOnce({
+ isBuilt: rebuildStale
+ ? () => !coreBuildIsStale(entry, roots, generated)
+ : () => fs.existsSync(entry),
+ build,
+ lockDir,
+ staleLockMs,
+ pollMs,
+ });
+}
+
+/**
+ * Ensure packages/core/dist is built exactly once, even when called
+ * concurrently from parallel Vitest workers. A build-theme suite's beforeAll
+ * calls it with no options, and it builds only a missing Core. The node global
+ * setup rebuilds a build older than Core's sources (`ensureCoreBuiltForRun`).
+ * It does so before any worker forks, once every run that started before it
+ * has ended, so never under a running suite.
+ * @param {{rebuildStale?: boolean}} [options]
+ */
+export function ensureCoreBuilt({rebuildStale = false} = {}) {
+ ensureCoreBuiltAt({
+ entry: CORE_BUILD_MARKER,
+ roots: CORE_SOURCES,
+ generated: CORE_GENERATED,
+ build: buildCore,
+ lockDir: LOCK_DIR,
+ rebuildStale,
+ });
+}
+
+/** How long a run waits for the runs that started before it, ms. */
+export const EARLIER_RUN_WAIT_MS = 120_000;
+
+/**
+ * Waits while the build is stale and a run that started before this one is
+ * still live (`leaseDir`). It waits on a timer, so the event loop stays free
+ * and Ctrl-C still stops the run. Gives up after `limitMs`. Resolves to the runs still
+ * live when it gave up, or to none.
+ * @param {{entry: string, roots: readonly string[], generated?: readonly string[], leaseDir: string, isAlive?: (pid: number) => boolean, pid?: number, pollMs?: number, limitMs?: number}} options
+ * @returns {Promise<number[]>}
+ */
+export async function waitForEarlierRuns({
+ entry,
+ roots,
+ generated = [],
+ leaseDir,
+ isAlive,
+ pid,
+ pollMs = POLL_MS,
+ limitMs = EARLIER_RUN_WAIT_MS,
+}) {
+ const deadline = Date.now() + limitMs;
+ let announced = false;
+ for (;;) {
+ if (!fs.existsSync(entry) || !coreBuildIsStale(entry, roots, generated)) {
+ return [];
+ }
+ const earlier = earlierLiveRuns(leaseDir, {pid, isAlive});
+ if (earlier.length === 0 || Date.now() >= deadline) {
+ return earlier;
+ }
+ if (!announced) {
+ console.warn(
+ `[ensure-core-built] Core changed; waiting up to ${Math.round(limitMs / 1000)} s for the test run already running in this checkout (pid ${earlier.join(', ')}) to end before rebuilding it.`,
+ );
+ announced = true;
+ }
+ await new Promise(resolve => setTimeout(resolve, pollMs));
+ }
+}
+
+/**
+ * The node global setup's Core step: once the runs that started earlier have
+ * ended, a stale build is rebuilt. If one is still live at the limit, this run
+ * stops with an error naming it. Rebuilding would delete dist under that run,
+ * and testing the stale build would test old code.
+ * @param {{entry: string, roots: readonly string[], generated?: readonly string[], build: () => void, lockDir: string, leaseDir: string, isAlive?: (pid: number) => boolean, pid?: number, pollMs?: number, limitMs?: number}} options
+ */
+export async function ensureCoreBuiltForRunAt({
+ entry,
+ roots,
+ generated = [],
+ build,
+ lockDir,
+ leaseDir,
+ isAlive,
+ pid,
+ pollMs,
+ limitMs,
+}) {
+ const stillLive = await waitForEarlierRuns({
+ entry,
+ roots,
+ generated,
+ leaseDir,
+ isAlive,
+ pid,
+ pollMs,
+ limitMs,
+ });
+ if (stillLive.length > 0) {
+ throw new Error(
+ `[ensure-core-built] Core's sources changed since packages/core/dist was built, and test run ${stillLive.join(', ')} in this checkout still uses that build after ${Math.round((limitMs ?? EARLIER_RUN_WAIT_MS) / 1000)} s: rebuilding would delete dist under it, and this run would test old code. Run again once it ends; if that process is not a test run, remove its lease: ${stillLive.map(pid => path.join(leaseDir, String(pid))).join(', ')}.`,
+ );
+ }
+ ensureCoreBuiltAt({
+ entry,
+ roots,
+ generated,
+ build,
+ lockDir,
+ rebuildStale: true,
+ pollMs,
+ });
+}
+
+/**
+ * `ensureCoreBuiltForRunAt` for this checkout: the node global setup's step.
+ * @param {{limitMs?: number}} [options]
+ */
+export function ensureCoreBuiltForRun({limitMs} = {}) {
+ return ensureCoreBuiltForRunAt({
+ entry: CORE_BUILD_MARKER,
+ roots: CORE_SOURCES,
+ generated: CORE_GENERATED,
+ build: buildCore,
+ lockDir: LOCK_DIR,
+ leaseDir: RUN_LEASE_DIR,
+ limitMs,
+ });
}
diff --git a/scripts/ensure-core-built.test.mjs b/scripts/ensure-core-built.test.mjs
new file mode 100644
index 0000000000..15ee267d87
--- /dev/null
+++ b/scripts/ensure-core-built.test.mjs
@@ -0,0 +1,638 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file ensure-core-built.test.mjs
+ * @input coreBuildIsStale over temporary trees; ensureBuiltOnce with a
+ * temporary lock directory; the repository's .gitignore
+ * @output A core build counts as current only when it is newer than every
+ * source file. A missing entry, or a source or package.json changed after
+ * it, is stale. node_modules, dist and generated output are not sources, and
+ * Core's translations are. A caller waiting on another's build returns only
+ * once that build released the lock. A suite builds only a missing Core.
+ * Only the node global setup rebuilds a stale one, before the workers fork.
+ * It waits until every run that started before it has ended. Each checkout
+ * has its own lock and its own run leases. Every file Core's build scripts
+ * write output with, and every local file those load, is a source.
+ * @position Unit test of packages/cli/clients/cli/commands/ensure-core-built.mjs
+ */
+
+import {spawn} from 'node:child_process';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {afterEach, describe, expect, it, vi} from 'vitest';
+import {
+ CORE_BUILD_MARKER,
+ CORE_GENERATED,
+ CORE_SOURCES,
+ coreBuildIsStale,
+ coreBuildLockDir,
+ coreRunLeaseDir,
+ earlierLiveRuns,
+ ensureBuiltOnce,
+ ensureCoreBuiltAt,
+ ensureCoreBuiltForRunAt,
+ registerTestRun,
+} from '../packages/cli/clients/cli/commands/ensure-core-built.mjs';
+
+const REPO_ROOT = path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ '..',
+);
+
+let dir;
+afterEach(() => {
+ if (dir) fs.rmSync(dir, {recursive: true, force: true});
+ dir = undefined;
+});
+
+/** A core-shaped tree: sources at t = 1000 s, the built entry at t = 2000 s. */
+function tree() {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-core-build-stale-'));
+ const write = (relative, content, seconds) => {
+ const file = path.join(dir, relative);
+ fs.mkdirSync(path.dirname(file), {recursive: true});
+ fs.writeFileSync(file, content);
+ fs.utimesSync(file, seconds, seconds);
+ return file;
+ };
+ const source = write('src/theme/index.ts', 'export {};', 1000);
+ const pkg = write('package.json', '{}', 1000);
+ const entry = write('dist/theme/index.js', 'export {};', 2000);
+ return {
+ write,
+ source,
+ entry,
+ roots: [path.join(dir, 'src'), pkg],
+ };
+}
+
+describe('coreBuildIsStale', () => {
+ it('throws on a built entry it cannot read, rather than read it as missing', () => {
+ const {roots, source} = tree();
+ expect(() =>
+ coreBuildIsStale(path.join(source, 'index.js'), roots),
+ ).toThrow(/ENOTDIR/);
+ });
+
+ it('throws on a source it cannot read, rather than skip it', () => {
+ const {entry, source} = tree();
+ expect(() =>
+ coreBuildIsStale(entry, [path.join(source, 'nested.ts')]),
+ ).toThrow(/ENOTDIR/);
+ });
+
+ it('is stale with no build', () => {
+ const {entry, roots} = tree();
+ fs.rmSync(entry);
+ expect(coreBuildIsStale(entry, roots)).toBe(true);
+ });
+
+ it('is current when the build is newer than every source', () => {
+ const {entry, roots} = tree();
+ expect(coreBuildIsStale(entry, roots)).toBe(false);
+ });
+
+ it('is stale when a source changed after the build, as a checkout does', () => {
+ const {entry, roots, source} = tree();
+ fs.utimesSync(source, 3000, 3000);
+ expect(coreBuildIsStale(entry, roots)).toBe(true);
+ });
+
+ it('is stale when a new source appeared after the build', () => {
+ const {entry, roots, write} = tree();
+ write('src/theme/adaptations.ts', 'export {};', 3000);
+ expect(coreBuildIsStale(entry, roots)).toBe(true);
+ });
+
+ it('is stale when package.json changed after the build', () => {
+ const {entry, roots} = tree();
+ fs.utimesSync(roots[1], 3000, 3000);
+ expect(coreBuildIsStale(entry, roots)).toBe(true);
+ });
+
+ it('skips node_modules and dist under a source root', () => {
+ const {entry, roots, write} = tree();
+ write('src/node_modules/x/index.js', 'x', 3000);
+ write('src/dist/x.js', 'x', 3000);
+ expect(coreBuildIsStale(entry, roots)).toBe(false);
+ });
+
+ it('skips generated output, which the test setup rewrites after a build', () => {
+ const {entry, roots, write} = tree();
+ write('src/i18n/generated-locales/en.generated.ts', 'x', 3000);
+ write('locales/pseudo.json', '{}', 3000);
+ write('locales/en.json', '{}', 1000);
+ const generated = [
+ path.join(dir, 'src/i18n/generated-locales'),
+ path.join(dir, 'locales/pseudo.json'),
+ ];
+ const withLocales = [...roots, path.join(dir, 'locales')];
+ expect(coreBuildIsStale(entry, withLocales, generated)).toBe(false);
+ // The translations they come from are sources.
+ fs.utimesSync(path.join(dir, 'locales/en.json'), 3000, 3000);
+ expect(coreBuildIsStale(entry, withLocales, generated)).toBe(true);
+ });
+});
+
+describe("Core's sources", () => {
+ it('include its translations, and skip exactly the git-ignored output under them', () => {
+ const core = path.join(REPO_ROOT, 'packages/core');
+ expect(CORE_SOURCES).toContain(path.join(core, 'locales'));
+ // Every git-ignored path under a source root is generated output.
+ const ignored = fs
+ .readFileSync(path.join(REPO_ROOT, '.gitignore'), 'utf8')
+ .split('\n')
+ .map(line => line.trim())
+ .filter(line => line.startsWith('packages/core/'))
+ .map(line => path.join(REPO_ROOT, line.replace(/\/$/u, '')))
+ .filter(p => CORE_SOURCES.some(root => p.startsWith(root + path.sep)));
+ expect(ignored.length).toBeGreaterThan(0);
+ expect([...CORE_GENERATED].sort()).toEqual([...ignored].sort());
+ });
+});
+
+describe('ensureBuiltOnce', () => {
+ /** A lock directory another process holds and releases after `ms`. */
+ function heldFor(ms) {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-core-build-lock-'));
+ const lockDir = path.join(dir, 'lock');
+ fs.mkdirSync(lockDir);
+ const child = spawn(
+ process.execPath,
+ [
+ '-e',
+ `setTimeout(() => require('node:fs').rmSync(${JSON.stringify(lockDir)}, {recursive: true, force: true}), ${ms})`,
+ ],
+ {stdio: 'ignore'},
+ );
+ return {lockDir, child};
+ }
+
+ it('waits for the lock to be released before reading a build another caller is writing', async () => {
+ const {lockDir, child} = heldFor(400);
+ const exited = new Promise(resolve => child.on('exit', resolve));
+ let builds = 0;
+ const start = Date.now();
+ // The entry already reads as current while the holder still writes the
+ // rest of the build.
+ ensureBuiltOnce({
+ isBuilt: () => true,
+ build: () => {
+ builds += 1;
+ },
+ lockDir,
+ pollMs: 20,
+ });
+ const waited = Date.now() - start;
+ await exited;
+ expect(builds).toBe(0);
+ expect(waited).toBeGreaterThanOrEqual(350);
+ });
+
+ it('returns at once when the build is current and nobody holds the lock', () => {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-core-build-lock-'));
+ let builds = 0;
+ const start = Date.now();
+ ensureBuiltOnce({
+ isBuilt: () => true,
+ build: () => {
+ builds += 1;
+ },
+ lockDir: path.join(dir, 'lock'),
+ });
+ expect(builds).toBe(0);
+ expect(Date.now() - start).toBeLessThan(200);
+ });
+
+ it('builds once under the lock when the build is stale, then releases it', () => {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-core-build-lock-'));
+ const lockDir = path.join(dir, 'lock');
+ let built = false;
+ let heldDuringBuild = false;
+ ensureBuiltOnce({
+ isBuilt: () => built,
+ build: () => {
+ heldDuringBuild = fs.existsSync(lockDir);
+ built = true;
+ },
+ lockDir,
+ });
+ expect(built).toBe(true);
+ expect(heldDuringBuild).toBe(true);
+ expect(fs.existsSync(lockDir)).toBe(false);
+ });
+});
+
+describe('ensureCoreBuiltAt', () => {
+ it('builds a missing Core', () => {
+ const {entry, roots} = tree();
+ fs.rmSync(entry);
+ let builds = 0;
+ ensureCoreBuiltAt({
+ entry,
+ roots,
+ build: () => {
+ builds++;
+ },
+ lockDir: path.join(dir, 'lock'),
+ });
+ expect(builds).toBe(1);
+ });
+
+ it('leaves a present build alone in a suite, even when a source changed after it', () => {
+ // A Core edit while the suites run must not rebuild under them: the
+ // build deletes dist first, and suites still reading it fail.
+ const {source, entry, roots} = tree();
+ fs.utimesSync(source, 3000, 3000);
+ expect(coreBuildIsStale(entry, roots)).toBe(true);
+ let builds = 0;
+ ensureCoreBuiltAt({
+ entry,
+ roots,
+ build: () => {
+ builds++;
+ },
+ lockDir: path.join(dir, 'lock'),
+ });
+ expect(builds).toBe(0);
+ });
+
+ it('rebuilds a build older than a source when asked, as the global setup does', () => {
+ const {source, entry, roots} = tree();
+ fs.utimesSync(source, 3000, 3000);
+ let builds = 0;
+ ensureCoreBuiltAt({
+ entry,
+ roots,
+ build: () => {
+ builds++;
+ },
+ lockDir: path.join(dir, 'lock'),
+ rebuildStale: true,
+ });
+ expect(builds).toBe(1);
+ });
+
+ it('is asked to rebuild a stale Core only by the node global setup', () => {
+ const setup = fs.readFileSync(
+ path.join(REPO_ROOT, 'vitest.global-setup.node.mjs'),
+ 'utf8',
+ );
+ expect(setup).toContain('await ensureCoreBuiltForRun();');
+ const suites = [];
+ const walk = current => {
+ for (const entry of fs.readdirSync(current, {withFileTypes: true})) {
+ const full = path.join(current, entry.name);
+ if (entry.isDirectory()) {
+ if (entry.name !== 'node_modules') walk(full);
+ } else if (
+ entry.name.endsWith('.test.mjs') &&
+ /ensureCoreBuiltForRun\(|ensureCoreBuilt\([^)]*rebuildStale/.test(
+ fs.readFileSync(full, 'utf8'),
+ )
+ ) {
+ suites.push(path.relative(REPO_ROOT, full));
+ }
+ }
+ };
+ walk(path.join(REPO_ROOT, 'packages/cli'));
+ expect(suites).toEqual([]);
+ });
+});
+
+describe('coreBuildLockDir', () => {
+ it('gives each checkout its own lock', () => {
+ expect(coreBuildLockDir('/work/a')).not.toBe(coreBuildLockDir('/work/b'));
+ expect(coreBuildLockDir('/work/a')).toBe(coreBuildLockDir('/work/a/'));
+ expect(path.dirname(coreBuildLockDir('/work/a'))).toBe(os.tmpdir());
+ });
+
+ it("does not keep a checkout with a current build waiting on another checkout's build", () => {
+ const {entry, roots} = tree();
+ const other = coreBuildLockDir(path.join(dir, 'other-checkout'));
+ fs.mkdirSync(other);
+ try {
+ const started = Date.now();
+ ensureCoreBuiltAt({
+ entry,
+ roots,
+ build: () => {
+ throw new Error('a current build is not rebuilt');
+ },
+ lockDir: coreBuildLockDir(dir),
+ staleLockMs: 5000,
+ pollMs: 50,
+ });
+ expect(Date.now() - started).toBeLessThan(1000);
+ } finally {
+ fs.rmSync(other, {recursive: true, force: true});
+ }
+ });
+});
+
+describe('the build marker', () => {
+ it("is the output Core's build writes last, after the modules and their declarations", () => {
+ const {scripts} = JSON.parse(
+ fs.readFileSync(
+ path.join(REPO_ROOT, 'packages/core/package.json'),
+ 'utf8',
+ ),
+ );
+ const steps = scripts.build.split('&&').map(step => step.trim());
+ const at = name => steps.findIndex(step => step.includes(name));
+ expect(path.relative(REPO_ROOT, CORE_BUILD_MARKER)).toBe(
+ 'packages/core/dist/astryx.css',
+ );
+ expect(at('build:css')).toBeGreaterThan(at('build:esm'));
+ expect(at('build:css')).toBeGreaterThan(at('tsc '));
+ expect(at('build:css')).toBeGreaterThan(at('add-declaration-extensions'));
+ expect(scripts['build:css']).toContain('build-css.mjs');
+ expect(
+ fs.readFileSync(path.join(REPO_ROOT, 'scripts/build-css.mjs'), 'utf8'),
+ ).toContain("outFile: 'astryx.css'");
+ });
+
+ it('rebuilds a build that stopped before its marker, even when its modules are newer than every source', () => {
+ // A rebuild interrupted after Babel wrote dist/theme/index.js, before the
+ // declarations and the stylesheet: the marker is missing.
+ const {write, roots} = tree();
+ write('dist/theme/index.js', 'export {};', 3000);
+ let builds = 0;
+ ensureCoreBuiltAt({
+ entry: path.join(dir, 'dist/astryx.css'),
+ roots,
+ build: () => {
+ builds++;
+ },
+ lockDir: path.join(dir, 'lock'),
+ });
+ expect(builds).toBe(1);
+ });
+});
+
+describe("Core's build inputs", () => {
+ it("cover every script and config file Core's build scripts write output with, and every local file those load", () => {
+ const coreDir = path.join(REPO_ROOT, 'packages/core');
+ const {scripts} = JSON.parse(
+ fs.readFileSync(path.join(coreDir, 'package.json'), 'utf8'),
+ );
+ const inputs = new Set();
+ for (const [name, command] of Object.entries(scripts)) {
+ if (!/^build(:|$)/.test(name)) continue;
+ for (const [, file] of command.matchAll(/\bnode\s+(\S+\.m?js)/g)) {
+ // Checks read the build; they write nothing it depends on.
+ if (!/\/(check-|clean-dist)/.test(file)) inputs.add(file);
+ }
+ for (const [, file] of command.matchAll(
+ /--(?:config-file|project)\s+(\S+)/g,
+ )) {
+ inputs.add(file);
+ }
+ }
+ const covered = file =>
+ CORE_SOURCES.some(root => {
+ const relative = path.relative(root, file);
+ return (
+ relative === '' ||
+ (!relative.startsWith('..') && !path.isAbsolute(relative))
+ );
+ });
+ // Follow what those files load: a config's local plugins and presets, and
+ // a script's relative imports, transitively.
+ const resolveLocal = (from, specifier) => {
+ const base = path.resolve(path.dirname(from), specifier);
+ return ['', '.mjs', '.js', '.cjs', '.json']
+ .map(ext => base + ext)
+ .find(
+ candidate =>
+ fs.existsSync(candidate) && fs.statSync(candidate).isFile(),
+ );
+ };
+ const queue = [...inputs].map(file => path.resolve(coreDir, file));
+ const seen = new Set();
+ while (queue.length > 0) {
+ const file = queue.pop();
+ if (seen.has(file) || !fs.existsSync(file)) continue;
+ seen.add(file);
+ const text = fs.readFileSync(file, 'utf8');
+ const local = [];
+ if (file.endsWith('.json')) {
+ const config = JSON.parse(text);
+ for (const entry of [
+ ...(config.plugins ?? []),
+ ...(config.presets ?? []),
+ ]) {
+ const name = Array.isArray(entry) ? entry[0] : entry;
+ if (typeof name === 'string' && /^\.\.?\//.test(name))
+ local.push(name);
+ }
+ if (
+ typeof config.extends === 'string' &&
+ /^\.\.?\//.test(config.extends)
+ ) {
+ local.push(config.extends);
+ }
+ } else {
+ for (const [, specifier] of text.matchAll(
+ /(?:\bfrom\s*|\bimport\s*\(?\s*|\brequire\s*\(\s*)['"](\.\.?\/[^'"]+)['"]/g,
+ )) {
+ local.push(specifier);
+ }
+ }
+ for (const specifier of local) {
+ const target = resolveLocal(file, specifier);
+ if (target) queue.push(target);
+ }
+ }
+ const missing = [...seen]
+ .filter(file => !covered(file))
+ .map(file => path.relative(REPO_ROOT, file));
+ expect(inputs.size).toBeGreaterThan(3);
+ expect(seen.size).toBeGreaterThan(inputs.size);
+ expect(missing).toEqual([]);
+ });
+});
+
+describe('run leases', () => {
+ /** A stale build (a source newer than it) and a lease directory. */
+ function staleTree() {
+ const built = tree();
+ fs.utimesSync(built.source, 3000, 3000);
+ return {...built, leaseDir: path.join(dir, 'runs')};
+ }
+
+ /** The node global setup's Core step over a made-up checkout. */
+ function forRun(built, options) {
+ const builds = [];
+ const done = ensureCoreBuiltForRunAt({
+ entry: built.entry,
+ roots: built.roots,
+ build: () => builds.push(Date.now()),
+ lockDir: path.join(dir, 'lock'),
+ leaseDir: built.leaseDir,
+ pollMs: 20,
+ ...options,
+ });
+ return {builds, done};
+ }
+
+ it('make a stale rebuild wait until a run that started before it has ended', async () => {
+ const built = staleTree();
+ registerTestRun({leaseDir: built.leaseDir, pid: 101, now: 1});
+ registerTestRun({leaseDir: built.leaseDir, pid: process.pid, now: 2});
+ const ends = Date.now() + 300;
+ const {builds, done} = forRun(built, {
+ isAlive: pid => pid === 101 && Date.now() < ends,
+ });
+ await done;
+ expect(builds).toHaveLength(1);
+ expect(builds[0]).toBeGreaterThanOrEqual(ends);
+ });
+
+ it('wait on a timer, so the run stays stoppable: its timers and signal handlers run meanwhile', async () => {
+ const built = staleTree();
+ registerTestRun({leaseDir: built.leaseDir, pid: 101, now: 1});
+ registerTestRun({leaseDir: built.leaseDir, pid: process.pid, now: 2});
+ const ends = Date.now() + 300;
+ let timerAt = 0;
+ setTimeout(() => {
+ timerAt = Date.now();
+ }, 50);
+ const {builds, done} = forRun(built, {
+ isAlive: pid => pid === 101 && Date.now() < ends,
+ });
+ await done;
+ expect(timerAt).toBeGreaterThan(0);
+ expect(timerAt).toBeLessThan(builds[0]);
+ });
+
+ it('give up after the limit: the run stops with an error naming the live run, and rebuilds nothing', async () => {
+ const built = staleTree();
+ registerTestRun({leaseDir: built.leaseDir, pid: 101, now: 1});
+ registerTestRun({leaseDir: built.leaseDir, pid: process.pid, now: 2});
+ // The wait announces itself once; this test reads only the outcome.
+ const announced = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ try {
+ const startedAt = Date.now();
+ const {builds, done} = forRun(built, {
+ isAlive: pid => pid === 101,
+ limitMs: 150,
+ });
+ const error = await done.catch(e => e);
+ expect(error.message).toMatch(
+ /test run 101 in this checkout still uses that build/,
+ );
+ // A leftover lease (a run killed by SIGHUP) can name another process
+ // that took its pid: the message names the file to remove.
+ expect(error.message).toContain(path.join(built.leaseDir, '101'));
+ expect(Date.now() - startedAt).toBeLessThan(2000);
+ expect(builds).toEqual([]);
+ expect(coreBuildIsStale(built.entry, built.roots)).toBe(true);
+ expect(announced.mock.calls[0]?.[0]).toMatch(/waiting up to 0 s/);
+ } finally {
+ announced.mockRestore();
+ }
+ });
+
+ it('throw on a lease directory it cannot read, rather than read it as empty', () => {
+ const built = staleTree();
+ fs.writeFileSync(built.leaseDir, 'not a directory');
+ expect(() => earlierLiveRuns(built.leaseDir)).toThrow(/ENOTDIR/);
+ });
+
+ it('throw on a lease it cannot read, rather than read it as no start time', () => {
+ const built = staleTree();
+ registerTestRun({leaseDir: built.leaseDir, pid: process.pid, now: 2});
+ fs.mkdirSync(path.join(built.leaseDir, '101'));
+ expect(() =>
+ earlierLiveRuns(built.leaseDir, {isAlive: () => true}),
+ ).toThrow(/EISDIR/);
+ });
+
+ it('throw on a lease whose process id the system refuses, rather than read the run as ended and remove its lease', () => {
+ const built = staleTree();
+ registerTestRun({leaseDir: built.leaseDir, pid: process.pid, now: 2});
+ registerTestRun({leaseDir: built.leaseDir, pid: 2 ** 31, now: 1});
+ expect(() => earlierLiveRuns(built.leaseDir)).toThrow(/pid/);
+ expect(fs.existsSync(path.join(built.leaseDir, String(2 ** 31)))).toBe(
+ true,
+ );
+ });
+
+ it('let a run that started later wait instead, so two runs never wait for each other', async () => {
+ const built = staleTree();
+ registerTestRun({leaseDir: built.leaseDir, pid: process.pid, now: 1});
+ registerTestRun({leaseDir: built.leaseDir, pid: 202, now: 2});
+ expect(earlierLiveRuns(built.leaseDir, {isAlive: () => true})).toEqual([]);
+ expect(
+ earlierLiveRuns(built.leaseDir, {pid: 202, isAlive: () => true}),
+ ).toEqual([process.pid]);
+ const {builds, done} = forRun(built, {isAlive: () => true});
+ await done;
+ expect(builds).toHaveLength(1);
+ });
+
+ it('drop the lease of a run whose process is gone', () => {
+ const {leaseDir} = staleTree();
+ registerTestRun({leaseDir, pid: 303, now: 1});
+ registerTestRun({leaseDir, pid: process.pid, now: 2});
+ expect(earlierLiveRuns(leaseDir, {isAlive: () => false})).toEqual([]);
+ expect(fs.readdirSync(leaseDir)).toEqual([String(process.pid)]);
+ });
+
+ it('end with the process too: a run stopped with Ctrl-C, which calls no teardown, leaves no lease', async () => {
+ const {leaseDir} = staleTree();
+ const helper = path.join(
+ REPO_ROOT,
+ 'packages/cli/clients/cli/commands/ensure-core-built.mjs',
+ );
+ // The node global setup's pattern: a lease ended on exit, in a process
+ // that exits on SIGINT as Vitest does.
+ const child = spawn(
+ process.execPath,
+ [
+ '--input-type=module',
+ '-e',
+ [
+ `import {registerTestRun} from ${JSON.stringify(helper)};`,
+ `const endRun = registerTestRun({leaseDir: ${JSON.stringify(leaseDir)}});`,
+ "process.once('exit', endRun);",
+ "process.on('SIGINT', () => process.exit(130));",
+ "console.log('ready');",
+ 'setInterval(() => {}, 1000);',
+ ].join('\n'),
+ ],
+ {stdio: ['ignore', 'pipe', 'inherit']},
+ );
+ await new Promise(resolve => child.stdout.once('data', resolve));
+ expect(fs.readdirSync(leaseDir)).toEqual([String(child.pid)]);
+ const exited = new Promise(resolve => child.once('exit', resolve));
+ child.kill('SIGINT');
+ expect(await exited).toBe(130);
+ expect(fs.readdirSync(leaseDir)).toEqual([]);
+ });
+
+ it("end with the run: the teardown removes this run's lease", () => {
+ const {leaseDir} = staleTree();
+ const end = registerTestRun({leaseDir});
+ expect(fs.readdirSync(leaseDir)).toEqual([String(process.pid)]);
+ end();
+ expect(fs.readdirSync(leaseDir)).toEqual([]);
+ });
+
+ it('are held by the node global setup for its whole run, beside its own lock', () => {
+ const setup = fs.readFileSync(
+ path.join(REPO_ROOT, 'vitest.global-setup.node.mjs'),
+ 'utf8',
+ );
+ expect(setup).toMatch(
+ /export default async function setup\(project\) \{[\s\S]*const endRun = registerTestRun\(\);\s*process\.once\('exit', endRun\);\s*try \{[\s\S]*await ensureCoreBuiltForRun\(\);[\s\S]*return \(\) => \{[\s\S]*endRun\(\);\s*\};\s*\} catch \(error\) \{\s*endRun\(\);\s*throw error;\s*\}/,
+ );
+ expect(path.dirname(coreRunLeaseDir(REPO_ROOT))).toBe(
+ path.dirname(coreBuildLockDir(REPO_ROOT)),
+ );
+ expect(coreRunLeaseDir(REPO_ROOT)).not.toBe(coreBuildLockDir(REPO_ROOT));
+ });
+});
diff --git a/vitest.global-setup.node.mjs b/vitest.global-setup.node.mjs
index 2066bc87b7..621944edcf 100644
--- a/vitest.global-setup.node.mjs
+++ b/vitest.global-setup.node.mjs
@@ -3,8 +3,10 @@
/**
* @file globalSetup for the `node` test project.
* @input Shared ensureCoreBuilt helper and authored Sandbox template descriptors.
- * @output Builds Core, regenerates the Sandbox template registry and route
- * wrappers, and packs the CLI once, before any test worker forks.
+ * @output Holds this run's lease and builds Core. It rebuilds a Core build
+ * older than its sources once no earlier run is still running. It
+ * regenerates the Sandbox template registry and route wrappers and packs the
+ * CLI, once, before any test worker forks.
* @position Referenced by vitest.config.ts's `node` project. The build-theme
* suites need a compiled Core; the Sandbox route contract imports generated
* template metadata and scans ignored wrappers that do not exist in a fresh
@@ -20,33 +22,54 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {ensureCoreBuilt} from './packages/cli/clients/cli/commands/ensure-core-built.mjs';
+import {
+ ensureCoreBuiltForRun,
+ registerTestRun,
+} from './packages/cli/clients/cli/commands/ensure-core-built.mjs';
import {packCli} from './packages/cli/test-utils/pack-cli.mjs';
/** @param {import('vitest/node').TestProject} project */
-export default function setup(project) {
- ensureCoreBuilt();
- // The ignored registry and ~700 physical page wrappers are build outputs,
- // not fixtures to check in. The route-manifest test must see the same inputs
- // as `pnpm -F @astryxdesign/sandbox build` on a clean CI checkout.
- execFileSync(
- process.execPath,
- [fileURLToPath(new URL('./scripts/sync-templates.js', import.meta.url))],
- {
- cwd: fileURLToPath(new URL('.', import.meta.url)),
- maxBuffer: 32 * 1024 * 1024,
- },
- );
- // `npm pack` costs about 5 s of CPU. Packing once here keeps that out of
- // every suite that places CLI copies, and out of their timed tests. A watch
- // session packs again before a rerun of this project's tests, so they see
- // CLI edits. A rerun of other projects' tests does not wait for it.
- const packDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-cli-tarball-'));
- const pack = () =>
- project.provide('astryxPackedCliTarball', packCli(packDir));
- pack();
- project.onTestsRerun(specs => {
- if (specs.some(spec => spec.project.name === project.name)) pack();
- });
- return () => fs.rmSync(packDir, {recursive: true, force: true});
+export default async function setup(project) {
+ // This run holds a lease for its whole length, so a later run's setup does
+ // not rebuild Core under it. The returned teardown ends it, and so does the
+ // process's exit: a run stopped with Ctrl-C calls no teardown.
+ const endRun = registerTestRun();
+ process.once('exit', endRun);
+ try {
+ // A stale Core is rebuilt only here, before the workers. A suite that
+ // rebuilt it mid-run would delete dist under suites still reading it. The
+ // wait for an earlier run is a timer, so Ctrl-C still stops this one.
+ await ensureCoreBuiltForRun();
+ // The ignored registry and ~700 physical page wrappers are build outputs,
+ // not fixtures to check in. The route-manifest test must see the same
+ // inputs as `pnpm -F @astryxdesign/sandbox build` on a clean CI checkout.
+ execFileSync(
+ process.execPath,
+ [fileURLToPath(new URL('./scripts/sync-templates.js', import.meta.url))],
+ {
+ cwd: fileURLToPath(new URL('.', import.meta.url)),
+ maxBuffer: 32 * 1024 * 1024,
+ },
+ );
+ // `npm pack` costs about 5 s of CPU. Packing once here keeps that out of
+ // every suite that places CLI copies, and out of their timed tests. A watch
+ // session packs again before a rerun of this project's tests, so they see
+ // CLI edits. A rerun of other projects' tests does not wait for it.
+ const packDir = fs.mkdtempSync(
+ path.join(os.tmpdir(), 'astryx-cli-tarball-'),
+ );
+ const pack = () =>
+ project.provide('astryxPackedCliTarball', packCli(packDir));
+ pack();
+ project.onTestsRerun(specs => {
+ if (specs.some(spec => spec.project.name === project.name)) pack();
+ });
+ return () => {
+ fs.rmSync(packDir, {recursive: true, force: true});
+ endRun();
+ };
+ } catch (error) {
+ endRun();
+ throw error;
+ }
}