From 86cb45e57786b0a62d9c523f9d5db4f8e5dc52df Mon Sep 17 00:00:00 2001 From: Rui Ying Date: Fri, 10 May 2024 02:51:11 -0700 Subject: [PATCH] Handle the case where the file reference doesn't have a path (#44410) Summary: The new cocoapod post install script includes aggregation and generation of privacy manifests for iOS, which is great. However, the script doesn't consider the case where the file reference doesn't have a path. Example, for a project setup like the screenshot: image The code https://github.com/facebook/react-native/blob/05a4232dd591e2d43f192d69ca14a04f4a3fb6a1/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb#L80-L81 prints `file_refs`: ``` [ , , , , ] ``` where a `PBXVariantGroup` exists and it doesn't have `path`. The error `undefined method 'end_with?' for nil` occurs as a result. ## Changelog: [IOS] [FIXED] - In privacy manifest post install script, handle the case where the file reference doesn't have a path Pull Request resolved: https://github.com/facebook/react-native/pull/44410 Test Plan: 1. Add a new "Strings File (Legacy)" in Xcode to the project. 2. Run `pod install` for iOS. 3. See the error `undefined method 'end_with?' for nil`. 4. Apply the fix and rerun `pod install`. 5. The script runs successfully. Reviewed By: javache Differential Revision: D57056159 Pulled By: cipolleschi fbshipit-source-id: 42caaf1a98efb9111f6ff1014a5c8b7703b042f2 --- .../react-native/scripts/cocoapods/privacy_manifest_utils.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb b/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb index 35229c481f1..691aed5bd2a 100644 --- a/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb +++ b/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb @@ -67,7 +67,7 @@ module PrivacyManifestUtils end def self.ensure_reference(file_path, user_project, target) - reference_exists = target.resources_build_phase.files_references.any? { |file_ref| file_ref.path.end_with? "PrivacyInfo.xcprivacy" } + reference_exists = target.resources_build_phase.files_references.any? { |file_ref| file_ref.path&.end_with? "PrivacyInfo.xcprivacy" } unless reference_exists # We try to find the main group, but if it doesn't exist, we default to adding the file to the project root – both work file_root = user_project.root_object.main_group.children.find { |group| group.class == Xcodeproj::Project::Object::PBXGroup && (group.name == target.name || group.path == target.name) } || user_project @@ -78,7 +78,7 @@ module PrivacyManifestUtils def self.get_privacyinfo_file_path(user_project, targets) file_refs = targets.flat_map { |target| target.resources_build_phase.files_references } - existing_file = file_refs.find { |file_ref| file_ref.path.end_with? "PrivacyInfo.xcprivacy" } + existing_file = file_refs.find { |file_ref| file_ref.path&.end_with? "PrivacyInfo.xcprivacy" } if existing_file return existing_file.real_path end