Skip to content

test(cli): fix concurrent core-build race in build-theme suites - #3518

Closed
cixzhang wants to merge 1 commit into
mainfrom
navi/fix/core-build-race-in-cli-tests
Closed

cixzhang wants to merge 1 commit into
mainfrom
navi/fix/core-build-race-in-cli-tests

Conversation

@cixzhang

@cixzhang cixzhang commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Problem

The two build-theme CLI suites intermittently fail in the test CI job with nondeterministic errors:

  • error: Could not resolve ".../packages/core/dist/index.js"
  • [Error: ENOTEMPTY: directory not empty, rmdir '.../packages/core/dist/hooks'] followed by dozens of Could not resolve "./<Component>/index.js"

The different error each run is the tell: it's a race, not a logic bug.

Root cause

astryx theme build imports the compiled @astryxdesign/core/theme entry — there's no in-CLI fallback generator — so build-theme.prose.test.mjs and build-theme.import-path.test.mjs both need a built packages/core/dist. The test CI job runs the suite without a prior core build, and Vitest runs test files in parallel worker forks.

Each suite's beforeAll independently did:

if (!fs.existsSync(CORE_THEME_ENTRY)) {
  execFileSync('pnpm', ['-F', '@astryxdesign/core', 'build'], ...);
}

When dist is absent, both workers pass the existsSync check and launch pnpm -F @astryxdesign/core build concurrently. Core's build script starts with rimraf dist, so the two builds stomp the shared packages/core/dist: one worker wipes/rebuilds dist while the other is mid-write, producing the missing-artifact and ENOTEMPTY failures above. It's a classic TOCTOU — the check doesn't prevent two processes from both deciding to build.

Fix

Extract a shared ensureCoreBuilt() helper that serializes the build behind an atomic lock directory (mkdir is atomic across processes):

  • The first worker to acquire the lock builds core; the others poll and wait until it's released, then read the finished dist.
  • If dist already exists, it's a no-op fast path (no rebuild).
  • Includes stale-lock recovery so a crashed/killed builder can't wedge the suite indefinitely.

Both suites now call ensureCoreBuilt() in beforeAll instead of racing.

Verification

Run locally from a cold state (rm -rf packages/core/dist), both suites in parallel:

  • Cold (one worker builds, other waits): 5 passed in ~31s.
  • Warm (dist present, build skipped): 5 passed in ~3.8s.

eslint clean on all three files; repo sync / package-boundary / changeset checks pass. No consumer-facing change, so no changeset.

The build-theme.prose and build-theme.import-path suites each need a
compiled @astryxdesign/core (theme build imports the built theme entry;
there is no in-CLI fallback generator). The CI test job runs the suite
without a prior core build, and Vitest runs test files in parallel
worker forks. Each suite's beforeAll independently ran
`if (!exists) pnpm -F @astryxdesign/core build`, so both workers saw
dist missing and launched concurrent builds that collided on the shared
packages/core/dist (core's build starts with `rimraf dist`): one worker
wiped dist while the other was mid-write, failing nondeterministically
("Could not resolve dist/index.js" or "ENOTEMPTY rmdir dist/hooks").

Extract a shared ensureCoreBuilt() helper that serializes the build
behind an atomic lock directory, so exactly one worker builds and the
rest wait for it to finish before reading dist. Includes stale-lock
recovery for a crashed/killed builder.
@vercel

vercel Bot commented Jul 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview, Comment Jul 4, 2026 1:31am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 4, 2026
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.6KB 0B

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Jul 4, 2026
@cixzhang cixzhang closed this Jul 4, 2026
@cixzhang
cixzhang deleted the navi/fix/core-build-race-in-cli-tests branch July 4, 2026 04:58

This branch was successfully deployed

1 active deployment
Preview — 6006fced Deployed Jul 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants