Can duplicate file references in XCode's project.pbxproj files be safely removed?
Asked Answered
M

4

9

My company's iPhone/ipad product has migrated through several generations of iOS and Xcode (now at 5.1 and 4.3), and has many targets. Perhaps because of these factors, there are many identical lines for each source file in the PBXBuildFile section, e.g:

14EE4CD315A5E69000DCA763 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* [email protected] */; };
14EE4CD415A5E69000DCA763 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* [email protected] */; };
14EE4CD515A5E69000DCA763 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* [email protected] */; };
14EE4CD615A5E69000DCA763 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* [email protected] */; };
14EE4CD715A5E69000DCA763 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* [email protected] */; };

Note that the lines for a given source file are all absolutely (I was wrong, they are not quite...) identical, and the number of duplicate lines varies from none to a half dozen or more. Because of this duplication, the project.pbxproj's PBXBuildFile section is almost 5000 lines long, though we really only have about 1200 unique files.

Before I cobble up a script to strip all these duplicate lines, and ask test for full regression on all builds and targets, I'd like to be sure that Xcode doesn't need these duplicate lines for some arcane reason.

I'm fairly certain that this wasn't caused by flubbed version control; duplicate lines only appear in the PBXBuildFile section and while I'm not fond of P4, we have little trouble merging project.pbxproj file changes, though at nearly 20,000 lines, it's a bit unwieldy. I think some release of Xcode did this, perhaps while adding new targets, but I've not found anyone else complaining about the issue.

Secondary questions: How did this happen? Has anyone else found these kind of duplicate lines?

Mcdermott answered 17/7, 2012 at 21:13 Comment(0)
P
6

Posting for posterity, in case others run into this too.

This happens all the time when developers are working on separate branches and e.g. move files around to different folders, add new groups in the same subfolder as another group that another developer makes a change to on a separate branch, etc., and then need to merge their project changes back to a common base branch. Merging Xcode project files is its own special kind of hell, and there's no good way to determine which of these references is the one you want to keep; as @Maistor Kokir points out, the id is going to be unique for each reference, and it's critical that you pick the correct one since groups refer to files by ID, and more importantly, build rules and targets also refer to the id.

The only way I've found to reliably fix this is to remove all references to the file in question (in the build target's "Build Phases" | "Compile Sources"), then add it back in again.

And, to truly fix it at the source, I've recommended that developers make any project changes on the common branch (e.g. a git "master" branch), then merge that change to their development branch. Project additions usually don't require the same treatment; only changes "within reason", which would include (but would not be limited to) moving a file to a different group, or even to a different location within the same group, renaming or removing a group, removing or modifying submodule references, etc. Experience will be your true guide.

Pulcheria answered 20/5, 2016 at 22:51 Comment(0)
S
7

As you see the file ref is duplicated , but the id of file inside each targets has one little difference 14EE4CD*3*15A5E69000DCA763 , the 8th digit rather varies.

Ster answered 16/11, 2012 at 11:47 Comment(2)
Rats, I can't believe I missed that in the sea of almost-the-same IDs. I'd given up on ever getting an answer on this one, I'll have to go back and see how the IDs map to project variants.Mcdermott
Yes, it seems like if you have multiple build targets, this section will uniquely reference them once per target. Kind of makes sense.Agricola
P
6

Posting for posterity, in case others run into this too.

This happens all the time when developers are working on separate branches and e.g. move files around to different folders, add new groups in the same subfolder as another group that another developer makes a change to on a separate branch, etc., and then need to merge their project changes back to a common base branch. Merging Xcode project files is its own special kind of hell, and there's no good way to determine which of these references is the one you want to keep; as @Maistor Kokir points out, the id is going to be unique for each reference, and it's critical that you pick the correct one since groups refer to files by ID, and more importantly, build rules and targets also refer to the id.

The only way I've found to reliably fix this is to remove all references to the file in question (in the build target's "Build Phases" | "Compile Sources"), then add it back in again.

And, to truly fix it at the source, I've recommended that developers make any project changes on the common branch (e.g. a git "master" branch), then merge that change to their development branch. Project additions usually don't require the same treatment; only changes "within reason", which would include (but would not be limited to) moving a file to a different group, or even to a different location within the same group, renaming or removing a group, removing or modifying submodule references, etc. Experience will be your true guide.

Pulcheria answered 20/5, 2016 at 22:51 Comment(0)
E
2

Usually Xcode generates unique reference(ID's) for files referenced in multiple targets. I suppose you are having more than one build targets and LoginViewController referenced in both which ends up with different fileRef ID's. Although it looks identical but removing them assuming that it might be harmless may cause crashes.

You can read more about pbxproj file from http://www.monobjc.net/xcode-project-file-format.html.

Erskine answered 10/3, 2016 at 6:17 Comment(0)
M
1

I did finally write and run the script stripping the many duplicate file references. Doing so seemed entirely harmless, my local builds all continued to work correctly. I never checked in those fixes, since I changed employers about then, and it seemed rude to toss a bomb that had potential to break builds for a dozen teams just before leaving.

Mcdermott answered 22/10, 2013 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.