Skip to content

Include out-of-target sources #8900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions Sources/PackageLoading/TargetSourcesBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,23 @@ public struct TargetSourcesBuilder {
additionalResources = []
}

let headers = pathToRule.lazy.filter { $0.value == .header }.map { $0.key }.sorted()
let compilePaths = pathToRule.lazy.filter { $0.value == .compile }.map { $0.key }
var additionalSources: [Basics.AbsolutePath] = []
var additionalHeaders: [Basics.AbsolutePath] = []
if self.toolsVersion >= .vNext {
if let declaredSources = self.declaredSources {
let unhandledSources = self.computeContents(for: declaredSources.filter { !$0.isDescendant(of: self.targetPath) })
for source in unhandledSources {
if let ext = source.extension, FileRuleDescription.header.fileTypes.contains(ext) {
additionalHeaders.append(source)
} else {
additionalSources.append(source)
}
}
}
}

let headers = (pathToRule.lazy.filter { $0.value == .header }.map { $0.key } + additionalHeaders).sorted()
let compilePaths = (pathToRule.lazy.filter { $0.value == .compile }.map { $0.key } + additionalSources)
let sources = Sources(paths: Array(compilePaths).sorted(), root: targetPath)
let resources: [Resource] = (pathToRule.compactMap { resource(for: $0.key, with: $0.value) } + additionalResources).sorted { a, b in
a.path.pathString < b.path.pathString
Expand Down Expand Up @@ -415,9 +430,9 @@ public struct TargetSourcesBuilder {
///
/// This avoids recursing into certain directories like exclude or the
/// ones that should be copied as-is.
public func computeContents() -> [Basics.AbsolutePath] {
public func computeContents(for paths: [Basics.AbsolutePath]) -> [Basics.AbsolutePath] {
var contents: [Basics.AbsolutePath] = []
var queue: [Basics.AbsolutePath] = [targetPath]
var queue: [Basics.AbsolutePath] = paths

// Ignore xcodeproj and playground directories.
var ignoredDirectoryExtensions = ["xcodeproj", "playground", "xcworkspace"]
Expand Down Expand Up @@ -519,6 +534,10 @@ public struct TargetSourcesBuilder {
return contents
}

public func computeContents() -> [Basics.AbsolutePath] {
self.computeContents(for: [self.targetPath])
}

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]) {
var sources = [Basics.AbsolutePath]()
var resources = [Resource]()
Expand Down
27 changes: 27 additions & 0 deletions Tests/PackageLoadingTests/PackageBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3477,6 +3477,33 @@ final class PackageBuilderTests: XCTestCase {
}
}
}

func testOutOfTargetSource() throws {
let fs = InMemoryFileSystem(
emptyFiles:
"/Sources/foo/foo.swift",
"/Sources/foo/foobar/foobar.swift",

"/Sources/bar/bar.swift"
)

let manifest = try Manifest.createRootManifest(
displayName: "pkg",
toolsVersion: .vNext,
targets: [
TargetDescription(
name: "foo",
sources: ["../bar/bar.swift", "foobar"]
),
]
)

PackageBuilderTester(manifest, in: fs) { package, _ in
package.checkModule("foo") { module in
module.checkSources(sources: ["foobar/foobar.swift","../bar/bar.swift"])
}
}
}
}

final class PackageBuilderTester {
Expand Down