Skip to content

Conversation

@Ant-tree
Copy link

@Ant-tree Ant-tree commented Feb 11, 2026

Fix crash when sorting project references with missing PBXFileElement.name

Resolves tuist/XcodeProj#1055

PBXProjEncoder.sortProjectReferences force-unwraps PBXFileElement.name when sorting PBXProject.projectReferences. However, name can legitimately be nil in pbxproj files generated by Xcode (where the display label is derived from path). This caused a runtime crash during encoding.

Short description

When PBXOutputSettings.projReferenceFormat == .xcode, PBXProjEncoder sorts PBXProject.projectReferences alphabetically by the referenced project name. If the referenced PBXFileElement has name == nil, encoding crashes with:

XcodeProj/PBXProjEncoder.swift:491: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Solution

Use a stable "display name" for sorting that matches Xcode’s behavior:

  • Prefer name when present
  • Fallback to path when name is nil (and finally "" as a last resort)

This prevents crashes while keeping sorting deterministic.

Implementation ‍‍

  • Updated PBXProjEncoder.sortProjectReferences to compute a display key using name ?? path ?? ""
  • Preserved case-insensitive ordering to match the existing intent

Testing

  • Reproduced the crash before the change on a project containing project references where the PBXFileReference omits name
  • Verified encoding succeeds after the change and output ordering is stable
  • All tests pass locally

Refactor project reference sorting to handle nil values and use display names.
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 11, 2026
@dosubot
Copy link

dosubot bot commented Feb 11, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@Ant-tree
Copy link
Author

@dosubot could you review this PR? It fixes a crash in PBXProjEncoder when PBXFileElement.name is nil by falling back to path.

@dosubot
Copy link

dosubot bot commented Feb 11, 2026

The fix is correct and aligns with best practices: replacing the force-unwrapping of PBXFileElement.name with a fallback to name ?? path ?? "" prevents crashes and maintains stable, case-insensitive sorting of project references. This approach is consistent with Xcode's behavior and other parts of the codebase, and no side effects are expected as long as the fallback is used consistently see supporting details.

To ensure robustness, it's important to add or update tests that cover encoding projects where PBXFileReference.name is nil and only path is set, so the fallback logic is exercised and future regressions are prevented see implementation context.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@Ant-tree Ant-tree changed the title Improve sorting of project references Fix crash when sorting project references with missing PBXFileElement.name Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PBXProjEncoder crashes when sorting projectReferences if ProjectRef file element has nil name

1 participant