Skip to content

Commit 7dd67b7

Browse files
yonaskolbclaude
andcommitted
Extract basePath computed property in SourceGenerator
Consolidates `projectDirectory ?? project.basePath` into a single `basePath` property to prevent future mismatches between path resolution call sites. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent fe59c88 commit 7dd67b7

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/XcodeGenKit/SourceGenerator.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ class SourceGenerator {
3232

3333
private(set) var knownRegions: Set<String> = []
3434

35+
/// The effective base path for resolving group and file paths in the generated project.
36+
/// Uses `projectDirectory` when the xcodeproj is generated in a different location than the spec.
37+
private var basePath: Path {
38+
projectDirectory ?? project.basePath
39+
}
40+
3541
init(project: Project, pbxProj: PBXProj, projectDirectory: Path?) {
3642
self.project = project
3743
self.pbxProj = pbxProj
3844
self.projectDirectory = projectDirectory
3945
}
4046

4147
private func resolveGroupPath(_ path: Path, isTopLevelGroup: Bool) -> String {
42-
if isTopLevelGroup, let relativePath = try? path.relativePath(from: projectDirectory ?? project.basePath).string {
48+
if isTopLevelGroup, let relativePath = try? path.relativePath(from: basePath).string {
4349
return relativePath
4450
} else {
4551
return path.lastComponent
@@ -62,7 +68,7 @@ class SourceGenerator {
6268
let absolutePath = project.basePath + path.normalize()
6369

6470
// Get the local package's relative path from the project root
65-
let fileReferencePath = try? absolutePath.relativePath(from: projectDirectory ?? project.basePath).string
71+
let fileReferencePath = try? absolutePath.relativePath(from: basePath).string
6672

6773
let fileReference = addObject(
6874
PBXFileReference(
@@ -886,7 +892,7 @@ class SourceGenerator {
886892
element = parent
887893
}
888894

889-
let completePath = (projectDirectory ?? project.basePath) + Path(paths.joined(separator: "/"))
895+
let completePath = (basePath) + Path(paths.joined(separator: "/"))
890896
let relativePath = try path.relativePath(from: completePath)
891897
let relativePathString = relativePath.string
892898

0 commit comments

Comments
 (0)