Skip to content

Instantly share code, notes, and snippets.

@estevaolucas
Created July 15, 2019 18:46
Show Gist options
  • Save estevaolucas/9e4c469b923582bfefa07940ea3e57c5 to your computer and use it in GitHub Desktop.
Save estevaolucas/9e4c469b923582bfefa07940ea3e57c5 to your computer and use it in GitHub Desktop.
Fix pbxproj file -> Zeh's research and solution πŸ€―πŸ‘πŸ’ͺ

The current project.pbxproj used by the iOS project is damaged in some way. While XCode can open the file and read its contents correctly, attempting to save it results in a crash. This files that crash, making the file editable again.

Well, pbxproj files are finicky. They are a constant source of pain when fixing merge conflicts, something that happens frequently in our project because of React Native updates.

While I can't say what caused the error with a lot of confidence, here's how it was diagnosed and fixed, for future reference when needed.

Diagnosis

XCode crashes create crash logs that go into ~/Library/Logs/DiagnosticReports/. In there, we could get a crash report that looked like this:

Process:               Xcode [17017]
Path:                  /Applications/Xcode.app/Contents/MacOS/Xcode
Identifier:            Xcode
Version:               10.2.1 (14490.122)
App Item ID:           497799835
App External ID:       830924854
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           Xcode [17017]
User ID:               502

Date/Time:             2019-07-15 13:15:37.270 -0400
OS Version:            Mac OS X 10.14.5 (18F132)
Report Version:        12
Anonymous UUID:        9A95EB49-A761-FC95-AC13-6319270E2525

Sleep/Wake UUID:       BF9D35D4-E1FA-4960-8BD6-62E2A7D9C904

Time Awake Since Boot: 1800000 seconds
Time Since Wake:       12000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
ProductBuildVersion: 10E1001
UNCAUGHT EXCEPTION (NSInvalidArgumentException): -[PBXFileReference realReference]: unrecognized selector sent to instance 0x7fc703e806e0
UserInfo: (null)
Hints: 
 
Backtrace:
  0   __exceptionPreprocess (in CoreFoundation)
  1   DVTFailureHintExceptionPreprocessor (in DVTFoundation)
  (many more lines)

The log or the stack trace are not super helpful, but the error was more or less visible in this line:

UNCAUGHT EXCEPTION (NSInvalidArgumentException): -[PBXFileReference realReference]: unrecognized selector sent to instance 0x7fc703e806e0

It indicates some problem parsing a PBXFileReference; that is, a reference was invalid or missing.

An easier pbxproj validity test can be done with xcproj. This tool manipulates a pbxproj file in the same manner XCode does (likely using the same APIs). Calling the touch command (to open and re-save the file) resulted in the following output:

$ xcproj --project MyProject.xcodeproj touch
2019-07-15 13:33:08.207 xcproj[22033:22952932] -[PBXFileReference realReference]: unrecognized selector sent to instance 0x7f98715e9410
Could not write '<PBXProject:0x7f98715c9270:83CBB9F71A601CBA00E9B192:path='/Users/zehfernando/Repos/myproject/ios/MyProject.xcodeproj'>' to file system.

That is, the same error as XCode itself.

Diagnosis then encompassed trial and error: by removing entire sections of file references from the pbxproj, and then individual sections, we were able to identify the guilty object as this entry inside PBXProject:

{
	ProductGroup = 146834001AC3E56700842450 /* Products */;
	ProjectRef = 146833FF1AC3E56700842450 /* React.xcodeproj */;
},

Fixing

This reference group, 146834001AC3E56700842450, was then compared to the expected group inside the pbxproj file on a new React Native project using the same version as ours (0.59.9).

It turns out our version had two additional references inside the group, linking the JavaScriptCore.framework file inside the group.

image

To remediate that, these files were extracted into a new group of their own ("Frameworks") and re-linked where appropriate. This was also done by duplicating existing entries from a brand new project's pbxproj file.

After those changes, changing projects are working as expected again.

Additional changes were done to the pbxproj file when re-saved from XCode (or touch-ed from xcproj). These including replacing references to 4 files. I'm not sure on the reason for those changes, but the project seems to work well after that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment