Skip to content

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

@Ant-tree

Description

@Ant-tree

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions