Skip to content

Commit

Permalink
fix(iOS): don't reference PrivacyInfo.xcprivacy twice for new projects (
Browse files Browse the repository at this point in the history
#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: #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
  • Loading branch information
okwasniewski authored and facebook-github-bot committed Sep 12, 2024
1 parent 120558c commit cadd41b
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/react-native/scripts/cocoapods/privacy_manifest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,29 @@ def self.read_privacyinfo_file(file_path)
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

Expand Down

0 comments on commit cadd41b

Please sign in to comment.