From acc0cca7108dfbdbb09cde571006b000915734f4 Mon Sep 17 00:00:00 2001 From: Yevhenii Date: Wed, 14 Oct 2026 11:06:42 -0700 Subject: [PATCH] Skip third-party components whose class is not linked instead of crashing at launch (#59067) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: `RCTThirdPartyComponentsProvider.mm` builds its component map from a dictionary literal of `NSClassFromString` results. When a component's library is not linked into the target, such as an app extension or App Clip that leaves a library out (https://github.com/react/react-native/issues/56469), or a macOS app whose dependencies include an iOS-only component library (react-native-macos generates the same file), `NSClassFromString` returns nil and the literal throws at launch: ``` *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]' ``` The provider now maps component names to class names and registers the classes the runtime has, the way `RCTModuleProviders.mm` already handles a module provider that cannot be found. A component left out this way falls back to `` if it is rendered, as an unknown component does. ## Changelog: [IOS] [FIXED] - Skip third-party components whose class is not linked instead of crashing at launch Pull Request resolved: https://github.com/react/react-native/pull/59067 Test Plan: - `generate-artifacts-executor-test.js`: the provider snapshots and the crawl case expect the name-to-class-name map. In Linux (WSL) the file's 52 tests and 24 snapshots pass; its suite-level `fs.rmdirSync(…, {recursive: true})` cleanup fails under Node 26, on `main` too. - The generated `RCTThirdPartyComponentsProvider.mm` for two component libraries, compiled into a binary that links only one of their classes (clang++, Foundation, macOS): - `main`: `NSInvalidArgumentException … attempt to insert nil object from objects[1]` - this change: `registered components: LinkedComponent` - `yarn flow`: no errors; eslint and prettier on the changed files: clean. Reviewed By: christophpurrer Differential Revision: D125236957 Pulled By: cipolleschi fbshipit-source-id: 55126e43352aad7275298978dceaa744a163f966 --- .../generate-artifacts-executor-test.js.snap | 38 ++++++++++++++++--- .../generate-artifacts-executor-test.js | 2 +- .../generateRCTThirdPartyComponents.js | 2 +- ...RCTThirdPartyComponentsProviderMM.template | 16 +++++++- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/packages/react-native/scripts/codegen/__tests__/__snapshots__/generate-artifacts-executor-test.js.snap b/packages/react-native/scripts/codegen/__tests__/__snapshots__/generate-artifacts-executor-test.js.snap index 6bc79e6875e..4f2318466f5 100644 --- a/packages/react-native/scripts/codegen/__tests__/__snapshots__/generate-artifacts-executor-test.js.snap +++ b/packages/react-native/scripts/codegen/__tests__/__snapshots__/generate-artifacts-executor-test.js.snap @@ -328,10 +328,24 @@ exports[`execute test-app "ReactCodegen/RCTThirdPartyComponentsProvider.mm" shou static dispatch_once_t nativeComponentsToken; dispatch_once(&nativeComponentsToken, ^{ - thirdPartyComponents = @{ - @\\"TestAppComponent\\": NSClassFromString(@\\"RCTTestAppComponent\\"), // test-app - @\\"TestLibraryComponent\\": NSClassFromString(@\\"RCTTestLibraryComponent\\"), // test-library + NSDictionary * componentMapping = @{ + @\\"TestAppComponent\\": @\\"RCTTestAppComponent\\", // test-app + @\\"TestLibraryComponent\\": @\\"RCTTestLibraryComponent\\", // test-library }; + + NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:componentMapping.count]; + + for (NSString *key in componentMapping) { + // Nil for a library this target does not link (an app extension, macOS). + Class klass = NSClassFromString(componentMapping[key]); + if (!klass) { + continue; + } + + [dict setObject:klass forKey:key]; + } + + thirdPartyComponents = dict; }); return thirdPartyComponents; @@ -822,9 +836,23 @@ exports[`execute test-app-legacy "ReactCodegen/RCTThirdPartyComponentsProvider.m static dispatch_once_t nativeComponentsToken; dispatch_once(&nativeComponentsToken, ^{ - thirdPartyComponents = @{ - @\\"TestAppDeprecatedComponent\\": NSClassFromString(@\\"RCTTestAppDeprecatedComponentClass\\"), // test-app-legacy + NSDictionary * componentMapping = @{ + @\\"TestAppDeprecatedComponent\\": @\\"RCTTestAppDeprecatedComponentClass\\", // test-app-legacy }; + + NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:componentMapping.count]; + + for (NSString *key in componentMapping) { + // Nil for a library this target does not link (an app extension, macOS). + Class klass = NSClassFromString(componentMapping[key]); + if (!klass) { + continue; + } + + [dict setObject:klass forKey:key]; + } + + thirdPartyComponents = dict; }); return thirdPartyComponents; diff --git a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js index ea1aca23327..207feaa12dc 100644 --- a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js +++ b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js @@ -297,7 +297,7 @@ describe('generateRCTThirdPartyComponents', () => { 'utf8', ); expect(generatedFile).toContain( - '@"ExampleComponent": NSClassFromString(@"RCTExampleComponent"), // component-library', + '@"ExampleComponent": @"RCTExampleComponent", // component-library', ); } finally { fs.rmSync(libraryPath, {recursive: true, force: true}); diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js index 84857672fa0..dfe462782a8 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js @@ -139,7 +139,7 @@ function generateRCTThirdPartyComponents( .flatMap(library => { const components = componentsInLibraries[library]; return components.map(({componentName, className}) => { - return `\t\t@"${componentName}": NSClassFromString(@"${className}"), // ${library}`; + return `\t\t@"${componentName}": @"${className}", // ${library}`; }); }) .join('\n'); diff --git a/packages/react-native/scripts/codegen/templates/RCTThirdPartyComponentsProviderMM.template b/packages/react-native/scripts/codegen/templates/RCTThirdPartyComponentsProviderMM.template index 4ce510709e3..6c50d6d9c98 100644 --- a/packages/react-native/scripts/codegen/templates/RCTThirdPartyComponentsProviderMM.template +++ b/packages/react-native/scripts/codegen/templates/RCTThirdPartyComponentsProviderMM.template @@ -19,9 +19,23 @@ static dispatch_once_t nativeComponentsToken; dispatch_once(&nativeComponentsToken, ^{ - thirdPartyComponents = @{ + NSDictionary * componentMapping = @{ {thirdPartyComponentsMapping} }; + + NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:componentMapping.count]; + + for (NSString *key in componentMapping) { + // Nil for a library this target does not link (an app extension, macOS). + Class klass = NSClassFromString(componentMapping[key]); + if (!klass) { + continue; + } + + [dict setObject:klass forKey:key]; + } + + thirdPartyComponents = dict; }); return thirdPartyComponents;