Skip to content

Commit 1db1ee7

Browse files
authored
Skip static view config verification without native view configs
## Summary: A bridgeless host can turn on static view config verification with `NativeComponentRegistry.setRuntimeConfigProvider(() => ({native: false, verify: true}))`. If the host has no legacy view config interop binding, each native component then logs a `console.error` the first time it is used: ``` [ReactNative Architecture][JS] 'getViewManagerConfig('RCTView')' is not available in the new React Native architecture. If 'RCTView' has a ViewManager and you want to retrieve its native ViewConfig, please turn on the native ViewConfig interop layer. ... ``` In bridgeless mode, `UIManager.getViewManagerConfig` reads native view configs from the binding `global.RN$LegacyInterop_UIManager_getConstants`. Without it, the call logs the error above and returns null, so verification does nothing. The binding is absent when: - `useNativeViewConfigsInBridgelessMode` is off. It is off in `ReactNativeFeatureFlagsDefaults`; the stock entry points turn it on (`RCTReactNativeFactory` on iOS, `ReactNativeNewArchitectureFeatureFlagsDefaults` on Android), so this affects hosts that set up feature flags another way. - An iOS app is built with `RCT_REMOVE_LEGACY_COMPONENT_INTEROP`, which compiles the binding out (`RCTInstance.mm`). RNTester, which turns verification on in bridgeless development builds, logs these errors only in that case. `NativeComponentRegistry.get` now skips verification when the binding is absent. Nothing was verified in that case, so the only change is that the errors stop. `getWithFallback_DEPRECATED`, with no runtime config provider set, checks that a component exists with `getViewManagerConfig(name) != null`, which without the binding logs the error and returns the fallback for a component that exists. It now calls `hasViewManagerConfig(name)`: the same null check on the old architecture, and a query of the native component registry in bridgeless mode. That query throws if `global.__nativeComponentRegistry__hasComponent` is missing (`NativeComponentRegistryUnstable.js`), so the change reaches every bridgeless host; every host here installs it before running JavaScript (`RCTInstance.mm`, `ComponentNameResolverBinding` on Android, `ReactHost.cpp` in `ReactCxxPlatform`); development builds already call `hasViewManagerConfig` when `AppContainer-dev.js` loads `DebuggingOverlay.js`. ## Changelog: [GENERAL] [FIXED] - Stop logging a `getViewManagerConfig` error for every component when static view config verification is on in bridgeless mode, and stop `getWithFallback_DEPRECATED` from returning the fallback for a registered component there ## Test Plan: ```sh yarn jest --runInBand packages/react-native/Libraries/NativeComponent/__tests__/NativeComponentRegistry-test.js ``` `NativeComponentRegistry-test.js` is new. With `NativeComponentRegistry.js` from `main`, the case for bridgeless mode without the binding fails (verification requests the native view config), and so does the `getWithFallback_DEPRECATED` case (it returns the fallback for a registered component). Replacing either half of the new condition (`global.RN$Bridgeless === true`, or the check for `global.RN$LegacyInterop_UIManager_getConstants`) with `true` fails the case that covers it. The tests set or remove the binding themselves. ```sh yarn jest --runInBand packages/react-native/Libraries/NativeComponent packages/react-native/Libraries/ReactNative packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js ``` These pass, as do Flow, ESLint and Prettier. Not run: an app without the binding. Differential Revision: D135948468 Pull Request resolved: #60361
1 parent a1673b7 commit 1db1ee7

2 files changed

Lines changed: 180 additions & 1 deletion

File tree

‎packages/react-native/Libraries/NativeComponent/NativeComponentRegistry.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
import getNativeComponentAttributes from '../ReactNative/getNativeComponentAttributes';
1818
import UIManager from '../ReactNative/UIManager';
1919
import * as ReactNativeViewConfigRegistry from '../Renderer/shims/ReactNativeViewConfigRegistry';
20+
import warnOnce from '../Utilities/warnOnce';
2021
import * as StaticViewConfigValidator from './StaticViewConfigValidator';
2122
import {createViewConfig} from './ViewConfig';
2223
import invariant from 'invariant';
@@ -76,6 +77,20 @@ export function get<Config extends {...}>(
7677
);
7778

7879
if (verify) {
80+
// Skip verification in bridgeless mode when the legacy view config interop binding is absent
81+
if (
82+
global.RN$Bridgeless === true &&
83+
global.RN$LegacyInterop_UIManager_getConstants == null
84+
) {
85+
if (__DEV__) {
86+
warnOnce(
87+
'static-view-config-verification-without-legacy-interop',
88+
'Skipping static view config verification because the legacy view config interop binding is unavailable in bridgeless mode. Disable `verify` in `NativeComponentRegistry.setRuntimeConfigProvider` or enable `useNativeViewConfigsInBridgelessMode`.',
89+
);
90+
}
91+
return viewConfig;
92+
}
93+
7994
const nativeViewConfig = native
8095
? viewConfig
8196
: getNativeComponentAttributes(name);
@@ -149,7 +164,7 @@ export function getWithFallback_DEPRECATED<Config extends {...}>(
149164

150165
function hasNativeViewConfig(name: string): boolean {
151166
invariant(getRuntimeConfig == null, 'Unexpected invocation!');
152-
return UIManager.getViewManagerConfig(name) != null;
167+
return UIManager.hasViewManagerConfig(name);
153168
}
154169

155170
/**
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {PartialViewConfig} from '../../Renderer/shims/ReactNativeTypes';
12+
13+
// The Jest preset replaces this module with a mock.
14+
jest.unmock('../NativeComponentRegistry');
15+
16+
const NAME = 'RCTTestComponent';
17+
const SECOND_NAME = 'RCTSecondTestComponent';
18+
19+
const PARTIAL_VIEW_CONFIG: PartialViewConfig = {
20+
uiViewClassName: NAME,
21+
validAttributes: {},
22+
};
23+
24+
const SECOND_PARTIAL_VIEW_CONFIG: PartialViewConfig = {
25+
uiViewClassName: SECOND_NAME,
26+
validAttributes: {},
27+
};
28+
29+
const originalBridgeless = global.RN$Bridgeless;
30+
const originalGetConstants = global.RN$LegacyInterop_UIManager_getConstants;
31+
const originalDev = global.__DEV__;
32+
33+
beforeEach(() => {
34+
jest.resetModules();
35+
});
36+
37+
afterEach(() => {
38+
jest.restoreAllMocks();
39+
// $FlowExpectedError[cannot-write]
40+
global.RN$Bridgeless = originalBridgeless;
41+
// $FlowExpectedError[cannot-write]
42+
global.RN$LegacyInterop_UIManager_getConstants = originalGetConstants;
43+
// $FlowExpectedError[cannot-write]
44+
global.__DEV__ = originalDev;
45+
});
46+
47+
describe('get with verify', () => {
48+
function getNativeViewConfigRequests({
49+
bridgeless,
50+
dev = true,
51+
interop,
52+
names = [NAME],
53+
}: {
54+
bridgeless: boolean,
55+
dev?: boolean,
56+
interop: boolean,
57+
names?: ReadonlyArray<string>,
58+
}): ReadonlyArray<string> {
59+
// $FlowExpectedError[cannot-write]
60+
global.__DEV__ = dev;
61+
// $FlowExpectedError[cannot-write]
62+
global.RN$Bridgeless = bridgeless;
63+
// $FlowExpectedError[cannot-write]
64+
global.RN$LegacyInterop_UIManager_getConstants = interop
65+
? () => ({})
66+
: undefined;
67+
const getNativeComponentAttributes = jest.fn((name: string) => null);
68+
jest.doMock(
69+
'../../ReactNative/getNativeComponentAttributes',
70+
() => getNativeComponentAttributes,
71+
);
72+
73+
const NativeComponentRegistry = require('../NativeComponentRegistry');
74+
NativeComponentRegistry.setRuntimeConfigProvider(() => ({
75+
native: false,
76+
verify: true,
77+
}));
78+
const ViewConfigRegistry = require('../../Renderer/shims/ReactNativeViewConfigRegistry');
79+
for (const componentName of names) {
80+
const config =
81+
componentName === SECOND_NAME
82+
? SECOND_PARTIAL_VIEW_CONFIG
83+
: PARTIAL_VIEW_CONFIG;
84+
NativeComponentRegistry.get<{...}>(componentName, () => config);
85+
// Reading the registered lazy view config runs verification
86+
ViewConfigRegistry.get(componentName);
87+
}
88+
89+
return getNativeComponentAttributes.mock.calls.map(([name]) => name);
90+
}
91+
92+
test('skips verification and warns once in development without the interop binding in bridgeless mode', () => {
93+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
94+
95+
expect(
96+
getNativeViewConfigRequests({
97+
bridgeless: true,
98+
interop: false,
99+
names: [NAME, SECOND_NAME],
100+
}),
101+
).toEqual([]);
102+
expect(warnSpy).toHaveBeenCalledTimes(1);
103+
expect(warnSpy).toHaveBeenCalledWith(
104+
expect.stringContaining('Skipping static view config verification'),
105+
);
106+
});
107+
108+
test('does not warn outside development without the interop binding in bridgeless mode', () => {
109+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
110+
111+
expect(
112+
getNativeViewConfigRequests({
113+
bridgeless: true,
114+
dev: false,
115+
interop: false,
116+
}),
117+
).toEqual([]);
118+
expect(warnSpy).not.toHaveBeenCalled();
119+
});
120+
121+
test('requests the native view config in bridgeless mode with the interop binding', () => {
122+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
123+
124+
expect(
125+
getNativeViewConfigRequests({
126+
bridgeless: true,
127+
interop: true,
128+
}),
129+
).toEqual([NAME]);
130+
expect(warnSpy).not.toHaveBeenCalled();
131+
});
132+
133+
test('requests the native view config outside bridgeless mode', () => {
134+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
135+
136+
expect(
137+
getNativeViewConfigRequests({
138+
bridgeless: false,
139+
interop: false,
140+
}),
141+
).toEqual([NAME]);
142+
expect(warnSpy).not.toHaveBeenCalled();
143+
});
144+
});
145+
146+
describe('getWithFallback_DEPRECATED without a runtime config provider', () => {
147+
test('finds a component without requesting its view config', () => {
148+
const getViewManagerConfig = jest.fn((name: string) => null);
149+
jest.doMock('../../ReactNative/UIManager', () => ({
150+
getViewManagerConfig,
151+
hasViewManagerConfig: (name: string) => name === NAME,
152+
}));
153+
154+
const NativeComponentRegistry = require('../NativeComponentRegistry');
155+
156+
expect(
157+
NativeComponentRegistry.getWithFallback_DEPRECATED<{...}>(
158+
NAME,
159+
() => PARTIAL_VIEW_CONFIG,
160+
),
161+
).toBe(NAME);
162+
expect(getViewManagerConfig).not.toHaveBeenCalled();
163+
});
164+
});

0 commit comments

Comments
 (0)