Skip to content

Commit 8118ff5

Browse files
Ckitakishiclaude
andcommitted
Keep file elements in a single parent group
`getGroup` infers "top level" from the shape of the path, which stops holding once the same directory is also reached as a child. Drop anything that gets attached to a group from `rootGroups`, and rewrite its path, project relative until now, to be relative to the new parent. The TestProject fixture loses the duplicate `Folder` entry in the main group. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 56d5a67 commit 8118ff5

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎Sources/XcodeGenKit/SourceGenerator.swift‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ class SourceGenerator {
335335
private func getGroup(path: Path, name: String? = nil, mergingChildren children: [PBXFileElement], createIntermediateGroups: Bool, hasCustomParent: Bool, isBaseGroup: Bool) -> PBXGroup {
336336
let groupReference: PBXGroup
337337

338+
// A child may already have been registered as top level before being attached to its parent.
339+
removeRootGroupMembership(from: children)
340+
338341
if let cachedGroup = groupsByPath[path] {
339342
var cachedGroupChildren = cachedGroup.children
340343
for child in children {
@@ -382,6 +385,19 @@ class SourceGenerator {
382385
return groupReference
383386
}
384387

388+
private func removeRootGroupMembership(from elements: [PBXFileElement]) {
389+
for element in elements where rootGroups.contains(element) {
390+
rootGroups.remove(element)
391+
// Top-level group paths are relative to the project; nested group paths are relative to their parent.
392+
guard element is PBXGroup, element.sourceTree == .group, let elementPath = element.path else { continue }
393+
let relativePath = Path(elementPath).lastComponent
394+
element.path = relativePath
395+
if element.name == relativePath {
396+
element.name = nil
397+
}
398+
}
399+
}
400+
385401
/// Creates a variant group or returns an existing one at the path
386402
private func getVariantGroup(path: Path, inPath: Path) -> PBXVariantGroup {
387403
let variantGroup: PBXVariantGroup

‎Tests/Fixtures/TestProject/Project.xcodeproj/project.pbxproj‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,6 @@
10901090
4C7F5EB7D6F3E0E9B426AB4A /* Utilities */,
10911091
3FEA12CF227D41EF50E5C2DB /* Vendor */,
10921092
80C3A0E524EC1ABCB9149EA2 /* XPC Service */,
1093-
DAA7880242A9DE61E68026CC /* Folder */,
10941093
2E1E747C7BC434ADB80CC269 /* Headers */,
10951094
6B1603BA83AA0C7B94E45168 /* ResourceFolder */,
10961095
6BBE762F36D94AB6FFBFE834 /* SomeFile */,

0 commit comments

Comments
 (0)