From cadd41b1a2e16b1c77a8d3022f4ccbdbd5ea295f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Thu, 12 Sep 2024 09:31:25 -0700 Subject: [PATCH] fix(iOS): don't reference PrivacyInfo.xcprivacy twice for new projects (#46457) Summary: This PR fixes an issue with PrivacyInfo files. When generating a new project for using the latest RC 0.76.0.rc0 I got two privacy manifests references in Xcode. This is because `PrivacyManifestUtils` look for build phase reference: ```ruby reference_exists = target.resources_build_phase.files_references.any? { |file_ref| file_ref&.path&.end_with? "xcprivacy" } ``` Which doesn't exist for the generated template. Here is how Xcode file tree looks like after installing pods: ![CleanShot 2024-09-12 at 13 23 21@2x](https://github.com/user-attachments/assets/44e5bb55-a1ab-4b4b-bfe4-e4a6808afd15) ## Changelog: [IOS] [FIXED] - don't reference PrivacyInfo.xcprivacy twice for new projects Pull Request resolved: https://github.com/facebook/react-native/pull/46457 Test Plan: 1. Generate a new project 2. Execute pod install 3. Check if only one PrivacyInfo file exists Reviewed By: cortinico Differential Revision: D62580116 Pulled By: cipolleschi fbshipit-source-id: 1224a41307ae6c9b862832f145baf0edc92476d6 --- .../cocoapods/privacy_manifest_utils.rb | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb b/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb index 922a87abddf..c6004c5728f 100644 --- a/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb +++ b/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb @@ -67,14 +67,29 @@ 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" } - 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 - file_ref = file_root.new_file(file_path) - build_file = target.resources_build_phase.add_file_reference(file_ref, true) + privacy_info_filename = File.basename(file_path) + + # Check if the file is already in the PBXBuildFile section + build_phase_reference_exists = target.resources_build_phase.files_references.any? { |file_ref| file_ref&.path&.end_with?(privacy_info_filename) } + + unless build_phase_reference_exists + # Check if the file is already in the PBXFileReference section + existing_file_reference = user_project.files.find { |file| file.path&.end_with?(privacy_info_filename) } + + if existing_file_reference + # If the file reference exists, add it to the build phase + target.resources_build_phase.add_file_reference(existing_file_reference, true) + else + # If the file reference doesn't exist, add it to the project and the build phase + # 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 + puts "file_root: #{file_root}" + + file_ref = file_root.new_file(file_path) + target.resources_build_phase.add_file_reference(file_ref, true) + end end end