Skip to content

[Compiler Bug]: enableFunctionOutlining breaks react-native-reanimated API callbacks #32580

@tjzel

Description

@tjzel

What kind of issue is this?

  • 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-compiler (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/#N4Igzg9grgTgxgUxALhHCA7MAXABAFQRwGEIBbAB0wQzwF5cAKYXDKMgIwRlwF8BKXHQB8uYAB0MuXOix4A1ggCe+CAEkMAEwQAPIbihgEAEW4BLAG4JNANQCGAGygJGjQSMnTpAbQCMAGlwAJkCAZgBdADoyOwpXd1E2Bwd+T1x+AG5JNJgEbFgpJIcsjF4SkF4gA

Repro steps

Current version the Compiler started to transform react-native-reanimated API callbacks too much, breaking the code. I compared it with an older version babel-plugin-react-compiler@19.0.0-beta-9ee70a1-20241017 and the problem didn't happen there. It seems to be due to the enableFunctionOutlining feature.

Given the code:

const TestComponent = ({ number }) => {
  const keyToIndex = useDerivedValue(() =>
    [1, 2, 3].map(() => null)
  );

  return null;
};

Compiler changes it to

const TestComponent = (t0) => {
  useDerivedValue(_temp2);
  return null;
};
function _temp() {
  return null;
}
function _temp2() {
  return [1, 2, 3].map(_temp);
}

Note that _temp and _temp2 are extracted outside. It's ok to extract _temp2, the callback of useDerivedValue, it's handled on our side as a part of the support for the Compiler. It's not ok to extract _temp.

The problem lies in the fact that we can (and we do) deduce that _temp2 must be a worklet, but we cannot in general infer if _temp should be a worklet. This leads to _temp not being workletized and causing a runtime error. We also can't workletize it regardless, in some flows it actually shouldn't be a worklet.

To fix it we need to keep all functions defined in a worklet to stay in this worklet.

How often does this bug happen?

Every time

What version of React are you using?

19

What version of React Compiler are you using?

19.0.0-beta-bafa41b-20250307

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions