Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Prevent cached groups from being reparented
Co-authored-by: Copilot <[email protected]>
  • Loading branch information
eschwieb and Copilot committed Aug 22, 2026
commit 195ac1865c8326cb14f0ca328b331bfad759b4fa
11 changes: 10 additions & 1 deletion Sources/XcodeGenKit/SourceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ class SourceGenerator {
if let cachedGroup = groupsByPath[path] {
var cachedGroupChildren = cachedGroup.children
for child in children {
guard !(child is PBXGroup)
|| (!rootGroups.contains(child)
&& (child.parent == nil || child.parent === cachedGroup)) else {
continue
}

// only add the children that aren't already in the cachedGroup
// Variant groups with the same name may select different localizations for different
// targets, so only the same variant group object is a duplicate.
Expand Down Expand Up @@ -403,9 +409,12 @@ class SourceGenerator {
let groupName = name ?? path.lastComponent

let groupPath = resolveGroupPath(path, isTopLevelGroup: hasCustomParent || isTopLevelGroup)
let unattachedChildren = children.filter {
!($0 is PBXGroup) || (!rootGroups.contains($0) && $0.parent == nil)
}

let group = PBXGroup(
children: children,
children: unattachedChildren,
sourceTree: .group,
name: groupName != groupPath ? groupName : nil,
path: groupPath
Expand Down
77 changes: 77 additions & 0 deletions Tests/XcodeGenKitTests/SourceGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,83 @@ class SourceGeneratorTests: XCTestCase {
try pbxProj.expectFile(paths: ["Sources", "A", "C2.0", "c.swift"], buildPhase: .sources)
}

$0.it("does not reparent a top-level group discovered by a broader source") {
let directories = """
Sources:
Resources:
- Localizable.strings
"""
try createDirectories(directories)

let resourcesTarget = Target(
name: "Resources",
type: .bundle,
platform: .iOS,
sources: ["Sources/Resources"]
)
let broadTarget = Target(
name: "Broad",
type: .application,
platform: .iOS,
sources: ["Sources"]
)
let project = Project(
basePath: directoryPath,
name: "Test",
targets: [resourcesTarget, broadTarget]
)

let pbxProj = try project.generatePbxProj()
let mainGroup = try pbxProj.getMainGroup()
let resourcesGroups = pbxProj.groups.filter { $0.nameOrPath == "Resources" }
let resourcesGroup = try unwrap(resourcesGroups.first)

try expect(resourcesGroups.count) == 1
try expect(resourcesGroup.path) == "Sources/Resources"
try expect(mainGroup.children.contains { $0 === resourcesGroup }) == true
try expect(
pbxProj.groups
.first { $0.nameOrPath == "Sources" }?
.children
.contains { $0 === resourcesGroup }
) == false
}

$0.it("preserves an explicit custom group for a top-level folder reference") {
let directories = """
Folder:
- resource.txt
"""
try createDirectories(directories)

let target = Target(
name: "Test",
type: .application,
platform: .iOS,
sources: [
TargetSource(
path: "Folder",
group: "CustomGroup",
type: .folder,
buildPhase: BuildPhaseSpec.none
),
]
)
let project = Project(basePath: directoryPath, name: "Test", targets: [target])

let pbxProj = try project.generatePbxProj()
let mainGroup = try pbxProj.getMainGroup()
let customGroup = try unwrap(
pbxProj.groups.first { $0.nameOrPath == "CustomGroup" }
)
let folderReference = try unwrap(
pbxProj.fileReferences.first { $0.nameOrPath == "Folder" }
)

try expect(mainGroup.children.contains { $0 === folderReference }) == true
try expect(customGroup.children.contains { $0 === folderReference }) == true
}

$0.it("generates synced folder") {
let directories = """
Sources:
Expand Down