Skip to content

Codegen variable naming can result in incorrect code #53839

Closed as not planned
Closed as not planned

Description

@mfazekas

Description

type MyEventType = {
  type: string;
  payload: {
    message: string;
  }
};

export interface NativeProps extends ViewProps {
  onPressed?: CodegenTypes.BubblingEventHandler<MyEventType> | null;
}

codegen generates such a code not the payload variable gets shadowed. And the bug is in the line: payload.setProperty(runtime, "payload", payload);

void SimpleViewEventEmitter::onPressed(OnPressed event) const {
  dispatchEvent("pressed", [event=std::move(event)](jsi::Runtime &runtime) {
    auto payload = jsi::Object(runtime);
    payload.setProperty(runtime, "type", event.type);
{
  auto payload = jsi::Object(runtime);
  payload.setProperty(runtime, "message", event.payload.message);
  payload.setProperty(runtime, "payload", payload);
}
    return payload;
  });
}

See
https://github.com/mfazekas/codegen-variable-naming-bug

Steps to reproduce

check out https://github.com/mfazekas/codegen-variable-naming-bug
cd ReproducerApp/ios
pod install
cat build/generated/ios/react/renderer/components/AppSpec/EventEmitters.cpp

React Native Version

0.81.4

Affected Platforms

Other (please specify)

Areas

Codegen

Output of npx @react-native-community/cli info

info Fetching system and libraries information...
System:
  OS: macOS 15.6
  CPU: (12) arm64 Apple M2 Max
  Memory: 125.66 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.16.0
    path: ~/.nvm/versions/node/v22.16.0/bin/node
  Yarn:
    version: 1.22.21
    path: ~/.local/bin/yarn
  npm:
    version: 11.5.2
    path: ~/.nvm/versions/node/v22.16.0/bin/npm
  Watchman:
    version: 2025.06.30.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /Users/boga/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.5
      - iOS 18.5
      - macOS 15.5
      - tvOS 18.5
      - visionOS 2.5
      - watchOS 11.5
  Android SDK: Not Found
IDEs:
  Android Studio: 2025.1 AI-251.26094.121.2513.14007798
  Xcode:
    version: 16.4/16F6
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 20.0.1
    path: /usr/bin/javac
  Ruby:
    version: 2.7.8
    path: /Users/boga/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react: Not Found
  react-native: Not Found
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

n/a

MANDATORY Reproducer

https://github.com/mfazekas/codegen-variable-naming-bug

Screenshots and Videos

No response

Activity

  1. mfazekas commented on Sep 18, 2025

    @mfazekas
    ContributorAuthor

    In this case the use of payload was a curlpit.
    But we can have the same issue with

    type MyEventType = {
      type: string;
      data: {
        type: string;
        data: {
           message: string
        }
      }
    };

    Possible solutions:

    • codegen maintains used variables and reports an error before it would use the same variable name
    • codegen uses variable names like payload_1, etc.

    Possible workarounds:

    • do not use payload or same name in nested structures.

    Relevant code:
    https://github.com/facebook/react-native/blob/6ef351b3cea4103c72eb4657aebb5a7a8f1232e7/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js#L379-L386

    https://github.com/facebook/react-native/blob/6ef351b3cea4103c72eb4657aebb5a7a8f1232e7/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js#L113-L137

  2. cortinico commented on Sep 18, 2025

    @cortinico
    Contributor
    • codegen uses variable names like payload_1, etc.

    This feels like the correct solution IMHO. Could you send us a PR + make sure the code is documented to explain why variables are renamed

  3. react-native-bot commented on Mar 19, 2026

    @react-native-bot
    Collaborator

    This issue is stale because it has been open for 180 days with no activity. It will be closed in 7 days unless you comment on it or remove the "Stale" label.

  4. added
    StaleThere has been a lack of activity on this issue and it may be closed soon.
    and removed
    StaleThere has been a lack of activity on this issue and it may be closed soon.
    on Mar 19, 2026
  5. react-native-bot commented on Sep 18, 2026

    @react-native-bot
    Collaborator

    This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

  6. added
    StaleThere has been a lack of activity on this issue and it may be closed soon.
    on Sep 18, 2026
  7. react-native-bot commented on Sep 25, 2026

    @react-native-bot
    Collaborator

    This issue was closed because it has been stalled for 7 days with no activity.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: PR SubmittedA pull request with a fix has been provided.StaleThere has been a lack of activity on this issue and it may be closed soon.Tech: CodegenRelated to react-native-codegenType: New ArchitectureIssues and PRs related to new architecture (Fabric/Turbo Modules)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions