From b8c4df36c9744cb0e57bf8166e02f14e27c54d2f Mon Sep 17 00:00:00 2001 From: Christian Falch Date: Wed, 2 Dec 2026 11:16:14 +0100 Subject: [PATCH] Fix SwiftPM: native library added after setup is not compiled until a clean build When you add a native library to a SwiftPM app, xcodebuild resolves the package graph first. Then the "Sync SPM Autolinking" scheme pre-action adds the library to the Autolinked Package.swift. Xcode keeps the old package graph for the new manifest, so incremental builds never compile the library and the app fails at runtime (for example "RNSVGCircle is not registered"). Only a clean build fixed it. The pre-action now syncs only when the Autolinked manifest is missing. Once it exists, the "Sync SPM Autolinking" build phase syncs it. When that sync changes the manifest, the build fails once with "native modules changed. Build again." The next build compiles the library. Test: yarn jest packages/react-native/scripts/spm --no-cache -i (1264 passed, 0 failed). --- .../__tests__/generate-spm-xcodeproj-test.js | 45 ++++++++++++++++--- .../spm/__tests__/spm-lifecycle-test.js | 17 +++---- .../scripts/spm/generate-spm-xcodeproj.js | 31 ++++++++++--- 3 files changed, 69 insertions(+), 24 deletions(-) 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 5cc88888ad39..62ea3dad6378 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 @@ -382,6 +382,7 @@ describe('sync scripts', () => { let fakeNode; let rnDir; let syncLog; + let output; // Does to the generated dir what a real sync does: on a mismatch it prints // the errors, records them, refreshes the stamp, and exits 2. @@ -392,6 +393,7 @@ if [ "$1" = "--print" ]; then fi echo sync >> "$FAKE_SYNC_LOG" DIR="$SRCROOT/build/generated/autolinking" +[ -n "$FAKE_MANIFEST" ] && printf '%s' "$FAKE_MANIFEST" > "$DIR/Package.swift" date > "$DIR/.spm-sync-stamp" if [ -n "$FAKE_MISMATCH" ]; then echo "$FAKE_MISMATCH" >&2 @@ -438,9 +440,9 @@ rm -f "$DIR/.spm-plugin-mismatch" fs.rmSync(root, {recursive: true, force: true}); }); - function build({mismatch}) { + function build({mismatch, manifest = '', phase = script}) { fs.writeFileSync(syncLog, ''); - const result = spawnSync('/bin/bash', ['-c', script], { + const result = spawnSync('/bin/bash', ['-c', phase], { encoding: 'utf8', env: { PATH: process.env.PATH, @@ -450,9 +452,10 @@ rm -f "$DIR/.spm-plugin-mismatch" FAKE_RN_DIR: rnDir, FAKE_SYNC_LOG: syncLog, FAKE_MISMATCH: mismatch ? MISMATCH : '', + FAKE_MANIFEST: manifest, }, }); - const output = result.stdout + result.stderr; + output = result.stdout + result.stderr; return { status: result.status, errors: output.split(MISMATCH).length - 1, @@ -544,11 +547,43 @@ rm -f "$DIR/.spm-plugin-mismatch" syncs: 0, }); }); + + describe('the Autolinked manifest', () => { + const manifestPath = () => path.join(generatedDir, 'Package.swift'); + + it('the build phase fails when its sync changes the manifest', () => { + fs.writeFileSync(manifestPath(), 'RNScreens\n'); + const manifest = 'RNScreens\nRNSVG\n'; + expect(build({mismatch: false, manifest}).status).toBe(1); + expect(output).toContain( + 'error: React Native SwiftPM: native modules changed. Build again.', + ); + expect(build({mismatch: false, manifest}).status).toBe(0); + }); + + it('the build phase passes when its sync leaves the manifest unchanged', () => { + fs.writeFileSync(manifestPath(), 'RNScreens\n'); + expect(build({mismatch: false, manifest: 'RNScreens\n'})).toEqual({ + status: 0, + errors: 0, + syncs: 1, + }); + }); + + it('the pre-action does not sync when the manifest exists', () => { + fs.writeFileSync(manifestPath(), 'RNScreens\n'); + const preAction = buildSchemePreActionScript(baked); + expect(build({mismatch: false, phase: preAction})).toEqual({ + status: 0, + errors: 0, + syncs: 0, + }); + }); + }); }); - it('is deterministic, shared with the pre-action, and valid POSIX shell', () => { + it('is deterministic and valid POSIX shell', () => { expect(buildSyncAutolinkingScript(baked)).toBe(script); - expect(buildSchemePreActionScript(baked)).toBe(script); const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-sync-script-')); const file = path.join(dir, 'sync.sh'); fs.writeFileSync(file, 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 60a39ffa4649..b08641640349 100644 --- a/packages/react-native/scripts/spm/__tests__/spm-lifecycle-test.js +++ b/packages/react-native/scripts/spm/__tests__/spm-lifecycle-test.js @@ -71,7 +71,6 @@ const { createSpmFixtureApp, } = require('./spm-fixture-app'); const {checkSpmInvariants} = require('./spm-invariants'); -const {DOMParser} = require('@xmldom/xmldom'); const fs = require('node:fs'); const path = require('node:path'); @@ -198,19 +197,13 @@ function assertHermesCompilerResolves() { } } +// The pre-action exits early once the manifest exists; from then on the build +// phase script runs the same stale check. function syncPreActionScript() { - const scheme = new DOMParser().parseFromString( - fs.readFileSync(app.schemePath, 'utf8'), - 'text/xml', + return fs.readFileSync( + path.join(app.appRoot, 'ReactNativeSPM/Scripts/sync-autolinking.sh'), + 'utf8', ); - const actions = Array.from(scheme.getElementsByTagName('ActionContent')); - const preAction = actions.find( - action => action.getAttribute('title') === 'Sync SPM Autolinking', - ); - if (preAction == null) { - throw new Error('The scheme has no "Sync SPM Autolinking" pre-action'); - } - return preAction.getAttribute('scriptText'); } // Runs the scheme pre-action as Xcode would and reports whether its stale diff --git a/packages/react-native/scripts/spm/generate-spm-xcodeproj.js b/packages/react-native/scripts/spm/generate-spm-xcodeproj.js index f7f55d1c41a7..bf3bbe90fd85 100644 --- a/packages/react-native/scripts/spm/generate-spm-xcodeproj.js +++ b/packages/react-native/scripts/spm/generate-spm-xcodeproj.js @@ -940,17 +940,22 @@ fi `; } -// Scheme pre-action: re-run codegen + autolinking before package resolution. +// Scheme pre-action: syncs only while the Autolinked manifest is missing. function buildSchemePreActionScript( reactNativePath /*: string */, ) /*: string */ { - return `${nodeAndRnDirPreamble(reactNativePath)} + return `# Runs after package resolution: rewriting the manifest here goes stale. +if [ -f "$SRCROOT/build/generated/autolinking/Package.swift" ]; then + exit 0 +fi + +${nodeAndRnDirPreamble(reactNativePath)} ${syncStaleCheckAndDispatch()} `; } -// The in-target phase is only an autolinking safety net. Runtime framework +// The in-target phase keeps autolinking current. Runtime framework // selection is expressed entirely through build settings and the independent // Embed React Native Flavored Frameworks phase. function buildSyncAutolinkingScript( @@ -958,7 +963,19 @@ function buildSyncAutolinkingScript( ) /*: string */ { return `${nodeAndRnDirPreamble(reactNativePath)} +AUTOLINKED_MANIFEST="$SRCROOT/build/generated/autolinking/Package.swift" +OLD_SUM="" +if [ -f "$AUTOLINKED_MANIFEST" ]; then + OLD_SUM="$(cksum < "$AUTOLINKED_MANIFEST")" +fi + ${syncStaleCheckAndDispatch()} + +# Xcode planned this build with the old package graph; the next uses the new. +if [ -n "$OLD_SUM" ] && [ "$(cksum < "$AUTOLINKED_MANIFEST")" != "$OLD_SUM" ]; then + echo "error: React Native SwiftPM: native modules changed. Build again." + exit 1 +fi `; } // XML-attribute escape. The sync script uses `>` and `&` for redirection and @@ -1768,8 +1785,8 @@ function injectSpmIntoPbxproj( ), }; - // 6. The Sync SPM Autolinking build phase (safety net; the scheme pre-action - // is what fires before SPM resolution). Prepended so it runs before + // 6. The Sync SPM Autolinking build phase (keeps the Autolinked manifest + // current). Prepended so it runs before // Sources. We do NOT add a JS-bundle phase — an existing app already // bundles JS via its own phase. const syncPhaseUuid = mkUuid('PBXShellScriptBuildPhase', 'SyncAutolinking'); @@ -2217,8 +2234,8 @@ function addPreActionToScheme( } /** - * Ensure the app target's shared scheme runs the sync pre-action before SPM - * resolution. Updates the scheme that builds the target if one exists, + * Ensure the app target's shared scheme runs the sync pre-action before each + * build. Updates the scheme that builds the target if one exists, * otherwise creates a fresh shared scheme. Returns 'updated' | 'created' | * 'unchanged'. */