From 0d093c23de475ed76a8b66673559272db83ae256 Mon Sep 17 00:00:00 2001 From: aleqsio Date: Thu, 2 May 2024 11:40:07 -0700 Subject: [PATCH] Hotfix privacy manifest aggregation script (#44390) Summary: As pointed out by liamjones here: https://github.com/facebook/react-native/pull/44214#discussion_r1587755403 The original PR did introduce a bug in the `find/first` check, but in my testing, we do need to look at `group.name`, so let's make sure we check both. This also makes it play nice with an existing file even if it is added to a different directory, by appending to it instead of forcing it to exist in the main group. ## Changelog: [IOS] [FIXED] - Fix privacy aggregation Pull Request resolved: https://github.com/facebook/react-native/pull/44390 Test Plan: Tested on rn-tester Reviewed By: cipolleschi Differential Revision: D56893594 Pulled By: philIip fbshipit-source-id: b92589bc2bed9d07e9af20c56a8b9f6c61d864f0 --- .../scripts/cocoapods/privacy_manifest_utils.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb b/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb index 322da24f0e9a87..2f4cac1f8bab2b 100644 --- a/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb +++ b/packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb @@ -7,7 +7,7 @@ module PrivacyManifestUtils def self.add_aggregated_privacy_manifest(installer) user_project = get_user_project_from(installer) targets = get_application_targets(user_project) - file_path = get_privacyinfo_file_path(user_project) + file_path = get_privacyinfo_file_path(user_project, targets) privacy_info = read_privacyinfo_file(file_path) || { "NSPrivacyCollectedDataTypes" => [], @@ -70,13 +70,18 @@ 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.first { |group| group.name == target.name } || user_project + 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) end end - def self.get_privacyinfo_file_path(user_project) + 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" } + if existing_file + return existing_file.real_path + end # We try to find a file we know exists in the project to get the path to the main group directory info_plist_path = user_project.files.find { |file_ref| file_ref.name == "Info.plist" } if info_plist_path.nil?