Skip to content

example of rslib issues in React Spectrum#9728

Draft
davidferguson wants to merge 1 commit intoadobe:mainfrom
davidferguson:davidferguson/rslib-s2-issues
Draft

example of rslib issues in React Spectrum#9728
davidferguson wants to merge 1 commit intoadobe:mainfrom
davidferguson:davidferguson/rslib-s2-issues

Conversation

@davidferguson
Copy link

We are seeing issues in React Spectrum / unplugin-parcel-macros when used in Rslib.

There seems to be a race condition so that the more macros that are used (and the more complex the macros that are used are), the more likely a rslib failure is, resulting in a CSS file not being generated in the virtual filesystem in time, and the build failing as it cannot find it.

Reproduction steps

Success case

  1. cd examples/s2-rslib-semiworking/
  2. yarn install
  3. yarn build
  4. Observe that the build (likely) succeeded. There is only one simple macro use in this project.

Failure case

  1. cd examples/s2-rslib-notworking/
  2. yarn install
  3. yarn build
  4. Observe the build (likely) failed. There are lots of files that use macros here, and they use larger/more complex macros.

Debugging notes from AI

TL;DR: It's a race condition in unplugin-parcel-macros + unplugin's virtual module system within rspack. No config-level fix found yet — likely needs an upstream fix or inlining styles.


🔍 The Problem
pnpm nx build out-package-name fails ~50% of the time with:

Module not found: Can't resolve 'macro-<hash>.css'

🕵️ Root Cause
The S2 style() macro generates CSS at build time. That CSS lives in:
• A module-level assets Map inside unplugin-parcel-macros (shared across plugin instances)
• Empty placeholder files in node_modules/.virtual/ written by unplugin's FakeVirtualModulesPlugin

The transform hook generates CSS → adds to assets Map → appends import "macro-<hash>.css" to the source. Then resolveId maps it to a .virtual/ file, and the load hook serves the real CSS from the Map.

The race happens within rspack's internal concurrent module processing. Even with a single lib entry and a single rspack compiler, the async transform → resolveId → load pipeline has a timing window where virtual CSS module resolution fails.


🧪 What I Tested

Approach Result
Normal build (ESM + CJS parallel) ~40% pass
Patch: remove assets.delete() cleanup Still fails
Patch: prevent .virtual/ dir deletion on shutdown Still fails
Both patches combined Still fails
Patch: per-instance assets Map Worse
Sequential builds (separate processes) ESM passes, CJS still flaky
Single lib entry only Still flaky (3/10)
Disable DTS Still fails
Clean .virtual/ before every build Still fails
cssModules: { namedExport: false } Still fails

💡 Key Takeaways
• Not caused by parallel ESM+CJS — fails with a single lib entry too
• Not caused by stale .virtual/ files — cleaning doesn't help
• Not caused by the asset cleanup code — removing it doesn't fix it
• The working repo (pandora-tooling-demo) has fewer macro files and simpler inline style() calls — smaller race window
• Importing style files with { type: 'macro' } doesn't work — S2 throws an error since the exports are already-expanded values, not macro functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant