Skip to content

Validation of package products should check that a library product doesn't try to include executable targets #5625

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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// swift-tools-version: 5.7
import PackageDescription

let package = Package(
name: "PackageWithMalformedLibraryProduct",
products: [
.library(
name: "PackageWithMalformedLibraryProduct",
targets: ["PackageWithMalformedLibraryProduct"]),
],
targets: [
.executableTarget(
name: "PackageWithMalformedLibraryProduct")
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello World")
4 changes: 4 additions & 0 deletions Sources/PackageLoading/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ extension Basics.Diagnostic {
.error("system library product \(product) shouldn't have a type and contain only one target")
}

static func libraryProductWithExecutableTarget(product: String, executableTargets: [String]) -> Self {
.error("library product '\(product)' should not contain executable targets (it has \(executableTargets.map{ "'\($0)'" }.joined(separator: ", ")))")
}

static func nonPluginProductWithPluginTargets(product: String, type: ProductType, pluginTargets: [String]) -> Self {
.error("\(type.description) product '\(product)' should not contain plugin targets (it has \(pluginTargets.map{ "'\($0)'" }.joined(separator: ", ")))")
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/PackageLoading/PackageBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,13 @@ public final class PackageBuilder {
self.observabilityScope.emit(.nonPluginProductWithPluginTargets(product: product.name, type: product.type, pluginTargets: pluginTargets.map{ $0.name }))
return false
}
if manifest.toolsVersion >= .v5_7 {
let executableTargets = targets.filter { $0.type == .executable }
guard executableTargets.isEmpty else {
self.observabilityScope.emit(.libraryProductWithExecutableTarget(product: product.name, executableTargets: executableTargets.map{ $0.name }))
return false
}
}
return true
}

Expand Down
12 changes: 12 additions & 0 deletions Tests/FunctionalTests/MiscellaneousTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,18 @@ class MiscellaneousTestCase: XCTestCase {
}
}

func testLibraryTriesToIncludeExecutableTarget() throws {
try fixture(name: "Miscellaneous/PackageWithMalformedLibraryProduct") { path in
XCTAssertThrowsCommandExecutionError(try executeSwiftBuild(path)) { error in
// if our code crashes we'll get an exit code of 256
guard error.result.exitStatus == .terminated(code: 1) else {
return XCTFail("failed in an unexpected manner: \(error)")
}
XCTAssertMatch(error.stdout + error.stderr, .contains("library product 'PackageWithMalformedLibraryProduct' should not contain executable targets (it has 'PackageWithMalformedLibraryProduct')"))
}
}
}

func testEditModeEndToEnd() throws {
try fixture(name: "Miscellaneous/Edit") { fixturePath in
let prefix = resolveSymlinks(fixturePath)
Expand Down