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
This commit is contained in:
Shaun Harrison 2023-08-16 23:58:50 -04:00 committed by GitHub
parent b448a6718f
commit 3a7e75f1fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 6 deletions

View File

@ -384,12 +384,12 @@ class SourceGenerator {
}
/// Checks whether the path is not in any default or TargetSource excludes
func isIncludedPath(_ path: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>) -> Bool {
func isIncludedPath(_ path: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>?) -> Bool {
return !defaultExcludedFiles.contains(where: { path.lastComponent == $0 })
&& !(path.extension.map(defaultExcludedExtensions.contains) ?? false)
&& !excludePaths.contains(path)
// 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.
&& (includePaths.value.isEmpty || _isIncludedPathSorted(path, sortedPaths: includePaths))
&& (includePaths.flatMap { _isIncludedPathSorted(path, sortedPaths: $0) } ?? true)
}
private func _isIncludedPathSorted(_ path: Path, sortedPaths: SortedArray<Path>) -> Bool {
@ -400,7 +400,7 @@ class SourceGenerator {
/// Gets all the children paths that aren't excluded
private func getSourceChildren(targetSource: TargetSource, dirPath: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>) throws -> [Path] {
private func getSourceChildren(targetSource: TargetSource, dirPath: Path, excludePaths: Set<Path>, includePaths: SortedArray<Path>?) throws -> [Path] {
try dirPath.children()
.filter {
if $0.isDirectory {
@ -429,7 +429,7 @@ class SourceGenerator {
isBaseGroup: Bool,
hasCustomParent: Bool,
excludePaths: Set<Path>,
includePaths: SortedArray<Path>,
includePaths: SortedArray<Path>?,
buildPhases: [Path: BuildPhaseSpec]
) throws -> (sourceFiles: [SourceFile], groups: [PBXGroup]) {
@ -586,7 +586,7 @@ class SourceGenerator {
let path = project.basePath + targetSource.path
let excludePaths = getSourceMatches(targetSource: targetSource, patterns: targetSource.excludes)
// generate included paths. Excluded paths will override this.
let includePaths = getSourceMatches(targetSource: targetSource, patterns: targetSource.includes)
let includePaths = targetSource.includes.isEmpty ? nil : getSourceMatches(targetSource: targetSource, patterns: targetSource.includes)
let type = resolvedTargetSourceType(for: targetSource, at: path)
@ -655,7 +655,7 @@ class SourceGenerator {
isBaseGroup: true,
hasCustomParent: hasCustomParent,
excludePaths: excludePaths,
includePaths: SortedArray(includePaths),
includePaths: includePaths.flatMap(SortedArray.init(_:)),
buildPhases: buildPhases
)

View File

@ -1042,6 +1042,39 @@ class SourceGeneratorTests: XCTestCase {
try pbxProj.expectFileMissing(paths: ["Sources", "group", "file.swift"])
}
$0.it("handles includes with no matches correctly") {
let directories = """
Sources:
- file3.swift
- file3Tests.swift
- file2.swift
- file2Tests.swift
- group2:
- file.swift
- fileTests.swift
- group:
- file.swift
"""
try createDirectories(directories)
let includes = [
"**/*NonExistent.*",
]
let target = Target(name: "Test", type: .application, platform: .iOS, sources: [TargetSource(path: "Sources", includes: includes)])
let project = Project(basePath: directoryPath, name: "Test", targets: [target])
let pbxProj = try project.generatePbxProj()
try pbxProj.expectFileMissing(paths: ["Sources", "file2.swift"])
try pbxProj.expectFileMissing(paths: ["Sources", "file3.swift"])
try pbxProj.expectFileMissing(paths: ["Sources", "file2Tests.swift"])
try pbxProj.expectFileMissing(paths: ["Sources", "file3Tests.swift"])
try pbxProj.expectFileMissing(paths: ["Sources", "group2", "file.swift"])
try pbxProj.expectFileMissing(paths: ["Sources", "group2", "fileTests.swift"])
try pbxProj.expectFileMissing(paths: ["Sources", "group", "file.swift"])
}
$0.it("prioritizes excludes over includes when both are present") {
let directories = """
Sources: