react icon indicating copy to clipboard operation
react copied to clipboard

[Compiler Bug]: incorrectly uses variable name as JSX tag (`<base />`) instead of referenced value

Open icyJoseph opened this issue 1 month ago • 2 comments

What kind of issue is this?

  • [x] React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • [ ] babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • [ ] eslint-plugin-react-hooks (build issue installing or using the eslint plugin)
  • [ ] react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://playground.react.dev/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEASggIZyEBmMEGBA5DGRfQNwA6AdnBJ2IQCNSYBAQC8DACZoAbmy5cefQgBUE-AMK0cnBJzzJizPADoAYhvEEAFAEpxAPgLAuBAkv4Et2K0JEdONyY8WECAHm8sAHoHLgBfAK4EAA8cfAJJBEpSKAAbKihuPDReAgAJWgQ7Z1cCYNCCMLVNbV49Qhj4rhAAGhAlSjQAcxR0bFxCPABPLFFgAgAFXKghtE4AeSxi3jACOIJqWgYhAQRcgFosZdXOc6ZyPHOebDRchBgo6X55Ti5rF0CBCiUWeWFepG2nAAshBMoZ2CBSLlcgj4gQwBC0GBBupFtc1ptIWBbKxeuAABYQADuAEl9O9OEiwChsrkRHEgA

Repro steps

Given this input:

import React from 'react';
const base = 'div';

const TestComponent: React.FC = () => {
  const Comp = base;
  return <Comp/>
};

export default function Home() {
  return <TestComponent />
}

The compiler outputs this for the TestComponent

const TestComponent: React.FC = () => {
  const $ = _c(1);
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = <base />;
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  return t0;
};

First reported here: https://github.com/vercel/next.js/issues/86728

This outputs a <base /> tag instead of a div.

I searched on issues before posting, but I couldn't find other reports.

How often does this bug happen?

Every time

What version of React are you using?

Next.js' version on 16.0.6

What version of React Compiler are you using?

1.0.0

icyJoseph avatar Dec 02 '25 12:12 icyJoseph

Although i am not sure... but i think it is caused by this file \compiler\packages\babel-plugin-react-compiler\src\Optimization\InlineJsxTransform.ts, i cannot get into where in the code it does the transformation yet (since there is many concepts i dont know about the compiler internals)...

Ssword-dev avatar Dec 02 '25 22:12 Ssword-dev

i think it is in the function createTagProperty which may have not accounted for cases where a variable is assigned to another variable of camel case name which has a primitive value (e.g: div), so the fix is check whether the identifier is PascalCase when resolving to a specific origin.

Ssword-dev avatar Dec 02 '25 22:12 Ssword-dev