Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 5 additions & 12 deletions packages/react-native/scripts/spm/__tests__/spm-lifecycle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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
Expand Down
31 changes: 24 additions & 7 deletions packages/react-native/scripts/spm/generate-spm-xcodeproj.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,25 +940,42 @@ 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(
reactNativePath /*: string */,
) /*: 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
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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'.
*/
Expand Down
Loading