Skip to content

fix(emotion): Handle React Compiler output without panicking - #636

Merged
Donny/강동윤 (kdy1) merged 3 commits into
swc-project:mainfrom
scttcper:scttcper/emotion-react-compiler
Jun 30, 2026
Merged

fix(emotion): Handle React Compiler output without panicking#636
Donny/강동윤 (kdy1) merged 3 commits into
swc-project:mainfrom
scttcper:scttcper/emotion-react-compiler

Conversation

@scttcper

@scttcper Scott Cooper (scttcper) commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

React Compiler can leave generated Emotion calls with dummy spans, then @swc/plugin-emotion panics trying to attach comments/source maps at BytePos(0).

This keeps normal Emotion output untouched and only skips pure comments/source maps when there is no real source position to attach them to.

minimal reproduction of panic in rspack/swc: https://github.com/scttcper/swc-react-compiler-plugins

React Compiler can leave generated Emotion calls at BytePos(0), which makes pure comments and source map lookup panic.

Skip those location-only bits when the span is synthetic and add a small wasm regression.

Co-authored-by: Codex <noreply@openai.com>
@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f83c16d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@swc/plugin-emotion Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@CLAassistant

CLAassistant commented Jun 29, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@socket-security

socket-security Bot commented Jun 29, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatednpm/​@​swc/​core@​1.15.40 ⏵ 1.15.4392 +110010096100

View full report

keep the fixture to the smallest JSX case we need here.

Co-authored-by: Codex <noreply@openai.com>
Comment thread package.json
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@swc/core": "^1.15.40",
"@swc/core": "^1.15.43",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed so the wasm test actually exercises reactCompiler; 1.15.40 rejects the option before it reaches the plugin.

@scttcper
Scott Cooper (scttcper) marked this pull request as ready for review June 29, 2026 23:42
@scttcper Scott Cooper (scttcper) changed the title fix(emotion): Handle synthetic React Compiler spans fix(emotion): Handle React Compiler output without panicking Jun 29, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 69eede5ee8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread packages/emotion/src/lib.rs Outdated
Comment on lines +75 to +76
let hash = if program.span().lo == BytePos(0) {
0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve a unique hash for dummy-span programs

When program.span().lo is BytePos(0), this now passes 0 as src_file_hash; create_label_prop_node uses that value to build every styled target class (e{hash}{count}). Any React-Compiler/SWC pipeline that gives the Program a dummy span but still contains styled(...) calls will therefore emit the same e00, e01, ... targets for unrelated files, so component selectors can match the wrong components. The fallback should still derive a per-file/source hash instead of using a constant.

Useful? React with 👍 / 👎.

drop the dummy program span fallback so styled target hashes stay on the existing path.

also adds the patch changeset for the plugin fix.

Co-authored-by: Codex <noreply@openai.com>

@kdy1 Donny/강동윤 (kdy1) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kdy1
Donny/강동윤 (kdy1) merged commit 14bd92c into swc-project:main Jun 30, 2026
11 of 12 checks passed
@scttcper
Scott Cooper (scttcper) deleted the scttcper/emotion-react-compiler branch June 30, 2026 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants