From 76cf797f172c75fd5b3e0ef571f8ca1054a4a9c4 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Tue, 3 Mar 2026 03:19:14 -0800 Subject: [PATCH] Update `generate-artifacts-executor` scripts to skip write/cp for unchanged outputs (#55532) Summary: > Side-note: I've been maintaining branches to test these changes for 3 months since around 0.83/0.84, but was waiting for other changes in `scripts/codegen` to stabilise and for 0.84 to land to not interfere with other changes, and to instead target 0.85. Currently, the codegen scripts seem to interfere with Xcode's caching. This can be validated against any app by closing and re-opening Xcode and rebuilding an app that's already been built. Many source files and libraries affected by codegen will start re-building from scratch, skipping the cache. The changes in https://github.com/facebook/react-native/commit/c0290329cdb1771ec087c8552049a287c67259c6 skip writing to a temporary path and let the codegen script directly write to the output paths, since `moveOutputs` calling `cp` is skipped. However, the codegen scripts still write and copy files to the output directory, even if they exist and haven't changed. This change will replace `writeFileSync` and `cpSync` calls with wrappers that skip writing if the target exists and is identical. ## Changelog: [IOS] [INTERNAL] - Skip writing codegen outputs when outputs are unchanged Pull Request resolved: https://github.com/facebook/react-native/pull/55532 Test Plan: I've been mainly testing this in [`expo/expo`'s `notification-tester`](https://github.com/expo/expo/tree/main/apps/notification-tester) since it's a small app with few dependencies and builds relatively quickly for testing. - Build: etc, `pod install`, `xed ios`, build in Xcode - Rebuild (ground-truth): close and re-open Xcode, build again - Copy new scripts into `node_modules/react-native/scripts/codegen/generate-artifacts-executor` - Rebuild: close and re-open Xcode, build again - Rebuild (2): close and re-open Xcode, build again The re-build timings and target files can then be compared between "Rebuild (ground-truth)" and "Rebuild (2)". The latter will have a very high cache-hit rate and the build completes in about ~10s (instead of ~20s) skipping almost all compilation. I've verified that running `pod install` with the new scripts still works. The re-built files in that case are identical between the old and new scripts. Reviewed By: cortinico Differential Revision: D93099065 Pulled By: cipolleschi fbshipit-source-id: c3da043935d7056990b11204868aaa62ce6b0f7d --- .../generateAppDependencyProvider.js | 8 ++-- .../generateCustomURLHandlers.js | 6 +-- .../generateNativeCode.js | 5 +- .../generatePackageSwift.js | 4 +- .../generateRCTModuleProviders.js | 5 +- .../generateRCTThirdPartyComponents.js | 5 +- .../generateReactCodegenPodspec.js | 4 +- ...eModulesRequiringMainQueueSetupProvider.js | 6 +-- .../generate-artifacts-executor/utils.js | 48 +++++++++++++++++++ 9 files changed, 70 insertions(+), 21 deletions(-) diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateAppDependencyProvider.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateAppDependencyProvider.js index bf1a24b4732..7c7383ef799 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateAppDependencyProvider.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateAppDependencyProvider.js @@ -10,7 +10,7 @@ 'use strict'; const {TEMPLATES_FOLDER_PATH, packageJson} = require('./constants'); -const {codegenLog} = require('./utils'); +const {codegenLog, writeFileSyncIfChanged} = require('./utils'); const fs = require('fs'); const path = require('path'); @@ -38,7 +38,7 @@ function generateAppDependencyProvider(outputDir /*: string */) { 'utf8', ); const finalPathH = path.join(outputDir, 'RCTAppDependencyProvider.h'); - fs.writeFileSync(finalPathH, templateH); + writeFileSyncIfChanged(finalPathH, templateH); codegenLog(`Generated artifact: ${finalPathH}`); const templateMM = fs.readFileSync( @@ -46,7 +46,7 @@ function generateAppDependencyProvider(outputDir /*: string */) { 'utf8', ); const finalPathMM = path.join(outputDir, 'RCTAppDependencyProvider.mm'); - fs.writeFileSync(finalPathMM, templateMM); + writeFileSyncIfChanged(finalPathMM, templateMM); codegenLog(`Generated artifact: ${finalPathMM}`); // Generate the podspec file @@ -58,7 +58,7 @@ function generateAppDependencyProvider(outputDir /*: string */) { outputDir, 'ReactAppDependencyProvider.podspec', ); - fs.writeFileSync(finalPathPodspec, templatePodspec); + writeFileSyncIfChanged(finalPathPodspec, templatePodspec); codegenLog(`Generated podspec: ${finalPathPodspec}`); } diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateCustomURLHandlers.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateCustomURLHandlers.js index a81c00faaa1..99f6c10ed20 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateCustomURLHandlers.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateCustomURLHandlers.js @@ -11,7 +11,7 @@ 'use strict'; const {TEMPLATES_FOLDER_PATH} = require('./constants'); -const {parseiOSAnnotations} = require('./utils'); +const {parseiOSAnnotations, writeFileSyncIfChanged} = require('./utils'); const fs = require('fs'); const path = require('path'); @@ -105,13 +105,13 @@ function generateCustomURLHandlers( fs.mkdirSync(outputDir, {recursive: true}); - fs.writeFileSync( + writeFileSyncIfChanged( path.join(outputDir, 'RCTModulesConformingToProtocolsProvider.mm'), finalMMFile, ); const templateH = fs.readFileSync(MODULES_PROTOCOLS_H_TEMPLATE_PATH, 'utf8'); - fs.writeFileSync( + writeFileSyncIfChanged( path.join(outputDir, 'RCTModulesConformingToProtocolsProvider.h'), templateH, ); diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateNativeCode.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateNativeCode.js index 1e15509f724..d8db33f00e3 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateNativeCode.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateNativeCode.js @@ -12,7 +12,7 @@ const generateSpecsCLIExecutor = require('../generate-specs-cli-executor'); const {CORE_LIBRARIES_WITH_OUTPUT_FOLDER} = require('./constants'); -const {codegenLog} = require('./utils'); +const {codegenLog, cpSyncRecursiveIfChanged} = require('./utils'); const fs = require('fs'); const os = require('os'); const path = require('path'); @@ -63,8 +63,7 @@ function generateCode( const outputDir = reactNativeCoreLibraryOutputPath(libraryName, platform) ?? outputPath; fs.mkdirSync(outputDir, {recursive: true}); - // $FlowFixMe[prop-missing] - `fs.cpSync` is missing in Flow libdefs. - fs.cpSync(tmpOutputDir, outputDir, {recursive: true}); + cpSyncRecursiveIfChanged(tmpOutputDir, outputDir); codegenLog(`Generated artifacts: ${outputDir}`); } diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generatePackageSwift.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generatePackageSwift.js index 3e669401aa8..8eaf9099e81 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generatePackageSwift.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generatePackageSwift.js @@ -10,7 +10,7 @@ 'use strict'; const {TEMPLATES_FOLDER_PATH} = require('./constants'); -const {codegenLog} = require('./utils'); +const {codegenLog, writeFileSyncIfChanged} = require('./utils'); const fs = require('fs'); const path = require('path'); @@ -35,7 +35,7 @@ function generatePackageSwift( path.relative(fullOutputPath, reactNativePath), ); const finalPathH = path.join(outputDir, 'Package.swift'); - fs.writeFileSync(finalPathH, templateH); + writeFileSyncIfChanged(finalPathH, templateH); codegenLog(`Generated artifact: ${finalPathH}`); } diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTModuleProviders.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTModuleProviders.js index 1c8051db5ba..b5b6ff52bfc 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTModuleProviders.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTModuleProviders.js @@ -14,6 +14,7 @@ const { codegenLog, isReactNativeCoreLibrary, parseiOSAnnotations, + writeFileSyncIfChanged, } = require('./utils'); const fs = require('fs'); const path = require('path'); @@ -39,7 +40,7 @@ function generateRCTModuleProviders( codegenLog('Generating RCTModulesProvider.h'); const templateH = fs.readFileSync(MODULE_PROVIDERS_H_TEMPLATE_PATH, 'utf8'); const finalPathH = path.join(outputDir, 'RCTModuleProviders.h'); - fs.writeFileSync(finalPathH, templateH); + writeFileSyncIfChanged(finalPathH, templateH); codegenLog(`Generated artifact: ${finalPathH}`); codegenLog('Generating RCTModuleProviders.mm'); @@ -112,7 +113,7 @@ function generateRCTModuleProviders( .readFileSync(MODULE_PROVIDERS_MM_TEMPLATE_PATH, 'utf8') .replace(/{moduleMapping}/, modulesMapping); const finalPathMM = path.join(outputDir, 'RCTModuleProviders.mm'); - fs.writeFileSync(finalPathMM, templateMM); + writeFileSyncIfChanged(finalPathMM, templateMM); codegenLog(`Generated artifact: ${finalPathMM}`); } 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 5c74af0d616..1bbfd760deb 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateRCTThirdPartyComponents.js @@ -15,6 +15,7 @@ const { codegenLog, isReactNativeCoreLibrary, parseiOSAnnotations, + writeFileSyncIfChanged, } = require('./utils'); const fs = require('fs'); const path = require('path'); @@ -41,7 +42,7 @@ function generateRCTThirdPartyComponents( 'utf8', ); const finalPathH = path.join(outputDir, 'RCTThirdPartyComponentsProvider.h'); - fs.writeFileSync(finalPathH, templateH); + writeFileSyncIfChanged(finalPathH, templateH); codegenLog(`Generated artifact: ${finalPathH}`); codegenLog('Generating RCTThirdPartyComponentsProvider.mm'); @@ -150,7 +151,7 @@ function generateRCTThirdPartyComponents( outputDir, 'RCTThirdPartyComponentsProvider.mm', ); - fs.writeFileSync(finalPathMM, templateMM); + writeFileSyncIfChanged(finalPathMM, templateMM); codegenLog(`Generated artifact: ${finalPathMM}`); } diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js index 0017bcd41e5..f12e7c4a406 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js @@ -15,7 +15,7 @@ const { TEMPLATES_FOLDER_PATH, packageJson, } = require('./constants'); -const {codegenLog} = require('./utils'); +const {codegenLog, writeFileSyncIfChanged} = require('./utils'); const {execSync} = require('child_process'); const fs = require('fs'); const path = require('path'); @@ -39,7 +39,7 @@ function generateReactCodegenPodspec( .replace(/{input-files}/, inputFiles) .replace(/{codegen-script}/, codegenScript); const finalPathPodspec = path.join(outputPath, 'ReactCodegen.podspec'); - fs.writeFileSync(finalPathPodspec, finalPodspec); + writeFileSyncIfChanged(finalPathPodspec, finalPodspec); codegenLog(`Generated podspec: ${finalPathPodspec}`); } diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateUnstableModulesRequiringMainQueueSetupProvider.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateUnstableModulesRequiringMainQueueSetupProvider.js index 4d2ea86017e..f2505a8f014 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/generateUnstableModulesRequiringMainQueueSetupProvider.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/generateUnstableModulesRequiringMainQueueSetupProvider.js @@ -11,7 +11,7 @@ 'use strict'; const {TEMPLATES_FOLDER_PATH} = require('./constants'); -const {parseiOSAnnotations} = require('./utils'); +const {parseiOSAnnotations, writeFileSyncIfChanged} = require('./utils'); const fs = require('fs'); const path = require('path'); @@ -70,7 +70,7 @@ function generateUnstableModulesRequiringMainQueueSetupProvider( fs.mkdirSync(outputDir, {recursive: true}); - fs.writeFileSync( + writeFileSyncIfChanged( path.join( outputDir, 'RCTUnstableModulesRequiringMainQueueSetupProvider.mm', @@ -82,7 +82,7 @@ function generateUnstableModulesRequiringMainQueueSetupProvider( UNSTABLE_MODULES_REQUIRING_MAIN_QUEUE_SETUP_PROVIDER_H_TEMPLATE_PATH, 'utf8', ); - fs.writeFileSync( + writeFileSyncIfChanged( path.join(outputDir, 'RCTUnstableModulesRequiringMainQueueSetupProvider.h'), templateH, ); 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 4649fe29da5..98ac2eb1c17 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -485,6 +485,52 @@ function findReactNativeRootPath(projectRoot /* : string */) /* : string */ { return path.dirname(reactNativePackageJsonPath); } +function writeFileSyncIfChanged( + targetPath /*: string */, + contents /*: string */, +) { + try { + const oldContents = fs.readFileSync(targetPath, 'utf8'); + if (oldContents === contents) { + return; + } + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + fs.writeFileSync(targetPath, contents); +} + +function cpSyncRecursiveIfChanged( + sourcePath /*: string */, + targetPath /*: string */, +) { + fs.cpSync(sourcePath, targetPath, { + recursive: true, + force: true, + preserveTimestamps: true, + filter(src /*: string */, dest /*: string */) { + try { + const stat = fs.statSync(src); + if (!stat.isFile()) { + return true; + } else { + const oldContents = fs.readFileSync(dest, 'utf8'); + const newContents = fs.readFileSync(src, 'utf8'); + return oldContents !== newContents; + } + } catch (error) { + if (error.code !== 'ENOENT' && error.code !== 'EISDIR') { + throw error; + } else { + return true; + } + } + }, + }); +} + module.exports = { buildCodegenIfNeeded, pkgJsonIncludesGeneratedCode, @@ -499,4 +545,6 @@ module.exports = { readReactNativeConfig, findDisabledLibrariesByPlatform, findReactNativeRootPath, + writeFileSyncIfChanged, + cpSyncRecursiveIfChanged, };