Skip to content

Commit fe59c88

Browse files
yonaskolbclaude
andcommitted
Fix makePathRelative using wrong base path when projectDirectory differs (#1606)
`makePathRelative` used `project.basePath` to compute parent paths, while `resolveGroupPath` used `projectDirectory ?? project.basePath`. When these differ (e.g. via --project flag), intermediate group paths become inconsistent, causing Xcode to resolve files to the wrong location. Regression from #1596 which added the `makePathRelative` call in the `createIntermediateGroups` code path. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 90dfa9d commit fe59c88

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

Sources/XcodeGenKit/SourceGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ class SourceGenerator {
886886
element = parent
887887
}
888888

889-
let completePath = project.basePath + Path(paths.joined(separator: "/"))
889+
let completePath = (projectDirectory ?? project.basePath) + Path(paths.joined(separator: "/"))
890890
let relativePath = try path.relativePath(from: completePath)
891891
let relativePathString = relativePath.string
892892

Tests/XcodeGenKitTests/SourceGeneratorTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,40 @@ class SourceGeneratorTests: XCTestCase {
911911
try pbxProj.expectFile(paths: ["Sources/B", "b.swift"], names: ["B", "b.swift"], buildPhase: .sources)
912912
}
913913

914+
$0.it("generates intermediate groups with different projectDirectory") {
915+
916+
let directories = """
917+
Sources:
918+
- a.swift
919+
- b.swift
920+
Modules:
921+
- m.swift
922+
"""
923+
try createDirectories(directories)
924+
925+
let target = Target(name: "Test", type: .application, platform: .iOS, sources: [
926+
"../Sources",
927+
"../Modules",
928+
])
929+
let options = SpecOptions(createIntermediateGroups: true)
930+
// basePath is a subdirectory (simulating spec in a subdir like ProjectRoot/XcodeGen/)
931+
// projectDirectory is the parent (simulating --project pointing to ProjectRoot/)
932+
let subdir = directoryPath + "SubDir"
933+
try subdir.mkpath()
934+
let project = Project(basePath: subdir, name: "Test", targets: [target], options: options)
935+
936+
let generator = PBXProjGenerator(project: project, projectDirectory: directoryPath)
937+
let pbxProj = try generator.generate()
938+
939+
// Sources and Modules should have path = "Sources"/"Modules" (not "../Sources")
940+
// The intermediate group for TestDirectory should have path = "."
941+
// So Xcode resolves: projectDir/./Sources = correct
942+
// Before fix: path was "../Sources", resolving to projectDir/./../Sources = wrong
943+
try pbxProj.expectFile(paths: [".", "Sources", "a.swift"], names: ["TestDirectory", "Sources", "a.swift"], buildPhase: .sources)
944+
try pbxProj.expectFile(paths: [".", "Sources", "b.swift"], names: ["TestDirectory", "Sources", "b.swift"], buildPhase: .sources)
945+
try pbxProj.expectFile(paths: [".", "Modules", "m.swift"], names: ["TestDirectory", "Modules", "m.swift"], buildPhase: .sources)
946+
}
947+
914948
$0.it("generates custom groups") {
915949

916950
let directories = """

0 commit comments

Comments
 (0)