Skip to content

fix(unplugin): serialize concurrent CSS writes in Bun plugin - #1845

Open
ryuhzk wants to merge 1 commit into
facebook:mainfrom
ryuhzk:fix/bun-write-css-race
Open

fix(unplugin): serialize concurrent CSS writes in Bun plugin#1845
ryuhzk wants to merge 1 commit into
facebook:mainfrom
ryuhzk:fix/bun-write-css-race

Conversation

@ryuhzk

@ryuhzk ryuhzk commented Sep 4, 2026

Copy link
Copy Markdown

Summary

When building multi-file projects with Bun, StyleX transforms run concurrently via onLoad. Each transform triggers an asynchronous writeCss call against the shared cssOutput file.

Previously, those writes were not queued. lastCss was updated before fsp.writeFile completed, and callers did not wait on any in-flight write. Concurrent races could therefore produce out-of-order disk writes (an earlier partial CSS snapshot overwriting a later complete one). Separately, if onEnd observed next === lastCss, it skipped rewriting even when the file on disk still held a stale or incomplete result from an unfinished earlier write.

Solution

  • Serialize CSS writes through a Promise queue (writeQueue) so each write waits for the previous one to finish.
  • Keep the lastCss short-circuit for intermediate updates, but return the in-flight queue so callers still await completion.
  • Pass force: true from build.onEnd so the final collected CSS is always flushed after transforms finish.

Test plan

  • packages/@stylexjs/unplugin Jest suite (yarn test) — 17/17 passed
  • Manual Bun multi-file build: confirm stylex.dev.css (or configured bunDevCssOutput) ends with the complete StyleX CSS after parallel onLoad transforms

@ryuhzk
ryuhzk requested review from mellyeliu and nmn as code owners September 4, 2026 04:50
@meta-cla

meta-cla Bot commented Sep 4, 2026

Copy link
Copy Markdown

Hi @ryuhzk!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@vercel

vercel Bot commented Sep 4, 2026

Copy link
Copy Markdown

@ryuhzk is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@ryuhzk
ryuhzk force-pushed the fix/bun-write-css-race branch from f1f09ef to e8f7bc0 Compare September 4, 2026 04:57
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 4, 2026
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 Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant