Skip to content
Merged
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
3 changes: 2 additions & 1 deletion Sources/PackageLoading/PackageBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,9 @@ public final class PackageBuilder {
if self.manifest.packageKind.isRoot {
// Snippets: depend on all available library targets in the package.
// TODO: Do we need to filter out targets that aren't available on the host platform?
let productTargets = Set(manifest.products.flatMap { $0.targets })
let snippetDependencies = targets
.filter { $0.type == .library }
.filter { $0.type == .library && productTargets.contains($0.name) }
.map { Target.Dependency.target($0, conditions: []) }
snippetTargets = try createSnippetTargets(dependencies: snippetDependencies)
} else {
Expand Down
40 changes: 40 additions & 0 deletions Tests/PackageLoadingTests/PackageBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,46 @@ class PackageBuilderTests: XCTestCase {
}
}
}

func testSnippetsLinkProductLibraries() throws {
let root = AbsolutePath(path: "/Foo")
let internalSourcesDir = root.appending(components: "Sources", "Internal")
let productSourcesDir = root.appending(components: "Sources", "Product")
let snippetsDir = root.appending(components: "Snippets")
let fs = InMemoryFileSystem(emptyFiles:
internalSourcesDir.appending(component: "Internal.swift").pathString,
productSourcesDir.appending(component: "Product.swift").pathString,
snippetsDir.appending(component: "ASnippet.swift").pathString)

let manifest = Manifest.createRootManifest(
name: "Foo", toolsVersion: .v5_7,
products: [
try ProductDescription(name: "Product", type: .library(.automatic), targets: ["Product"])
],
targets: [
try TargetDescription(name: "Internal"),
try TargetDescription(name: "Product"),
])

PackageBuilderTester(manifest, path: root, in: fs) { result, diagnostics in
result.checkProduct("Product") { product in
product.check(type: .library(.automatic), targets: ["Product"])
}
result.checkProduct("ASnippet") { aSnippet in
aSnippet.check(type: .snippet, targets: ["ASnippet"])
}
result.checkModule("Internal") { foo in
foo.checkSources(sources: ["Internal.swift"])
}
result.checkModule("Product") { foo in
foo.checkSources(sources: ["Product.swift"])
}
result.checkModule("ASnippet") { aSnippet in
aSnippet.checkSources(sources: ["ASnippet.swift"])
aSnippet.check(targetDependencies: ["Product"])
}
}
}
}

final class PackageBuilderTester {
Expand Down