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

StackOverflow https://stackoverflow.com/questions/11530977

  •  21-06-2021
  •  | 
  •  

Domanda

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 /* movie@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* movie@2x.png */; };
14EE4CD415A5E69000DCA763 /* movie@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* movie@2x.png */; };
14EE4CD515A5E69000DCA763 /* movie@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* movie@2x.png */; };
14EE4CD615A5E69000DCA763 /* movie@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* movie@2x.png */; };
14EE4CD715A5E69000DCA763 /* movie@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 14EE4CB615A5E68500DCA763 /* movie@2x.png */; };

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

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.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top