alunny / node-xcode

tools and utilities for working with xcode/ios projects
Apache License 2.0
226 stars 105 forks source link

If a file is added by addToPbxFileReferenceSection, project.pbxproj is then saved by Xcode 6.2, removeFromPbxFileReferenceSection will fail to remove it. #44

Closed initialxy closed 9 years ago

initialxy commented 9 years ago

I have already debugged this issue, so please allow me to explain. When a file is added by addToPbxFileReferenceSection, its record in project.pbxproj will look like this:

E077D027F77B4AACA6666825 /* somefile.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = "somefile.m"; path = "somepath/somefile.m"; sourceTree = "<group>"; fileEncoding = 4; };

However if user opens this Xcode project with Xcode 6.2, then saves it (for whatever reason, does not have to be related to the file in question), its record will now look like this:

E077D027F77B4AACA6666825 /* somefile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = somefile.m; path = somepath/somefile.m; sourceTree = "<group>"; };

The important thing to notice here is that quotes (") are stripped around name and path. This causes removeFromPbxFileReferenceSection to fail. See pbxProject.js line 279, where on the left hand side name and path are unquoted, while on the right hand side name and path are quoted. So if you debug this line you can see that it's comparing: somepath/somefile.m to "somepath/somefile.m", which will never equal.

This issue in turn causes a chain of issues, most important of which is when a file is removed by removeSourceFile, it successfully removes it from PBXBuildFile and PBXSourcesBuildPhase but fails to remove it from PBXFileReference. So the next time when you call addSourceFile on the same file, it will fail to add it to PBXBuildFile and PBXSourcesBuildPhase due to pbxProject.js line 114, where if a file is already found in PBXFileReference, it returns without adding it to PBXBuildFile and PBXSourcesBuildPhase.

This in turn screws up Cordova's plugin upgrade script, which removes bunch of files, update them, then add them again. I understand that Cordova is not this project's responsibility, however I leave this bit of information here just for FYI.

I propose either unquote both side of pbxProject.js line 279, or use || comparison similar to hasFile's implementation. I'm already preparing a pull request with the later strategy, which keeps the code base more consistent. Please consider.

EDIT: Pull request #45 is created for this issue.