Skip to content

Only automatically link snippets to product libraries #5929

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

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