From 5ee2fb12f7317896a81d0bb306dc8f0775dcc6b2 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Thu, 28 Aug 2025 14:26:21 +0100 Subject: [PATCH 1/4] Use autolinking react-native-config output in iOS artifacts generator --- .../scripts/cocoapods/autolinking.rb | 6 +++ .../scripts/cocoapods/codegen_utils.rb | 2 +- .../generate-artifacts-executor/index.js | 6 ++- .../generate-artifacts-executor/utils.js | 43 ++++++++++++++++--- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/autolinking.rb b/packages/react-native/scripts/cocoapods/autolinking.rb index b2e5600bc5d386..d8e9c7cec2f57c 100644 --- a/packages/react-native/scripts/cocoapods/autolinking.rb +++ b/packages/react-native/scripts/cocoapods/autolinking.rb @@ -40,6 +40,12 @@ def list_native_modules!(config_command) packages = config["dependencies"] ios_project_root = Pathname.new(config["project"]["ios"]["sourceDir"]) react_native_path = Pathname.new(config["reactNativePath"]) + codegen_output_path = ios_project_root.join("build/generated/autolinking/autolinking.json") + + # Write autolinking react-native-config output to codegen folder + FileUtils.mkdir_p(File.dirname(codegen_output_path)) + File.write(codegen_output_path, json) + found_pods = [] packages.each do |package_name, package| diff --git a/packages/react-native/scripts/cocoapods/codegen_utils.rb b/packages/react-native/scripts/cocoapods/codegen_utils.rb index 3ce12e28ebc7e6..3c51ad584e1187 100644 --- a/packages/react-native/scripts/cocoapods/codegen_utils.rb +++ b/packages/react-native/scripts/cocoapods/codegen_utils.rb @@ -87,7 +87,7 @@ def self.clean_up_build_folder(rn_path, codegen_dir, dir_manager: Dir, file_mana codegen_path = file_manager.join(ios_folder, codegen_dir) return if !dir_manager.exist?(codegen_path) - FileUtils.rm_rf(dir_manager.glob("#{codegen_path}/*")) + FileUtils.rm_rf("#{codegen_path}") base_provider_path = file_manager.join(rn_path, 'React', 'Fabric', 'RCTThirdPartyFabricComponentsProvider') FileUtils.rm_rf("#{base_provider_path}.h") FileUtils.rm_rf("#{base_provider_path}.mm") 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 002273410bd637..a3c52e39f386c6 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js @@ -86,10 +86,14 @@ function execute( buildCodegenIfNeeded(); } - const reactNativeConfig = readReactNativeConfig(projectRoot); + const reactNativeConfig = readReactNativeConfig( + projectRoot, + baseOutputPath, + ); const codegenEnabledLibraries = findCodegenEnabledLibraries( pkgJson, projectRoot, + baseOutputPath, reactNativeConfig, ); 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 e833a5921620fa..fd30e006987a3f 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -97,15 +97,40 @@ function cleanupEmptyFilesAndFolders(filepath /*: string */) { } } -function readReactNativeConfig(projectRoot /*: string */) /*: $FlowFixMe */ { - const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js'); +function readGeneratedReactNativeConfig( + baseOutputPath /*: string */, +) /*: $FlowFixMe */ { + // NOTE: Generated by scripts/cocoapods/autolinking.rb in list_native_modules (called by use_native_modules) + const rnConfigGeneratedPath = path.resolve( + baseOutputPath, + 'build/generated/autolinking/autolinking.json', + ); + if (fs.existsSync(rnConfigGeneratedPath)) { + /* $FlowFixMe */ + return require(rnConfigGeneratedPath); + } else { + console.warn( + `Could not find generated React Native config output at: ${rnConfigGeneratedPath}`, + ); + return null; + } +} - if (!fs.existsSync(rnConfigFilePath)) { +function readReactNativeConfig( + projectRoot /*: string */, + baseOutputPath /*: string */, +) /*: $FlowFixMe */ { + const rnGeneratedConfig = readGeneratedReactNativeConfig(baseOutputPath); + const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js'); + if (rnGeneratedConfig) { + return rnGeneratedConfig; + } else if (fs.existsSync(rnConfigFilePath)) { + /* $FlowFixMe */ + return require(rnConfigFilePath); + } else { + console.warn(`Could not find React Native config at: ${rnConfigFilePath}`); return {}; } - - // $FlowFixMe[unsupported-syntax] - return require(rnConfigFilePath); } /** @@ -114,8 +139,14 @@ function readReactNativeConfig(projectRoot /*: string */) /*: $FlowFixMe */ { function findCodegenEnabledLibraries( pkgJson /*: $FlowFixMe */, projectRoot /*: string */, + baseOutputPath /*: string */, reactNativeConfig /*: $FlowFixMe */, ) /*: Array<$FlowFixMe> */ { + if (!!readGeneratedReactNativeConfig(baseOutputPath)) { + // If we ran autolinking, we shouldn't try to run our own "autolinking-like" + // library discovery + return findLibrariesFromReactNativeConfig(projectRoot, reactNativeConfig); + } const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot); if (pkgJsonIncludesGeneratedCode(pkgJson)) { return projectLibraries; From 8bc910194f87bc196df5aefef48ce479bf6e6c82 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Thu, 28 Aug 2025 17:57:48 +0100 Subject: [PATCH 2/4] Always add `findProjectRootLibraries` --- .../generate-artifacts-executor/utils.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 fd30e006987a3f..9b2fd53fece589 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -142,20 +142,20 @@ function findCodegenEnabledLibraries( baseOutputPath /*: string */, reactNativeConfig /*: $FlowFixMe */, ) /*: Array<$FlowFixMe> */ { - if (!!readGeneratedReactNativeConfig(baseOutputPath)) { - // If we ran autolinking, we shouldn't try to run our own "autolinking-like" - // library discovery - return findLibrariesFromReactNativeConfig(projectRoot, reactNativeConfig); - } const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot); if (pkgJsonIncludesGeneratedCode(pkgJson)) { return projectLibraries; } else { - return [ - ...projectLibraries, - ...findExternalLibraries(pkgJson, projectRoot), + const libraries = [...projectLibraries]; + // If we ran autolinking, we shouldn't try to run our own "autolinking-like" + // library discovery + if (!readGeneratedReactNativeConfig(baseOutputPath)) { + libraries.push(...findExternalLibraries(pkgJson, projectRoot)); + } + libraries.push( ...findLibrariesFromReactNativeConfig(projectRoot, reactNativeConfig), - ]; + ); + return libraries; } } From b03cefee32d23b14cfd785c00b4df29c44e167ad Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Mon, 1 Sep 2025 19:34:49 +0100 Subject: [PATCH 3/4] Rename variables for clarity --- .../generate-artifacts-executor/utils.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 9b2fd53fece589..47e6caa5d83136 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -97,20 +97,20 @@ function cleanupEmptyFilesAndFolders(filepath /*: string */) { } } -function readGeneratedReactNativeConfig( +function readGeneratedAutolinkingOutput( baseOutputPath /*: string */, ) /*: $FlowFixMe */ { // NOTE: Generated by scripts/cocoapods/autolinking.rb in list_native_modules (called by use_native_modules) - const rnConfigGeneratedPath = path.resolve( + const autolinkingGeneratedPath = path.resolve( baseOutputPath, 'build/generated/autolinking/autolinking.json', ); - if (fs.existsSync(rnConfigGeneratedPath)) { + if (fs.existsSync(autolinkingGeneratedPath)) { /* $FlowFixMe */ - return require(rnConfigGeneratedPath); + return require(autolinkingGeneratedPath); } else { console.warn( - `Could not find generated React Native config output at: ${rnConfigGeneratedPath}`, + `Could not find generated autolinking output at: ${autolinkingGeneratedPath}`, ); return null; } @@ -120,10 +120,10 @@ function readReactNativeConfig( projectRoot /*: string */, baseOutputPath /*: string */, ) /*: $FlowFixMe */ { - const rnGeneratedConfig = readGeneratedReactNativeConfig(baseOutputPath); + const autolinkingOutput = readGeneratedAutolinkingOutput(baseOutputPath); const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js'); - if (rnGeneratedConfig) { - return rnGeneratedConfig; + if (autolinkingOutput) { + return autolinkingOutput; } else if (fs.existsSync(rnConfigFilePath)) { /* $FlowFixMe */ return require(rnConfigFilePath); @@ -149,7 +149,7 @@ function findCodegenEnabledLibraries( const libraries = [...projectLibraries]; // If we ran autolinking, we shouldn't try to run our own "autolinking-like" // library discovery - if (!readGeneratedReactNativeConfig(baseOutputPath)) { + if (!readGeneratedAutolinkingOutput(baseOutputPath)) { libraries.push(...findExternalLibraries(pkgJson, projectRoot)); } libraries.push( From a9ab883358203a289617d93f4edd54b6478a22a3 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Tue, 2 Sep 2025 08:25:35 +0100 Subject: [PATCH 4/4] Fix flow/test errors and replace output with codegenLog --- .../scripts/codegen/generate-artifacts-executor/utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 47e6caa5d83136..5cb42c28e70e14 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -106,10 +106,10 @@ function readGeneratedAutolinkingOutput( 'build/generated/autolinking/autolinking.json', ); if (fs.existsSync(autolinkingGeneratedPath)) { - /* $FlowFixMe */ + // $FlowFixMe[unsupported-syntax] return require(autolinkingGeneratedPath); } else { - console.warn( + codegenLog( `Could not find generated autolinking output at: ${autolinkingGeneratedPath}`, ); return null; @@ -125,10 +125,10 @@ function readReactNativeConfig( if (autolinkingOutput) { return autolinkingOutput; } else if (fs.existsSync(rnConfigFilePath)) { - /* $FlowFixMe */ + // $FlowFixMe[unsupported-syntax] return require(rnConfigFilePath); } else { - console.warn(`Could not find React Native config at: ${rnConfigFilePath}`); + codegenLog(`Could not find React Native config at: ${rnConfigFilePath}`); return {}; } }