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
This commit is contained in:
Phil Pluckthun authored and React Native Bot committed 2026-04-13 03:23:23 +00:00
1 parent 401b1620dc
commit 76cf797f17
9 files changed
+70 -21

No files matched your search

@@ -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}`);
}
@@ -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,
);
@@ -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}`);
}
@@ -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}`);
}
@@ -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}`);
}
@@ -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}`);
}
@@ -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}`);
}
@@ -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,
);
@@ -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,
};