mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Skip third-party components whose class is not linked instead of crashing at launch (#59067)
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 `<UnimplementedView>` 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
This commit is contained in:
1 parent
c89f4120ac
commit
acc0cca710
4 files changed
+50
-8
No files matched your search
+33
-5
@@ -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<NSString *, NSString *> * 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<NSString *, NSString *> * 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;
|
||||
|
||||
+1
-1
@@ -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});
|
||||
|
||||
packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js
Vendored
+1
-1
@@ -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');
|
||||
|
||||
+15
-1
@@ -19,9 +19,23 @@
|
||||
static dispatch_once_t nativeComponentsToken;
|
||||
|
||||
dispatch_once(&nativeComponentsToken, ^{
|
||||
thirdPartyComponents = @{
|
||||
NSDictionary<NSString *, NSString *> * 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;
|
||||
|
||||
Reference in new issue
Block a user