Skip to content

fix(compiler): use globalThis.Symbol to prevent shadowing by user components#35383

Closed
gowthamrdyy wants to merge 2 commits intofacebook:mainfrom
gowthamrdyy:fix/compiler-symbol-shadowing
Closed

fix(compiler): use globalThis.Symbol to prevent shadowing by user components#35383
gowthamrdyy wants to merge 2 commits intofacebook:mainfrom
gowthamrdyy:fix/compiler-symbol-shadowing

Conversation

@gowthamrdyy
Copy link
Copy Markdown

Summary

Fixes a bug where React Compiler generates code that uses Symbol.for() without qualifying it with globalThis, causing runtime errors when a user-defined component is named Symbol.

The Problem:
When a user defines a component named Symbol, it shadows the global built-in Symbol. The compiler-generated code (e.g., for memo cache sentinels) tries to call Symbol.for() but instead accesses the user's component, resulting in "Symbol.for is not a function".

Playground repro: Link

The Fix:
Updated CodegenReactiveFunction.ts to use globalThis.Symbol.for() instead of bare Symbol.for() references. This ensures the built-in global is always used, regardless of local variable names.

How did you test this change?

  1. Added a new test fixture (component-named-symbol-shadows-global.js) that defines a component named Symbol.
    • Verified that this test case previously failed (or would produce broken code) and now compiles to correct code using globalThis.Symbol.
  2. Verified generated output: checked that the compiled output for cache sentinels and early return sentinels now explicitly uses globalThis.Symbol.
  3. Manual verification: Confirmed the fix addresses the issue reported in the playground.

Copilot AI review requested due to automatic review settings December 17, 2025 10:25
@meta-cla meta-cla Bot added the CLA Signed label Dec 17, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical bug in the React Compiler where generated code uses Symbol.for() without qualifying it with globalThis, which causes runtime errors when a user-defined component is named Symbol, shadowing the global built-in.

Key Changes:

  • Updated CodegenReactiveFunction.ts to use globalThis.Symbol.for() instead of bare Symbol.for() in two locations (memo cache sentinels and early return sentinels)
  • Added a test fixture component-named-symbol-shadows-global.js to prevent regression

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-named-symbol-shadows-global.js Adds a regression test with a component named Symbol that shadows the global, ensuring the compiler generates code that explicitly uses globalThis.Symbol.for()
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts Fixes two locations where Symbol.for() is generated by qualifying it with globalThis.Symbol.for() to prevent shadowing issues

The fix is well-targeted and addresses the specific issue described in the PR. The test fixture follows the existing pattern used in the codebase, and the changes to CodegenReactiveFunction.ts are minimal and consistent with how globalThis is already used elsewhere in the codebase (e.g., in Globals.ts).


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Copy Markdown

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions Bot added the Resolution: Stale Automatically closed due to inactivity label Mar 17, 2026
@github-actions
Copy link
Copy Markdown

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions Bot closed this Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Resolution: Stale Automatically closed due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants