Skip to content
Open
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
9 changes: 9 additions & 0 deletions packages/react-native/scripts/spm/__docs__/spm-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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/<user>.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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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, '<plist><dict><key>');
expect(runStep().status).toBe(0);
expect(fs.readFileSync(settingsPath, 'utf8')).toBe('<plist><dict><key>');
});

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-'));
Expand Down
14 changes: 14 additions & 0 deletions packages/react-native/scripts/spm/__tests__/spm-lifecycle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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;
Expand Down Expand Up @@ -226,6 +228,7 @@ function preActionDispatchesSync() {
SRCROOT: app.appRoot,
PROJECT_DIR: app.appRoot,
PROJECT_FILE_PATH: app.xcodeprojPath,
USER: BUILD_USER,
},
},
);
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions packages/react-native/scripts/spm/generate-spm-xcodeproj.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<plist version="1.0"><dict><key>CompilationCachingSetting</key><string>Enable</string></dict></plist>' > "$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
Expand Down
Loading