From 6265176c05997df327bdce89693f9449be4a5190 Mon Sep 17 00:00:00 2001 From: Christian Falch Date: Thu, 3 Dec 2026 22:49:59 +0100 Subject: [PATCH] Turn on Xcode compilation caching for SwiftPM package targets Xcode applies its compilation cache to Swift package targets only through the per-user workspace setting CompilationCachingSetting. Project and xcconfig build settings do not reach them. The Sync SPM Autolinking build phase now writes CompilationCachingSetting=Enable to the user's WorkspaceSettings.xcsettings when the key is missing. It keeps other keys, leaves an explicit value or a malformed file alone, never fails the build, and is turned off with RCT_SPM_SKIP_COMPILATION_CACHE_SETTING=1. Signed-off-by: Christian Falch --- .../scripts/spm/__docs__/spm-scripts.md | 9 +++ .../__tests__/generate-spm-xcodeproj-test.js | 81 +++++++++++++++++++ .../spm/__tests__/spm-lifecycle-test.js | 14 ++++ .../scripts/spm/generate-spm-xcodeproj.js | 13 +++ 4 files changed, 117 insertions(+) diff --git a/packages/react-native/scripts/spm/__docs__/spm-scripts.md b/packages/react-native/scripts/spm/__docs__/spm-scripts.md index eec6b2e703a1..2ebb6406749d 100644 --- a/packages/react-native/scripts/spm/__docs__/spm-scripts.md +++ b/packages/react-native/scripts/spm/__docs__/spm-scripts.md @@ -377,6 +377,7 @@ value, so **members you added to it by hand are lost**. | `RN_SPM_REMOTE_URL`, `RN_SPM_REMOTE_VERSION` | Reserved for the planned remote-package mode. Not supported. See [Remote-package mode (planned)](./spm-header-paths-contract.md#remote-package-mode-planned). | | `RN_DEP_VERSION` | Version of the `ReactNativeDependencies` artifact, or `nightly`. Defaults to the React Native version. | | `HERMES_VERSION` | Version of the Hermes artifact, `nightly`, or `latest-v1`. Defaults to the locally pinned `hermes-compiler` version, else `latest-v1`. | +| `RCT_SPM_SKIP_COMPILATION_CACHE_SETTING` | `1` stops the Sync build phase from turning on [compilation caching](#auto-sync) in your per-user workspace settings. | A tarball path that does not exist fails the download. Each tarball override applies to both flavors, so the Debug and Release slots get the same file. @@ -855,6 +856,14 @@ create the first one: see [Fresh clones & CI](#fresh-clones--ci). `build/xcframeworks/`. 4. If all inputs are fresh: exits immediately (~1ms). +**Compilation caching.** Xcode applies its compilation cache to Swift package +targets only through the per-user setting `CompilationCachingSetting` in +`MyApp.xcodeproj/project.xcworkspace/xcuserdata/.xcuserdatad/WorkspaceSettings.xcsettings`. +On every build, the phase sets it to `Enable` when the key is missing. It keeps +any value already set, and `deinit` leaves it. Xcode reads it from the next +build on. To opt out, set the key to `Disable` or set +`RCT_SPM_SKIP_COMPILATION_CACHE_SETTING=1`. + **Ordering.** As observed in an `xcodebuild -scheme … build` log on Xcode 26.6: | Step | Owner | diff --git a/packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js b/packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js index 62ea3dad6378..e6472d687b6f 100644 --- a/packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js +++ b/packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js @@ -27,6 +27,7 @@ const {execFileSync, spawnSync} = require('node:child_process'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); +const plist = require('plist'); const FRAMEWORK = { id: 'react', @@ -582,6 +583,86 @@ rm -f "$DIR/.spm-plugin-mismatch" }); }); + describe('the compilation cache workspace setting', () => { + const step = + /^[^\n]*RCT_SPM_SKIP_COMPILATION_CACHE_SETTING[\s\S]*?^fi$/m.exec( + script, + )?.[0]; + const itWithPlutil = process.platform === 'darwin' ? it : it.skip; + + let root; + let xcodeprojPath; + let settingsPath; + + const runStep = (env = {}) => + spawnSync('/bin/bash', ['-c', `set -euo pipefail\n${String(step)}\n`], { + encoding: 'utf8', + env: { + PATH: process.env.PATH, + PROJECT_FILE_PATH: xcodeprojPath, + USER: 'builder', + ...env, + }, + }); + const readSettings = () => + plist.parse(fs.readFileSync(settingsPath, 'utf8')); + const writeSettings = settings => { + fs.mkdirSync(path.dirname(settingsPath), {recursive: true}); + fs.writeFileSync(settingsPath, plist.build(settings)); + }; + + beforeEach(() => { + root = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-compile-cache-')); + xcodeprojPath = path.join(root, 'MyApp.xcodeproj'); + fs.mkdirSync(xcodeprojPath); + settingsPath = path.join( + xcodeprojPath, + 'project.xcworkspace/xcuserdata/builder.xcuserdatad/WorkspaceSettings.xcsettings', + ); + }); + + afterEach(() => { + fs.rmSync(root, {recursive: true, force: true}); + }); + + it('creates the settings file with only the setting when it is missing', () => { + expect(runStep().status).toBe(0); + expect(readSettings()).toEqual({CompilationCachingSetting: 'Enable'}); + }); + + itWithPlutil('adds the setting and keeps the other keys', () => { + writeSettings({BuildLocationStyle: 'UseAppPreferences'}); + expect(runStep().status).toBe(0); + expect(readSettings()).toEqual({ + BuildLocationStyle: 'UseAppPreferences', + CompilationCachingSetting: 'Enable', + }); + }); + + it('leaves a setting the user chose', () => { + writeSettings({CompilationCachingSetting: 'Disable'}); + const before = fs.readFileSync(settingsPath); + expect(runStep().status).toBe(0); + expect(fs.readFileSync(settingsPath)).toEqual(before); + }); + + it('leaves a malformed file untouched', () => { + fs.mkdirSync(path.dirname(settingsPath), {recursive: true}); + fs.writeFileSync(settingsPath, ''); + expect(runStep().status).toBe(0); + expect(fs.readFileSync(settingsPath, 'utf8')).toBe(''); + }); + + it('writes nothing when RCT_SPM_SKIP_COMPILATION_CACHE_SETTING=1', () => { + expect( + runStep({RCT_SPM_SKIP_COMPILATION_CACHE_SETTING: '1'}).status, + ).toBe(0); + expect( + fs.existsSync(path.join(xcodeprojPath, 'project.xcworkspace')), + ).toBe(false); + }); + }); + it('is deterministic and valid POSIX shell', () => { expect(buildSyncAutolinkingScript(baked)).toBe(script); const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-sync-script-')); diff --git a/packages/react-native/scripts/spm/__tests__/spm-lifecycle-test.js b/packages/react-native/scripts/spm/__tests__/spm-lifecycle-test.js index b08641640349..49bbd8678bc5 100644 --- a/packages/react-native/scripts/spm/__tests__/spm-lifecycle-test.js +++ b/packages/react-native/scripts/spm/__tests__/spm-lifecycle-test.js @@ -73,6 +73,7 @@ const { const {checkSpmInvariants} = require('./spm-invariants'); const fs = require('node:fs'); const path = require('node:path'); +const plist = require('plist'); const {spawnSync} = jest.requireActual('node:child_process'); @@ -90,6 +91,7 @@ const SPM_ENVIRONMENT = [ // Rewritten with the time of every sync, by design. const SYNC_STAMP = 'build/generated/autolinking/.spm-sync-stamp'; const LIBS_DIR = 'build/generated/autolinking/libs'; +const BUILD_USER = 'spm-lifecycle'; const [CURRENT_VERSION, NEXT_VERSION] = ARTIFACT_VERSIONS; let app; @@ -226,6 +228,7 @@ function preActionDispatchesSync() { SRCROOT: app.appRoot, PROJECT_DIR: app.appRoot, PROJECT_FILE_PATH: app.xcodeprojPath, + USER: BUILD_USER, }, }, ); @@ -410,6 +413,17 @@ describe('spm lifecycle on a fixture app', () => { }); }); + it('turns on Xcode compilation caching for the user who builds', () => { + preActionDispatchesSync(); + const settings = path.join( + app.xcodeprojPath, + `project.xcworkspace/xcuserdata/${BUILD_USER}.xcuserdatad/WorkspaceSettings.xcsettings`, + ); + expect(plist.parse(fs.readFileSync(settings, 'utf8'))).toEqual({ + CompilationCachingSetting: 'Enable', + }); + }); + it('fails sync with exit 2 when a plugin adds a framework the project does not link', async () => { app.setPluginFrameworks(PLUGIN_FRAMEWORKS); try { diff --git a/packages/react-native/scripts/spm/generate-spm-xcodeproj.js b/packages/react-native/scripts/spm/generate-spm-xcodeproj.js index bf3bbe90fd85..1cb4c5cea5a4 100644 --- a/packages/react-native/scripts/spm/generate-spm-xcodeproj.js +++ b/packages/react-native/scripts/spm/generate-spm-xcodeproj.js @@ -963,6 +963,19 @@ function buildSyncAutolinkingScript( ) /*: string */ { return `${nodeAndRnDirPreamble(reactNativePath)} +# Xcode applies compilation caching to Swift package targets only through this +# per-user setting; project and xcconfig build settings do not reach them. +if [ "\${RCT_SPM_SKIP_COMPILATION_CACHE_SETTING:-}" != "1" ] && [ -n "\${PROJECT_FILE_PATH:-}" ]; then + WORKSPACE_SETTINGS="$PROJECT_FILE_PATH/project.xcworkspace/xcuserdata/\${USER:-$(id -un 2>/dev/null || true)}.xcuserdatad/WorkspaceSettings.xcsettings" + if [ ! -e "$WORKSPACE_SETTINGS" ]; then + { mkdir -p "$(dirname "$WORKSPACE_SETTINGS")" && + echo 'CompilationCachingSettingEnable' > "$WORKSPACE_SETTINGS"; } 2>/dev/null || true + elif ! plutil -extract CompilationCachingSetting xml1 -o - "$WORKSPACE_SETTINGS" >/dev/null 2>&1 && + plutil -lint -s "$WORKSPACE_SETTINGS" >/dev/null 2>&1; then + plutil -insert CompilationCachingSetting -string Enable "$WORKSPACE_SETTINGS" >/dev/null 2>&1 || true + fi +fi + AUTOLINKED_MANIFEST="$SRCROOT/build/generated/autolinking/Package.swift" OLD_SUM="" if [ -f "$AUTOLINKED_MANIFEST" ]; then