react-native-community / cli

The React Native Community CLI - command line tools to help you build RN apps
MIT License
2.32k stars 896 forks source link

autolink scans non used components #2455

Open mfazekas opened 1 month ago

mfazekas commented 1 month ago

Environment

info Fetching system and libraries information...
System:
  OS: macOS 14.5
  CPU: (12) arm64 Apple M2 Max
  Memory: 124.28 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.20.2
    path: ~/.nvm/versions/node/v18.20.2/bin/node
  Yarn:
    version: 3.6.4
    path: ~/.nvm/versions/node/v18.20.2/bin/yarn
  npm:
    version: 10.5.0
    path: ~/.nvm/versions/node/v18.20.2/bin/npm
  Watchman:
    version: 2024.05.06.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.2
    path: /Users/boga/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.5
      - iOS 17.5
      - macOS 14.5
      - tvOS 17.5
      - visionOS 1.2
      - watchOS 10.5
  Android SDK: Not Found
IDEs:
  Android Studio: 2024.1 AI-241.15989.150.2411.11948838
  Xcode:
    version: 15.4/15F31d
    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:
    installed: 19.0.0-rc-fb9a90fa48-20240614
    wanted: 19.0.0-rc-fb9a90fa48-20240614
  react-native:
    installed: 0.75.0-rc.3
    wanted: 0.75.0-rc.4
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Description

react-native 0.75-rc.4 uses npx @react-native-community/cli config to get components needed to autolink. Hovewer it just scans for all the folders for all source files, so it can find false positives. And it's hard to understand/troubleshoot what's going on.

I've faced it when I've moved a non finished spec out of the folders I've specified , yet autolink was still referred to it. I thought that there was some cached state that caused the component to be searched for.

In the reproducer the cli will find and adds https://github.com/mfazekas/rn-075-autolink/blob/autolink-wrong-files/RTNCenteredText/temp/Playgroud.ts to autolink.

Reproducible Demo

git clone https://github.com/mfazekas/rn-075-autolink
cd rn-075-autolink
git checkout autolink-wrong-files
cd ReproducerApp
yarn install
cd android 
./gradlew assembleDebug
  /tmp/rn-075-autolink/ReproducerApp/android/app/build/generated/autolinking/src/main/jni/autolinking.cpp:31:59: error: use of undeclared identifier 'SomeDrafrComponentComponentDescriptor'
  providerRegistry->add(concreteComponentDescriptorProvider<SomeDrafrComponentComponentDescriptor>());
mfazekas commented 1 month ago

As for fix, either the cli would need to invoke the codegen, or at lease use the same rules as codegen.

  1. read codegenConfig/jsSrcsDir from package.json
  2. only consider files with pattern /^(Native.+|.+NativeComponent)/ - see https://github.com/facebook/react-native/blob/65a3259f039416394d2b945ec10565d38b3cad9e/packages/react-native-codegen/src/cli/combine/combine-utils.js#L29-L43 and https://github.com/reactwg/react-native-new-architecture/blob/main/docs/fabric-native-components.md#2-javascript-specification and https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md#2-javascript-specification

https://github.com/react-native-community/cli/blob/d6982fc9a56812b0deff42cabcab09528f72fdca/packages/cli-platform-android/src/config/findComponentDescriptors.ts#L7-L23

https://github.com/facebook/react-native/blob/65a3259f039416394d2b945ec10565d38b3cad9e/packages/react-native/scripts/codegen/generate-artifacts-executor.js#L392-L416