Skip to content

Fix SwiftPM: native library added after setup is not compiled until a clean build - #60462

Merged
gabrieldonadel merged 1 commit into
mainfrom
spm-new-lib-needs-clean-build
Dec 2, 2026
Merged

gabrieldonadel merged 1 commit into
mainfrom
spm-new-lib-needs-clean-build

Conversation

@chrfalch

@chrfalch chrfalch commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary:

In a SwiftPM app, a native library you add after setup (for example npm install react-native-svg) is not compiled on the next builds. The build succeeds, but the app shows a red box such as "RNSVGCircle is not registered". Only a clean build fixes it. The Xcode window has the same problem.

The cause:

  1. xcodebuild resolves the package graph first.
  2. Then the "Sync SPM Autolinking" scheme pre-action adds the new library to the Autolinked Package.swift.
  3. Xcode keeps the old package graph for the new manifest. Later builds do not correct it, so the library is never compiled.

Where it came from: #57332 added the pre-action on the assumption that it runs before Xcode resolves packages. The xcodebuild log shows it runs after. Until #58826 fixed its encoding, Xcode read the pre-action script as one line, so it did nothing and only the build phase synced; a new library was then missing for one build only. Since #58826, the pre-action runs, so 0.88.1 and later, and 0.89, are affected.

This change:

  1. The scheme pre-action syncs only when the Autolinked Package.swift is missing. Once the file exists, the "Sync SPM Autolinking" build phase keeps it current.
  2. When the build phase sync changes the Autolinked Package.swift, that one build fails with: native modules changed. Build again.
  3. The next build compiles the library. Builds where the manifest does not change behave as before.

Known limit: if only a library's generated code or source files change, and the Autolinked Package.swift does not, one build can use the old package sources. The next build is correct. This is the same as 0.87 and 0.88.0.

Changelog:

[IOS] [FIXED] - SwiftPM: a native library added after setup is now compiled on the next build, without a clean build

Test Plan:

Unit tests:

yarn jest packages/react-native/scripts/spm --no-cache -i

25 suites, 1264 tests passed, 0 failed. Without the fix, 2 of the 3 new tests fail: the build phase does not fail with "Build again" when its sync changes the manifest, and the pre-action syncs when the manifest exists. The third new test, "the build phase passes when the manifest is unchanged", passes with and without the fix; it guards against a build that fails every time.

App (RN 0.89.0-rc.4, CLI 21.1.0, SwiftPM app, Debug, iOS simulator):

xcodebuild clean build -project ios/CliE2E.xcodeproj -scheme CliE2E -configuration Debug -destination id=<simulator> -derivedDataPath <dir>
npm install react-native-svg@15.16.2
xcodebuild build ...   # build 1
xcodebuild build ...   # build 2
xcodebuild build ...   # build 3
Build after npm install react-native-svg Without the fix With the fix (xcodebuild and the Xcode window)
1 Reports success. Library missing from the app. Stops on purpose: native modules changed. Build again.
2 Reports success. Library missing (red box at runtime). Succeeds. Library in the app (RNSVG compiled, RNSVGCircle in the binary).
3 and later Reports success. Library missing until a clean build. Succeeds. Library in the app. Nothing to rebuild.

Not checked: the app on a simulator after build 2.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 2, 2026
… 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).
@chrfalch
chrfalch force-pushed the spm-new-lib-needs-clean-build branch from ae09b81 to b8c4df3 Compare December 2, 2026 10:37
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 2, 2026
@chrfalch
chrfalch marked this pull request as ready for review December 2, 2026 10:41
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 2, 2026
@cortinico cortinico removed the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 2, 2026
@gabrieldonadel
gabrieldonadel merged commit ee2d411 into main Dec 2, 2026
116 checks passed
gabrieldonadel pushed a commit that referenced this pull request Dec 2, 2026
… clean build (#60462)

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).

(cherry picked from commit ee2d411)
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 Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants