Skip to content

Commit bccb1df

Browse files
chrfalchgabrieldonadel
authored andcommitted
Let spm deinit take an app back to CocoaPods
## Summary: An app converted to SwiftPM cannot go back to CocoaPods today. The conversion deletes the React Native lines from the Podfile, one line at a time. A call that spans several lines, such as `use_react_native!(…)`, keeps its other lines, so the Podfile is no longer valid Ruby. This PR keeps the Podfile unchanged. The way back to CocoaPods is then `npx react-native spm deinit`, followed by `pod install`. What changes: 1. **`spm add --deintegrate` keeps the Podfile.** It runs `pod deintegrate` and nothing else on the Podfile. 2. **Plain `npx react-native spm` no longer converts a CocoaPods app.** It stops and points to `spm add --deintegrate`. 3. **`pod install` stops on a SwiftPM app.** The Podfile is still there, so a user can run `pod install` by mistake. That would add CocoaPods back and break the generated `Package.swift`. The React Native CocoaPods scripts now stop with an error, before they write any file, when the `.xcodeproj` has the SwiftPM marker file (`.spm-injected.json`). 4. **`spm deinit` cleans up after itself.** It removes the `.gitignore` lines that the conversion added, and the empty `ReactNativeSPM` folders. Then it says to run `pod install`. Smaller fixes from the review: - The uncommitted-changes prompt now comes before `pod deintegrate`, so "no" changes nothing. - `.gitignore` is written only after a successful conversion. - Pods that come from `node_modules/react-native` no longer show in the "pods that are not part of React Native" warning. The three SwiftPM iOS CI workflows checked the Podfile content after the conversion. They now check that the `.xcodeproj` has no CocoaPods references. Where to look: - `scripts/setup-apple-spm.js`: the conversion and `deinit` (items 1, 2, 4). - `scripts/cocoapods/utils.rb` (`error_if_spm_injected`), called from `react_native_pods.rb` and `cocoapods/autolinking.rb`: the `pod install` check (item 3). ## Changelog: [IOS] [CHANGED] - `react-native spm add --deintegrate` keeps the Podfile, and `pod install` stops with an error on an app that uses SwiftPM. `react-native spm deinit` and then `pod install` take an app back to CocoaPods. ## Test Plan: Unit tests: ``` yarn jest packages/react-native/scripts/spm/__tests__ ``` 25 suites, 1236 tests pass. A new app on React Native 0.89.0-nightly-20261021-c7429b0e9 and CLI 21.0.0-alpha.1, with the changed scripts copied in: 1. `npx react-native spm add --deintegrate --yes`: the app converts, the Podfile is unchanged, and the SwiftPM build succeeds. 2. `pod install`: it stops with "This app uses Swift Package Manager for React Native … so `pod install` would break the Xcode project." No file changes. 3. `npx react-native run-ios` and `build-ios`: both build the app. The CLI sees the marker file and does not run `pod install`. 4. `npx react-native spm deinit`, then `pod install`: the CocoaPods build succeeds. Not checked: Flow locally (my Flow binary is too old), `run-ios --force-pods`, and CLI 20. Differential Revision: D129064536 Pull Request resolved: #59368 (cherry picked from commit b255245)
1 parent 0ca7b7b commit bccb1df

13 files changed

Lines changed: 1297 additions & 307 deletions

‎.github/workflows/test-ios-spm-helloworld.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ jobs:
9494
echo "::error::spm add did not inject SwiftPM: $XCODE_PROJECT/.spm-injected.json is missing"
9595
exit 1
9696
fi
97-
if [[ -f Podfile ]] && grep -q 'use_react_native!' Podfile; then
98-
echo "::error::spm add --deintegrate left use_react_native! in the Podfile"
97+
if grep -q 'Pods-' "$XCODE_PROJECT/project.pbxproj"; then
98+
echo "::error::spm add --deintegrate left CocoaPods references in $XCODE_PROJECT"
9999
exit 1
100100
fi
101-
echo "SwiftPM injected in place; Podfile de-integrated."
101+
echo "SwiftPM injected in place; CocoaPods de-integrated."
102102
- name: Build ${{ env.XCODE_SCHEME }} (${{ matrix.flavor }})
103103
shell: bash
104104
working-directory: ${{ env.APP_IOS_DIR }}

‎.github/workflows/test-ios-spm-newapp.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ jobs:
105105
echo "::error::spm add did not inject SwiftPM: $XCODE_PROJECT/.spm-injected.json is missing"
106106
exit 1
107107
fi
108-
if [[ -f Podfile ]] && grep -q 'use_react_native!' Podfile; then
109-
echo "::error::spm add --deintegrate left use_react_native! in the Podfile"
108+
if grep -q 'Pods-' "$XCODE_PROJECT/project.pbxproj"; then
109+
echo "::error::spm add --deintegrate left CocoaPods references in $XCODE_PROJECT"
110110
exit 1
111111
fi
112-
echo "SwiftPM injected in place; Podfile de-integrated."
112+
echo "SwiftPM injected in place; CocoaPods de-integrated."
113113
- name: Build ${{ env.XCODE_SCHEME }} (${{ matrix.flavor }})
114114
shell: bash
115115
working-directory: ${{ env.APP_IOS_DIR }}

‎.github/workflows/test-ios-spm-rntester.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ jobs:
9494
echo "::error::spm add did not inject SwiftPM: $XCODE_PROJECT/.spm-injected.json is missing"
9595
exit 1
9696
fi
97-
if [[ -f Podfile ]] && grep -q 'use_react_native!' Podfile; then
98-
echo "::error::spm add --deintegrate left use_react_native! in the Podfile"
97+
if grep -q 'Pods-' "$XCODE_PROJECT/project.pbxproj"; then
98+
echo "::error::spm add --deintegrate left CocoaPods references in $XCODE_PROJECT"
9999
exit 1
100100
fi
101-
echo "SwiftPM injected in place; Podfile de-integrated."
101+
echo "SwiftPM injected in place; CocoaPods de-integrated."
102102
- name: Build ${{ env.XCODE_SCHEME }} (${{ matrix.flavor }})
103103
shell: bash
104104
working-directory: ${{ env.APP_IOS_DIR }}

‎packages/react-native/scripts/cocoapods/autolinking.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,6 @@ def link_native_modules!(config)
208208
# - config_command: the command to run to get the application's current config, e.g. ['npx', '@react-native-community/cli', 'config'],
209209
# you can override this if you'd like to avoid the dependency. e.g. ['cat', 'your_config.json']
210210
def use_native_modules!(config_command = $default_command)
211+
ReactNativePodsUtils.error_if_spm_injected
211212
return link_native_modules!(list_native_modules!(config_command))
212213
end

‎packages/react-native/scripts/cocoapods/utils.rb‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,24 @@ def self.create_xcode_env_if_missing(file_manager: File)
316316
end
317317
end
318318

319+
# Stops `pod install` on an app that `npx react-native spm add` converted to
320+
# Swift Package Manager. Codegen would rewrite the app's generated
321+
# Package.swift, and Xcode resolves packages before any build phase can
322+
# report it, so this has to run before React Native writes any file.
323+
# prepare_react_native_project!, use_native_modules! and use_react_native!
324+
# all call it, because a Podfile need not call the first.
325+
def self.error_if_spm_injected
326+
podfile_dir = Pod::Config.instance.installation_root.to_s
327+
xcodeproj = Dir.glob('*.xcodeproj', base: podfile_dir).sort.find do |name|
328+
File.exist?(File.join(podfile_dir, name, '.spm-injected.json'))
329+
end
330+
return if xcodeproj.nil?
331+
332+
raise Pod::Informative, "This app uses Swift Package Manager for React Native " +
333+
"(#{xcodeproj} has .spm-injected.json), so `pod install` would break the Xcode project. " +
334+
"To go back to CocoaPods, run `npx react-native spm deinit` first, then `pod install`."
335+
end
336+
319337
# It examines the target_definition property and sets the appropriate value for
320338
# ENV['USE_FRAMEWORKS'] variable.
321339
#

‎packages/react-native/scripts/react_native_pods.rb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def min_supported_versions
4747
# This function prepares the project for React Native, before processing
4848
# all the target exposed by the framework.
4949
def prepare_react_native_project!
50+
ReactNativePodsUtils.error_if_spm_injected
51+
5052
# Temporary solution to suppress duplicated GUID error & master specs repo warning.
5153
# Can be removed once we move to generate files outside pod install.
5254
install! 'cocoapods', :deterministic_uuids => false, :warn_for_unused_master_specs_repo => false
@@ -96,6 +98,7 @@ def use_react_native! (
9698
config_file_dir: '',
9799
privacy_file_aggregation_enabled: true
98100
)
101+
ReactNativePodsUtils.error_if_spm_injected
99102
error_if_try_to_use_jsc_from_core()
100103
warn_if_new_arch_disabled()
101104

0 commit comments

Comments
 (0)