Skip to content

Commit 3a7e75f

Browse files
authored
Fix issue with includes not working when no matches are found (#1337)
* Fix issue where an includes pattern without matches prevented includes from working * Add new test to handle includes with no matches
1 parent b448a67 commit 3a7e75f

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

Sources/XcodeGenKit/SourceGenerator.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ class SourceGenerator {
384384
}
385385

386386
/// Checks whether the path is not in any default or TargetSource excludes
387-
func isIncludedPath(_ path: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>) -> Bool {
387+
func isIncludedPath(_ path: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>?) -> Bool {
388388
return !defaultExcludedFiles.contains(where: { path.lastComponent == $0 })
389389
&& !(path.extension.map(defaultExcludedExtensions.contains) ?? false)
390390
&& !excludePaths.contains(path)
391391
// If includes is empty, it's included. If it's not empty, the path either needs to match exactly, or it needs to be a direct parent of an included path.
392-
&& (includePaths.value.isEmpty || _isIncludedPathSorted(path, sortedPaths: includePaths))
392+
&& (includePaths.flatMap { _isIncludedPathSorted(path, sortedPaths: $0) } ?? true)
393393
}
394394

395395
private func _isIncludedPathSorted(_ path: Path, sortedPaths: SortedArray<Path>) -> Bool {
@@ -400,7 +400,7 @@ class SourceGenerator {
400400

401401

402402
/// Gets all the children paths that aren't excluded
403-
private func getSourceChildren(targetSource: TargetSource, dirPath: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>) throws -> [Path] {
403+
private func getSourceChildren(targetSource: TargetSource, dirPath: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>?) throws -> [Path] {
404404
try dirPath.children()
405405
.filter {
406406
if $0.isDirectory {
@@ -429,7 +429,7 @@ class SourceGenerator {
429429
isBaseGroup: Bool,
430430
hasCustomParent: Bool,
431431
excludePaths: Set<Path>,
432-
includePaths: SortedArray<Path>,
432+
includePaths: SortedArray<Path>?,
433433
buildPhases: [Path: BuildPhaseSpec]
434434
) throws -> (sourceFiles: [SourceFile], groups: [PBXGroup]) {
435435

@@ -586,7 +586,7 @@ class SourceGenerator {
586586
let path = project.basePath + targetSource.path
587587
let excludePaths = getSourceMatches(targetSource: targetSource, patterns: targetSource.excludes)
588588
// generate included paths. Excluded paths will override this.
589-
let includePaths = getSourceMatches(targetSource: targetSource, patterns: targetSource.includes)
589+
let includePaths = targetSource.includes.isEmpty ? nil : getSourceMatches(targetSource: targetSource, patterns: targetSource.includes)
590590

591591
let type = resolvedTargetSourceType(for: targetSource, at: path)
592592

@@ -655,7 +655,7 @@ class SourceGenerator {
655655
isBaseGroup: true,
656656
hasCustomParent: hasCustomParent,
657657
excludePaths: excludePaths,
658-
includePaths: SortedArray(includePaths),
658+
includePaths: includePaths.flatMap(SortedArray.init(_:)),
659659
buildPhases: buildPhases
660660
)
661661

Tests/XcodeGenKitTests/SourceGeneratorTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,39 @@ class SourceGeneratorTests: XCTestCase {
10421042
try pbxProj.expectFileMissing(paths: ["Sources", "group", "file.swift"])
10431043
}
10441044

1045+
$0.it("handles includes with no matches correctly") {
1046+
let directories = """
1047+
Sources:
1048+
- file3.swift
1049+
- file3Tests.swift
1050+
- file2.swift
1051+
- file2Tests.swift
1052+
- group2:
1053+
- file.swift
1054+
- fileTests.swift
1055+
- group:
1056+
- file.swift
1057+
"""
1058+
try createDirectories(directories)
1059+
1060+
let includes = [
1061+
"**/*NonExistent.*",
1062+
]
1063+
1064+
let target = Target(name: "Test", type: .application, platform: .iOS, sources: [TargetSource(path: "Sources", includes: includes)])
1065+
1066+
let project = Project(basePath: directoryPath, name: "Test", targets: [target])
1067+
let pbxProj = try project.generatePbxProj()
1068+
1069+
try pbxProj.expectFileMissing(paths: ["Sources", "file2.swift"])
1070+
try pbxProj.expectFileMissing(paths: ["Sources", "file3.swift"])
1071+
try pbxProj.expectFileMissing(paths: ["Sources", "file2Tests.swift"])
1072+
try pbxProj.expectFileMissing(paths: ["Sources", "file3Tests.swift"])
1073+
try pbxProj.expectFileMissing(paths: ["Sources", "group2", "file.swift"])
1074+
try pbxProj.expectFileMissing(paths: ["Sources", "group2", "fileTests.swift"])
1075+
try pbxProj.expectFileMissing(paths: ["Sources", "group", "file.swift"])
1076+
}
1077+
10451078
$0.it("prioritizes excludes over includes when both are present") {
10461079
let directories = """
10471080
Sources:

0 commit comments

Comments
 (0)