Re-sync SwiftPM autolinking when a watched path appears (#58828)

Summary:
The SwiftPM build-time sync runs only when its stamp is older than one of the files it watches. A watched path that did not exist at the last sync was dropped from the watch list. So when that path appeared later, the sync did not run.

Examples:

- A plugin watches its precompiled-module folder. On a fresh clone, that folder does not exist yet, so later precompiles never trigger a sync.
- A dependency has no root `Package.swift` or `.react-native/` folder. When one appears later, for example after `npx react-native spm scaffold`, the dependency is detected differently, but the sync does not run.

This PR writes missing watch paths to a new file, `.spm-sync-watch-absent`. The stamp is stale when one of those paths now exists. The missing paths do not go to `.spm-sync-watch-paths`, because projects from older versions treat a missing path in that file as stale and would sync on every build. Those projects get this fix after running `npx react-native spm update` once.

The paths that can go to `.spm-sync-watch-absent`:

- a plugin `watchPaths` entry that does not exist yet
- the root `Package.swift` and `.react-native/` of each dependency, when they do not exist

The docs now tell plugins to list their precompiled-module folder in `watchPaths`, but only a folder that the Xcode build does not write to.

This PR does not watch `project.pbxproj`. Xcode rewrites that file on many edits in the IDE, so watching it would run a full sync after each edit. https://github.com/react/react-native/issues/58827 makes `.spm-injected.json` a watched file instead. Only `spm add` and `spm update` change that file.

This PR is independent of https://github.com/react/react-native/issues/58827. They can land in either order. Both change the generated sync check, so the second one to land needs a rebase.

## Changelog:

[IOS] [FIXED] - SwiftPM: the build-time sync now runs again when a watched path that did not exist at the last sync appears.

Pull Request resolved: https://github.com/react/react-native/pull/58828

Test Plan:
```
yarn jest --no-cache -i packages/react-native/scripts/spm/__tests__/
```

Result: 21 suites and 1010 tests pass.

- `generate-spm-autolinking-test.js`:
  - A missing watch path goes to `.spm-sync-watch-absent` only. This test failed before the change.
  - A dependency without a root `Package.swift` or `.react-native/` lists both paths as absent. The first version of this PR already did this, so the test passed at once. Removing the two paths from the code makes it fail.
  - `project.pbxproj` is not watched. This test failed before the change.
- `generate-spm-xcodeproj-test.js`: runs the shipped bash check under `/bin/bash`. The stamp is stale only when a path listed as absent now exists.

Reviewed By: andrewdacenko

Differential Revision: D125206241

Pulled By: cipolleschi

fbshipit-source-id: 2cb530615941b15adb27f8fcc4699812538e9f3d
(cherry picked from commit c89f4120ac)
(cherry picked from commit 8c5d3879a2)
This commit is contained in:
Christian Falch authored and t committed 2026-10-26 12:32:46 +00:00
1 parent 90ba1173d9
commit 6da65e822a
8 files changed
+149 -15

No files matched your search

@@ -144,11 +144,18 @@ manifests. On the next build the phase re-syncs when a watched **file** is newer
than the last sync, a watched **dir** has a newer child, or a watched path has
**vanished** (a rename forces a re-sync so the config error surfaces).
A watched path that does not exist yet is watched for **appearing**. When it is
created, the next build re-syncs. So a plugin that builds precompiled modules
should list their output folder, even when a fresh clone does not have it yet.
List only folders that the Xcode build itself does not write to, or every build
re-syncs.
Unlike `flavoredFrameworks`, watch paths are best-effort: a non-array is ignored
with a warning (never fatal), and each non-string / empty / **relative** entry
is dropped with a warning. Absolute-only, because the generated phase tests
these paths with no cwd context. The kept paths are folded into
`<outputDir>/.spm-sync-watch-paths` alongside RN's own, then deduped and sorted.
these paths with no cwd context. The kept paths are folded in with RN's own,
deduped, and sorted: paths that exist go to `<outputDir>/.spm-sync-watch-paths`,
and paths that do not exist yet go to `<outputDir>/.spm-sync-watch-absent`.
### `scriptPhases` — build-time shell phases on the app target
@@ -258,7 +258,7 @@ Because `deinit` drops the marker, it drops both.
| Path | Written by | Contents |
| ------------------------------ | ----------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `build/generated/ios/` | `add`, `update`, `sync`, `codegen` | Codegen output plus the SwiftPM codegen manifest (the `React-GeneratedCode` package). |
| `build/generated/autolinking/` | `add`, `update`, `sync` | `Package.swift`, `autolinking.json`, `packages/`, `libs/`, `headers/`, the `.spm-sync-stamp`, `.spm-sync-watch-paths`, and any `.spm-plugin-*.json` plugin manifests. |
| `build/generated/autolinking/` | `add`, `update`, `sync` | `Package.swift`, `autolinking.json`, `packages/`, `libs/`, `headers/`, the `.spm-sync-stamp`, `.spm-sync-watch-paths`, `.spm-sync-watch-absent`, and any `.spm-plugin-*.json` manifests. |
| `build/xcframeworks/` | `add`, `update`, `sync`, `download` | The `debug/` and `release/` flavor slots (symlinks into the cache), `ReactHeadersTarget/`, the headers-only xcframeworks, `Package.swift`, `flavored-frameworks.json`, `.artifact-stamp`. |
| `.build/`, `Package.resolved` | Xcode / SwiftPM | SwiftPM's own build directory and resolution file. Machine-specific. |
@@ -711,6 +711,11 @@ _existing_ set of generated packages current; they do not create the first one.
[plugin](./spm-autolinking-plugins.md#watchpaths--plugin-staleness-inputs)
`watchPaths`; a watched file that is newer, a watched dir with a newer
child, or a watched path that has **vanished** all mark stale
- every path in `.spm-sync-watch-absent` — watched paths that did not exist
at the last sync; one that **appears** marks stale. This includes each
dependency's root `Package.swift` and `.react-native/`: one that appears
later (e.g. after `spm scaffold`) changes self-managed detection. Projects
get this check after running `npx react-native spm update` once
2. If any input is newer (or the stamp is missing): runs
`npx react-native spm sync`, which re-executes autolinking + package
generation (downloading artifacts if the cache slot is incomplete) and writes
@@ -1879,7 +1879,7 @@ describe('main() — scriptPhases sidecar', () => {
// and deduped/sorted: (1) each module's source DIR; (2) each npm dep's
// checked-in root Package.swift (FILE) and .react-native/ (DIR) — threaded from
// the autolinking model's dep root, not derived by walking up; (3) plugin
// watchPaths. Nonexistent paths are filtered at emission time.
// watchPaths. Nonexistent paths go to .spm-sync-watch-absent instead.
// ---------------------------------------------------------------------------
describe('main() — .spm-sync-watch-paths emission', () => {
@@ -1920,8 +1920,8 @@ describe('main() — .spm-sync-watch-paths emission', () => {
fs.writeFileSync(path.join(fooDir, 'Foo.swift'), '// src\n');
// (C) A plugin-host dep contributing watchPaths: one existing absolute path
// (kept), one absent absolute path (filtered at emission), one relative
// path (dropped by invokePlugins).
// (kept), one absent absolute path (listed as absent), one relative path
// (dropped by invokePlugins).
const expoDir = path.join(appRoot, 'node_modules', 'expo');
fs.mkdirSync(path.join(expoDir, 'ios'), {recursive: true});
fs.writeFileSync(path.join(expoDir, 'ios', 'Expo.mm'), '// native\n');
@@ -1940,7 +1940,7 @@ describe('main() — .spm-sync-watch-paths emission', () => {
" productDependencies: [{name: 'ExpoModulesCore', package: 'ExpoModulesCore'}],",
' watchPaths: [',
" path.join(__dirname, 'Package.swift'),", // exists → kept
" path.join(__dirname, 'MISSING.swift'),", // absent → filtered at emission
" path.join(__dirname, 'MISSING.swift'),", // absent → listed as absent
" 'rel/manifest',", // relative → dropped by invokePlugins
' ],',
' };',
@@ -1948,6 +1948,14 @@ describe('main() — .spm-sync-watch-paths emission', () => {
].join('\n') + '\n',
);
// (D) The app's project file: Xcode rewrites it on every IDE edit, so
// watching it would re-sync after each one.
fs.mkdirSync(path.join(appRoot, 'App.xcodeproj'));
fs.writeFileSync(
path.join(appRoot, 'App.xcodeproj', 'project.pbxproj'),
'\n',
);
const autolinkDir = path.join(appRoot, 'build', 'generated', 'autolinking');
fs.mkdirSync(autolinkDir, {recursive: true});
fs.writeFileSync(
@@ -1972,6 +1980,9 @@ describe('main() — .spm-sync-watch-paths emission', () => {
// (C) plugin's existing absolute watchPath is kept.
expect(lines).toContain(path.join(expoDir, 'Package.swift'));
// (D) the app's project file is not watched.
expect(lines.some(l => l.includes('project.pbxproj'))).toBe(false);
// Filtered / dropped entries never reach the file.
expect(lines.some(l => l.includes('MISSING.swift'))).toBe(false);
expect(lines.some(l => l.includes('rel/manifest'))).toBe(false);
@@ -1979,6 +1990,54 @@ describe('main() — .spm-sync-watch-paths emission', () => {
// Deduped and sorted (stable, deterministic output).
expect(new Set(lines).size).toBe(lines.length);
expect([...lines].sort()).toEqual(lines);
expect(
fs.readFileSync(path.join(autolinkDir, '.spm-sync-watch-absent'), 'utf8'),
).toBe(path.join(expoDir, 'MISSING.swift') + '\n');
});
it('lists a dep root without Package.swift or .react-native/ as absent', () => {
const appRoot = fs.realpathSync(
fs.mkdtempSync(path.join(os.tmpdir(), 'spm-watch-absent-')),
);
created.push(appRoot);
const rnRoot = path.join(appRoot, 'rn');
fs.mkdirSync(rnRoot, {recursive: true});
fs.writeFileSync(
path.join(appRoot, 'package.json'),
JSON.stringify({name: 'app'}),
);
// Self-managed through ios/Package.swift, so nothing exists at the root.
const barDir = path.join(appRoot, 'node_modules', 'react-native-bar');
fs.mkdirSync(path.join(barDir, 'ios'), {recursive: true});
fs.writeFileSync(
path.join(barDir, 'ios', 'Package.swift'),
'// swift-tools-version:5.9\n// hand-authored\n',
);
fs.writeFileSync(path.join(barDir, 'ios', 'Bar.swift'), '// src\n');
const autolinkDir = path.join(appRoot, 'build', 'generated', 'autolinking');
fs.mkdirSync(autolinkDir, {recursive: true});
fs.writeFileSync(
path.join(autolinkDir, 'autolinking.json'),
JSON.stringify({
dependencies: {
'react-native-bar': {root: barDir, platforms: {ios: {}}},
},
}),
);
main(['--app-root', appRoot, '--react-native-root', rnRoot]);
expect(
fs.readFileSync(path.join(autolinkDir, '.spm-sync-watch-absent'), 'utf8'),
).toBe(
[
path.join(barDir, '.react-native'),
path.join(barDir, 'Package.swift'),
].join('\n') + '\n',
);
});
});
@@ -333,6 +333,42 @@ describe('sync scripts', () => {
});
});
it('re-syncs when a path listed as absent appears', () => {
const loop = /^ABSENT_FILE=[\s\S]*?^fi$/m.exec(script)?.[0];
expect(loop).toBeDefined();
const probe = srcRoot =>
execFileSync(
'/bin/bash',
[
'-c',
`set -euo pipefail\nSRCROOT="$1"\nSTALE=0\n${String(loop)}\necho "$STALE"\n`,
'probe',
srcRoot,
],
{encoding: 'utf8'},
).trim();
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-sync-absent-'));
try {
const autolinkDir = path.join(root, 'build/generated/autolinking');
fs.mkdirSync(autolinkDir, {recursive: true});
const watched = path.join(root, 'precompiled');
fs.mkdirSync(watched);
expect(probe(root)).toBe('0');
fs.writeFileSync(
path.join(autolinkDir, '.spm-sync-watch-absent'),
`${watched}\n`,
);
expect(probe(root)).toBe('1');
fs.rmdirSync(watched);
expect(probe(root)).toBe('0');
} finally {
fs.rmSync(root, {recursive: true, force: true});
}
});
it('is deterministic, shared with the pre-action, and valid POSIX shell', () => {
expect(buildSyncAutolinkingScript(baked)).toBe(script);
expect(buildSchemePreActionScript(baked)).toBe(script);
+2 -1
View File
@@ -65,7 +65,8 @@
* // Absolute paths (dirs OR files) the Xcode auto-sync build phase
* // should watch for staleness — e.g. the plugin dep's own
* // `Package.swift`, `expo-module.config.json`, and per-module
* // manifests. Folded into `.spm-sync-watch-paths`; a file edit or a
* // manifests. Folded into `.spm-sync-watch-paths` (or
* // `.spm-sync-watch-absent` while missing); a file edit or a
* // dir add/remove there re-triggers the sync. Relative/empty/non-string
* // entries are dropped with a warning; a non-array is ignored.
* watchPaths: ['/abs/path/Package.swift', '/abs/dir'],
@@ -1920,12 +1920,15 @@ function main(argv /*:: ?: Array<string> */) /*: void */ {
// stale until an unrelated install triggers a sync. The dep ROOT is
// threaded through the entry (entry.root, from the autolinking model),
// not derived by walking up from the (possibly nested) source dir.
// Both are listed even when absent: one that appears later (e.g. after
// `spm scaffold`) changes self-managed detection and needs a re-sync.
// 3. Plugin-contributed paths (e.g. Expo's own Package.swift / per-module
// manifests) — already validated absolute in invokePlugins.
// The phase distinguishes dir vs file with `-d`/`-f` at build time (no
// markers). The existsSync filter here is safe: these paths come from a
// successful sync so they exist now; a path that later VANISHES is caught at
// phase time against this file and forces a re-sync.
// markers). A listed path that later VANISHES forces a re-sync. Paths that do
// not exist yet go to .spm-sync-watch-absent, where one that APPEARS forces a
// re-sync; they stay out of this file because phases from older versions
// treat a missing listed path as stale.
const watchCandidates /*: Array<string> */ = [...entryAbsDirs.values()];
for (const entry of entries) {
const root = entry.root;
@@ -1936,14 +1939,23 @@ function main(argv /*:: ?: Array<string> */) /*: void */ {
watchCandidates.push(path.join(root, '.react-native'));
}
watchCandidates.push(...pluginWatchPaths);
const watchPaths = Array.from(new Set(watchCandidates))
.filter(p => p.length > 0 && fs.existsSync(p))
.sort();
const watchPaths /*: Array<string> */ = [];
const absentWatchPaths /*: Array<string> */ = [];
for (const p of Array.from(new Set(watchCandidates)).sort()) {
if (p.length > 0) {
(fs.existsSync(p) ? watchPaths : absentWatchPaths).push(p);
}
}
fs.writeFileSync(
path.join(outputDir, '.spm-sync-watch-paths'),
watchPaths.join('\n') + (watchPaths.length > 0 ? '\n' : ''),
'utf8',
);
fs.writeFileSync(
path.join(outputDir, '.spm-sync-watch-absent'),
absentWatchPaths.join('\n') + (absentWatchPaths.length > 0 ? '\n' : ''),
'utf8',
);
// AutolinkedAggregate is glue; needs at least one source file (Swift, so we
// sidestep the Obj-C public-headers-dir requirement).
@@ -785,6 +785,19 @@ if [ "$STALE" -eq 0 ] && [ -f "$WATCH_FILE" ]; then
done < "$WATCH_FILE"
fi
# Watched paths that did not exist at the last sync. One that APPEARS (e.g. a
# plugin's precompiled output folder) forces a re-sync.
ABSENT_FILE="$SRCROOT/build/generated/autolinking/.spm-sync-watch-absent"
if [ "$STALE" -eq 0 ] && [ -f "$ABSENT_FILE" ]; then
while IFS= read -r P; do
[ -z "$P" ] && continue
if [ -e "$P" ]; then
STALE=1
break
fi
done < "$ABSENT_FILE"
fi
# Check 2: codegen spec files changed via git (covers monorepo after git pull)
if [ "$STALE" -eq 0 ] && [ -f "$STAMP" ]; then
STAMP_TIME=$(stat -f %m "$STAMP" 2>/dev/null || stat -c %Y "$STAMP" 2>/dev/null || echo 0)
+2 -1
View File
@@ -324,7 +324,8 @@ export type PluginResult = {
flavoredFrameworks: Array<PluginFlavoredFramework>,
// Absolute paths (dirs or files) the Xcode auto-sync build phase should watch
// for staleness, e.g. the plugin dep's own `Package.swift` and per-module
// manifests. Folded into `.spm-sync-watch-paths` by main().
// manifests. Folded into `.spm-sync-watch-paths` (or `.spm-sync-watch-absent`
// while missing) by main().
watchPaths: Array<string>,
// Build-time shell phases for the app target, recorded to
// `.spm-plugin-script-phases.json` by main() and injected by `spm add`/