Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autolink scans non used components #2455

Open
mfazekas opened this issue Jul 13, 2024 · 1 comment · May be fixed by #2456
Open

autolink scans non used components #2455

mfazekas opened this issue Jul 13, 2024 · 1 comment · May be fixed by #2456

Comments

@mfazekas
Copy link

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
Copy link
Author

mfazekas commented Jul 13, 2024

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

export function findComponentDescriptors(packageRoot: string) {
const files = glob.sync('**/+(*.js|*.jsx|*.ts|*.tsx)', {
cwd: unixifyPaths(packageRoot),
onlyFiles: true,
ignore: ['**/node_modules/**'],
});
const codegenComponent = files
.map((filePath) =>
fs.readFileSync(path.join(packageRoot, filePath), 'utf8'),
)
.map(extractComponentDescriptors)
.filter(Boolean);
// Filter out duplicates as it happens that libraries contain multiple outputs due to package publishing.
// TODO: consider using "codegenConfig" to avoid this.
return Array.from(new Set(codegenComponent as string[]));
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant