Skip to content

Commit 3eefb15

Browse files
committed
Include out-of-target sources
1 parent 4bc76e0 commit 3eefb15

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

Sources/PackageLoading/TargetSourcesBuilder.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,23 @@ public struct TargetSourcesBuilder {
199199
additionalResources = []
200200
}
201201

202-
let headers = pathToRule.lazy.filter { $0.value == .header }.map { $0.key }.sorted()
203-
let compilePaths = pathToRule.lazy.filter { $0.value == .compile }.map { $0.key }
202+
var additionalSources: [Basics.AbsolutePath] = []
203+
var additionalHeaders: [Basics.AbsolutePath] = []
204+
if self.toolsVersion >= .vNext {
205+
if let declaredSources = self.declaredSources {
206+
let unhandledSources = self.computeContents(for: declaredSources.filter { !$0.isDescendant(of: self.targetPath) })
207+
for source in unhandledSources {
208+
if let ext = source.extension, FileRuleDescription.header.fileTypes.contains(ext) {
209+
additionalHeaders.append(source)
210+
} else {
211+
additionalSources.append(source)
212+
}
213+
}
214+
}
215+
}
216+
1
217+
let headers = (pathToRule.lazy.filter { $0.value == .header }.map { $0.key } + additionalHeaders).sorted()
218+
let compilePaths = (pathToRule.lazy.filter { $0.value == .compile }.map { $0.key } + additionalSources)
204219
let sources = Sources(paths: Array(compilePaths).sorted(), root: targetPath)
205220
let resources: [Resource] = (pathToRule.compactMap { resource(for: $0.key, with: $0.value) } + additionalResources).sorted { a, b in
206221
a.path.pathString < b.path.pathString
@@ -415,9 +430,9 @@ public struct TargetSourcesBuilder {
415430
///
416431
/// This avoids recursing into certain directories like exclude or the
417432
/// ones that should be copied as-is.
418-
public func computeContents() -> [Basics.AbsolutePath] {
433+
public func computeContents(for paths: [Basics.AbsolutePath]) -> [Basics.AbsolutePath] {
419434
var contents: [Basics.AbsolutePath] = []
420-
var queue: [Basics.AbsolutePath] = [targetPath]
435+
var queue: [Basics.AbsolutePath] = paths
421436

422437
// Ignore xcodeproj and playground directories.
423438
var ignoredDirectoryExtensions = ["xcodeproj", "playground", "xcworkspace"]
@@ -519,6 +534,10 @@ public struct TargetSourcesBuilder {
519534
return contents
520535
}
521536

537+
public func computeContents() -> [Basics.AbsolutePath] {
538+
self.computeContents(for: [self.targetPath])
539+
}
540+
522541
public static func computeContents(for generatedFiles: [Basics.AbsolutePath], toolsVersion: ToolsVersion, additionalFileRules: [FileRuleDescription], defaultLocalization: String?, targetName: String, targetPath: Basics.AbsolutePath, observabilityScope: ObservabilityScope) -> (sources: [Basics.AbsolutePath], resources: [Resource]) {
523542
var sources = [Basics.AbsolutePath]()
524543
var resources = [Resource]()

Tests/PackageLoadingTests/PackageBuilderTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,6 +3477,33 @@ final class PackageBuilderTests: XCTestCase {
34773477
}
34783478
}
34793479
}
3480+
3481+
func testOutOfTargetSource() throws {
3482+
let fs = InMemoryFileSystem(
3483+
emptyFiles:
3484+
"/Sources/foo/foo.swift",
3485+
"/Sources/foo/foobar/foobar.swift",
3486+
3487+
"/Sources/bar/bar.swift"
3488+
)
3489+
3490+
let manifest = try Manifest.createRootManifest(
3491+
displayName: "pkg",
3492+
toolsVersion: .vNext,
3493+
targets: [
3494+
TargetDescription(
3495+
name: "foo",
3496+
sources: ["../bar/bar.swift", "foobar"]
3497+
),
3498+
]
3499+
)
3500+
3501+
PackageBuilderTester(manifest, in: fs) { package, _ in
3502+
package.checkModule("foo") { module in
3503+
module.checkSources(sources: ["foobar/foobar.swift","../bar/bar.swift"])
3504+
}
3505+
}
3506+
}
34803507
}
34813508

34823509
final class PackageBuilderTester {

0 commit comments

Comments
 (0)