From be8595b18a46635bf679d8e7473f2960c33530fa Mon Sep 17 00:00:00 2001 From: ismarbesic Date: Thu, 8 May 2025 04:38:29 -0700 Subject: [PATCH] Exclude selectively disabled libraries from codegen generation (#51078) Summary: Selectively disabling autolinking of a native dependency with components registered in the codegen configuration in react-native.config.js causes builds to crash upon launch on iOS. This is because the generated `RCTThirdPartyComponentsProvider.mm` file contains references to the excluded library using `NSClassFromString` causing the returned NSDictionary from `+[RCTThirdPartyComponentsProvider thirdPartyFabricComponents]` to be populated with nil values and therefore crashing the app. This has been confirmed in 0.78.2 and 0.79.2 but probably exists in 0.77.x as well. The issue has been further described in https://github.com/facebook/react-native/issues/51077. ## Changelog: [IOS][FIXED] - Skip codegen for selectively disabled libraries in react-native.config.js Pull Request resolved: https://github.com/facebook/react-native/pull/51078 Test Plan: 1. Install a library that has the componentProvider field set in the codegen config (see reproducer) and install the iOS pods. 2. Build the app. 3. App should run successfully without any crashes. Reviewed By: cortinico Differential Revision: D74248371 Pulled By: cipolleschi fbshipit-source-id: 1ff7b477ed3d94ca45616ae243d3d2d30bd897db --- .../generate-artifacts-executor/index.js | 23 +++++++++- .../generate-artifacts-executor/utils.js | 44 +++++++++++++------ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js index 488679b7735..b062ad6d798 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js @@ -35,8 +35,10 @@ const { cleanupEmptyFilesAndFolders, codegenLog, findCodegenEnabledLibraries, + findDisabledLibrariesByPlatform, pkgJsonIncludesGeneratedCode, readPkgJsonInDirectory, + readReactNativeConfig, } = require('./utils'); const path = require('path'); @@ -83,9 +85,14 @@ function execute( buildCodegenIfNeeded(); } - const libraries = findCodegenEnabledLibraries(pkgJson, projectRoot); + const reactNativeConfig = readReactNativeConfig(projectRoot); + const codegenEnabledLibraries = findCodegenEnabledLibraries( + pkgJson, + projectRoot, + reactNativeConfig, + ); - if (libraries.length === 0) { + if (codegenEnabledLibraries.length === 0) { codegenLog('No codegen-enabled libraries found.', true); return; } @@ -94,6 +101,18 @@ function execute( targetPlatform === 'all' ? supportedPlatforms : [targetPlatform]; for (const platform of platforms) { + const disabledLibraries = findDisabledLibrariesByPlatform( + reactNativeConfig, + platform, + ); + const libraries = codegenEnabledLibraries.filter( + ({name}) => !disabledLibraries.includes(name), + ); + + if (!libraries.length) { + continue; + } + const outputPath = computeOutputPath( projectRoot, baseOutputPath, diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js index 55e9f639c6a..d78cd074878 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -94,10 +94,20 @@ function cleanupEmptyFilesAndFolders(filepath) { } } +function readReactNativeConfig(projectRoot) { + const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js'); + + if (!fs.existsSync(rnConfigFilePath)) { + return {}; + } + + return require(rnConfigFilePath); +} + /** * Finding libraries! */ -function findCodegenEnabledLibraries(pkgJson, projectRoot) { +function findCodegenEnabledLibraries(pkgJson, projectRoot, reactNativeConfig) { const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot); if (pkgJsonIncludesGeneratedCode(pkgJson)) { return projectLibraries; @@ -105,7 +115,7 @@ function findCodegenEnabledLibraries(pkgJson, projectRoot) { return [ ...projectLibraries, ...findExternalLibraries(pkgJson, projectRoot), - ...findLibrariesFromReactNativeConfig(projectRoot), + ...findLibrariesFromReactNativeConfig(projectRoot, reactNativeConfig), ]; } } @@ -128,22 +138,13 @@ function findProjectRootLibraries(pkgJson, projectRoot) { return extractLibrariesFromJSON(pkgJson, projectRoot); } -function findLibrariesFromReactNativeConfig(projectRoot) { - const rnConfigFileName = 'react-native.config.js'; - +function findLibrariesFromReactNativeConfig(projectRoot, rnConfig) { codegenLog( - `Searching for codegen-enabled libraries in ${rnConfigFileName}`, + `Searching for codegen-enabled libraries in react-native.config.js`, true, ); - const rnConfigFilePath = path.resolve(projectRoot, rnConfigFileName); - - if (!fs.existsSync(rnConfigFilePath)) { - return []; - } - const rnConfig = require(rnConfigFilePath); - - if (rnConfig.dependencies == null) { + if (!rnConfig.dependencies) { return []; } return Object.keys(rnConfig.dependencies).flatMap(name => { @@ -366,6 +367,19 @@ function getLibraryName(library) { ).name; } +/** + * Finds all disabled libraries by platform based the react native config. + * + * This is needed when selectively disabling libraries in react-native.config.js since codegen should exclude those libraries as well. + */ +function findDisabledLibrariesByPlatform(reactNativeConfig, platform) { + const dependencies = reactNativeConfig.dependencies ?? {}; + + return Object.keys(dependencies).filter( + dependency => dependencies[dependency].platforms?.[platform] === null, + ); +} + module.exports = { buildCodegenIfNeeded, pkgJsonIncludesGeneratedCode, @@ -377,4 +391,6 @@ module.exports = { findProjectRootLibraries, extractLibrariesFromJSON, parseiOSAnnotations, + readReactNativeConfig, + findDisabledLibrariesByPlatform, };