PBXProjEncoder crashes when sorting projectReferences if ProjectRef file element has nil name
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 2.2k
- Forks
- 355
- Avg merge
- 5h 48m
- Merged PRs (30d)
- 13
Description
Summary
PBXProjEncoder crashes with Fatal error: Unexpectedly found nil while unwrapping an Optional value when encoding a project that containsPBXProject.projectReferences entries whose ProjectRef resolves to a PBXFileElement with name == nil.
This is common/valid in Xcode projects where PBXFileReference relies on path instead of name.
Steps to Reproduce
Create or open an .xcodeproj that includes a project reference (e.g., via Add Files to “…” → select another .xcodeproj).
Ensure the referenced PBXFileReference does NOT have a name field (only path), which is a typical pbxproj output.
Encode/write the project using XcodeProj with PBXOutputSettings.projReferenceFormat == .xcode (or any codepath that triggers sortProjectReferences).
Expected Behavior
Encoding should succeed. Sorting project references should use a stable display key even when name is nil (e.g., fallback to path).
Actual Behavior
Crash due to forced unwrap in PBXProjEncoder.sortProjectReferences:
let lName = lFile.name!
Stack Trace / Error
XcodeProj/PBXProjEncoder.swift:491: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Root Cause
PBXFileElement.name is optional and may be nil for project references. The encoder assumes it is always non-nil and force-unwraps it during sorting.
Proposed Fix
Use a safe fallback when name is nil. For example:
displayName = name ?? path ?? ""
And compare displayName case-insensitively.
I have a PR ready that implements this fallback and prevents the crash.
Additional Notes
This occurs with valid pbxproj files produced by Xcode, since PBXFileReference often omits name and uses path as the display label.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in PBXProjEncoder.swift at the sortProjectReferences code around line 491, where PBXFileElement.name is force-unwrapped. Reproduce encoding with a project reference whose file element has only a path, then verify encoding succeeds and sorting uses the available display information without crashing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100