From eab32f19aff1b8ccb301d53852c0798117a7a2a5 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 20 Apr 2023 11:58:26 -0700 Subject: [PATCH] Report loaded modules when first module load fails (#36980) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36980 With the TurboModule interop layer, some modules aren't being loaded at all. This logging will help us root cause the problem: is the issue with the TurboModule system, or with instantiating a particular module. Changelog: [Internal] Reviewed By: cortinico, mdvacca Differential Revision: D45102812 fbshipit-source-id: 5c5f55d5857c694270a83c38b68cae0fdb2c3b50 --- .../TurboModule/TurboModuleRegistry.js | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js b/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js index 27167a13474..a069106ff29 100644 --- a/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js +++ b/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js @@ -16,22 +16,50 @@ const NativeModules = require('../BatchedBridge/NativeModules'); const turboModuleProxy = global.__turboModuleProxy; +const moduleLoadHistory = { + NativeModules: ([]: Array), + TurboModules: ([]: Array), + NotFound: ([]: Array), +}; + +function isBridgeless() { + return global.RN$Bridgeless === true; +} + +function isTurboModuleInteropEnabled() { + return global.RN$TurboInterop === true; +} + +function shouldReportLoadedModules() { + return isTurboModuleInteropEnabled(); +} + // TODO(148943970): Consider reversing the lookup here: // Lookup on __turboModuleProxy, then lookup on nativeModuleProxy function requireModule(name: string): ?T { - const isBridgeless = global.RN$Bridgeless === true; - const isTurboModuleInteropEnabled = global.RN$TurboInterop === true; - if (!isBridgeless || isTurboModuleInteropEnabled) { + if (!isBridgeless() || isTurboModuleInteropEnabled()) { // Backward compatibility layer during migration. const legacyModule = NativeModules[name]; if (legacyModule != null) { + if (shouldReportLoadedModules()) { + moduleLoadHistory.NativeModules.push(name); + } return ((legacyModule: $FlowFixMe): T); } } if (turboModuleProxy != null) { const module: ?T = turboModuleProxy(name); - return module; + if (module != null) { + if (shouldReportLoadedModules()) { + moduleLoadHistory.TurboModules.push(name); + } + return module; + } + } + + if (shouldReportLoadedModules()) { + moduleLoadHistory.NotFound.push(name); } return null; @@ -43,10 +71,14 @@ export function get(name: string): ?T { export function getEnforcing(name: string): T { const module = requireModule(name); - invariant( - module != null, + let message = `TurboModuleRegistry.getEnforcing(...): '${name}' could not be found. ` + - 'Verify that a module by this name is registered in the native binary.', - ); + 'Verify that a module by this name is registered in the native binary.'; + + if (shouldReportLoadedModules()) { + message += 'Modules loaded: ' + JSON.stringify(moduleLoadHistory); + } + + invariant(module != null, message); return module; }