Skip to content

[0.87] Stop re-creating library package roots on every sync - #59500

Open
chrfalch wants to merge 1 commit into
0.87-stablefrom
chrfalch/0.87-port-58597
Open

chrfalch wants to merge 1 commit into
0.87-stablefrom
chrfalch/0.87-port-58597

Conversation

@chrfalch

@chrfalch chrfalch commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Port of 97cc934 (#58597, "Stop re-creating library package roots on every sync") to 0.87-stable. That commit already landed in main. A plain cherry-pick conflicts, so this PR resolves the conflict.

SwiftPM autolinking removed and re-created each library's package root on every sync. For a library that ships its own Package.swift, Xcode then reported "Missing package product" and the build failed. This change keeps the existing package roots.

The only conflict is in the test file generate-spm-autolinking-test.js. The new tests on main use a useTempApps() helper from #58290, which is not on 0.87-stable. This port writes the same setup and cleanup inline. The production code is the same as on main.

This is PR 1 of 3 in a stack. Each PR contains the commits of the PRs before it. Merge them in order:

  1. [0.87] Stop re-creating library package roots on every sync #59500 (port of [iOS][SPM] Stop re-creating library package roots on every sync #58597) Stop re-creating library package roots on every sync ← this PR
  2. [0.87] Encode line breaks in SwiftPM scheme pre-action scriptText #59501 (port of Encode line breaks in SwiftPM scheme pre-action scriptText #58826) Encode line breaks in SwiftPM scheme pre-action scriptText
  3. [0.87] Re-sync SwiftPM autolinking when a watched path appears #59502 (port of Re-sync SwiftPM autolinking when a watched path appears #58828) Re-sync SwiftPM autolinking when a watched path appears

Pick request: reactwg/react-native-releases#1473

Changelog:

[IOS] [FIXED] - Stop SwiftPM autolinking from recreating library package roots on every sync, which broke Xcode builds of apps using libraries that ship their own Package.swift

Test Plan:

  • git range-diff against 97cc934: the only differences are the inline test setup and one context line.
  • yarn jest --no-cache -i packages/react-native/scripts/spm on this branch: 819 of 819 tests pass.
  • prettier --check and eslint --max-warnings 0 on the changed files: no problems.
  • CI on this PR.

Summary:
Building an app in Xcode fails when it autolinks a library that ships its own `Package.swift`, with one error per such library:

```
Missing package product 'reactnativeskia_ReactNativeSkia.ReactNativeSkia'
Missing package product 'reactnativesafeareacontext_ReactNativeSafeAreaContext.ReactNativeSafeAreaContext'
```

The same project builds fine from the command line. Two problems combine.

**The sync destroys package roots Xcode has already loaded.** Autolinking writes one symlink per self-managed library into `build/generated/autolinking/libs/<Name>`, and Xcode treats each as a local Swift package root. The generator deleted that whole directory and recreated it on every run, even when the generated output was byte-for-byte identical. Measured on a real app, across one no-op sync: the `libs/` inode changed from 1913513750 to 1913930645, `libs/ReactNativeSkia` from 1913513937 to 1913930649, while the generated `Package.swift` kept the same MD5. Recreating a package root that Xcode has already resolved is what produces the error above.

**Xcode's own bookkeeping made the sync run every time.** The "Sync SPM Autolinking" build phase re-syncs when a watched input looks newer than its stamp, and it checked each library's whole directory with `find <dir> -newer <stamp>`. Xcode writes its per-user scheme state inside that directory, at `<lib>/.swiftpm/xcode/xcuserdata/<user>.xcuserdatad/xcschemes/xcschememanagement.plist`. So Xcode's own write marked the next build stale, which triggered the destructive re-sync, which broke that build. `xcodebuild` does not write that file, which is why command-line builds were never affected.

This change makes the `libs/` tree idempotent — unchanged entries keep their inode, and entries that are no longer autolinked are pruned instead of wiped — and makes the staleness check skip `.swiftpm`.

### How to verify

In an app that autolinks a library shipping its own `Package.swift`, build in Xcode twice in a row. Both builds should succeed. Before this change the second build fails with `Missing package product`.

## Changelog:
[IOS][FIXED] - Stop SwiftPM autolinking from recreating library package roots on every sync, which broke Xcode builds of apps using libraries that ship their own Package.swift

Pull Request resolved: #58597

Test Plan:
**Unit tests.** `yarn test packages/react-native/scripts/spm` — 21 suites, 1009 tests pass.

New tests, written and seen failing before the fix:

- Two consecutive generation runs over an unchanged self-managed dependency keep both the `libs/` inode and each entry's inode. Failed before the fix with the same inode churn measured on the real app.
- A dependency removed between two runs leaves no symlink under `libs/`.
- The emitted staleness snippet is extracted from the generated script and executed under `/bin/bash -c` with `set -euo pipefail` against a temporary tree. A write under `.swiftpm/` is ignored; a real source change is still detected.

**Real app.** A React Native 0.87.1 app on Xcode 27 autolinking `shopify/react-native-skia` and `react-native-safe-area-context`, both self-managed. Before: `xcodebuild` succeeded, Xcode failed in about 5 seconds with the two errors above, on nearly every build.

**Not run:** the full CI matrix, and no Android-side check — this touches iOS SwiftPM tooling only.

## Scope

Deliberately minimal.

One case still re-syncs: the first time Xcode creates `.swiftpm` inside a library, that bumps the library directory's own mtime, so the build after a fresh checkout reports stale once per library. That is harmless now that the sync is idempotent.

One related item is left alone: the aggregate `Package.swift` is still rewritten on every sync even when its content is unchanged, which bumps its mtime and can make Xcode re-resolve. That is wasteful but not destructive, and it is no longer reached on an ordinary IDE build now that `.swiftpm` writes do not mark the sync stale.

Reviewed By: cipolleschi

Differential Revision: D120740029

Pulled By: shwanton

fbshipit-source-id: 3783d4cee4791ab13517d8f0dcac71f752cb7c1b

(cherry picked from commit 97cc934)

Conflict resolution for 0.87-stable: the new libs/ symlink tests inline their temp-dir and console-spy setup, because the useTempApps() helper from #58290 does not exist on 0.87.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Expo Partner: Expo Partner Pick Request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant