Skip to content

Commit

Permalink
Merge pull request #132 from dmhts/fix-duplicate-build-products
Browse files Browse the repository at this point in the history
Fix: Duplicate build products in swift-6.0-DEVELOPMENT-SNAPSHOTs
  • Loading branch information
giginet authored Jul 7, 2024
2 parents 205cb96 + e471c99 commit 2d2f361
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
18 changes: 18 additions & 0 deletions Sources/ScipioKit/DescriptionPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,22 @@ struct BuildProduct: Hashable, Sendable {
var binaryTarget: BinaryTarget? {
target.underlyingTarget as? BinaryTarget
}

func hash(into hasher: inout Hasher) {
// Important: Relevant for swift-6.0+ toolchain versions. For the versions below
// this change has no effect as SwiftPM provides its own proper `Hashable`
// implementations for both `ResolvedPackage` and `ResolvedTarget`.
//
// We cannot directly use `ResolvedModule.id` here as `id` also includes `BuildTriple`.
// The reason for this is that `ResolvedModule.buildTriple` is parent-dependent; more
// specifically, the same `ResolvedModule` will have a different build triple depending
// on whether it is in a root or dependency position.
// For more context, see `ResolvedModule.updateBuildTriplesOfDependencies`.
//
// At the same time, build triples remain irrelevant for the `Scipio` use case where the
// build product must be the same regardless of the triple. Meanwhile, the target name and
// package identity remain relevant and unambiguously identify the build product.
hasher.combine(target.name)
hasher.combine(package.identity)
}
}
9 changes: 6 additions & 3 deletions Tests/ScipioKitTests/DescriptionPackageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ final class DescriptionPackageTests: XCTestCase {
XCTAssertEqual(package.name, "TestingPackage")

XCTAssertEqual(
Set(try package.resolveBuildProducts().map(\.target.name)),
try package.resolveBuildProducts().map(\.target.name),
[
"Logging",
"TestingPackage",
]
"MyTarget",
"ExecutableTarget",
"MyPlugin",
],
"Order of the resolved products should be correct"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,38 @@ let package = Package(
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "TestingPackage",
targets: ["TestingPackage"]),
targets: ["MyTarget"]
),
.plugin(
name: "MyPlugin",
targets: ["MyPlugin"]
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", .upToNextMinor(from: "1.4.4")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
// Directly exported targets
.target(
name: "TestingPackage",
name: "MyTarget",
dependencies: [
.product(name: "Logging", package: "swift-log"),
]
),
.plugin(
name: "MyPlugin",
capability: .command(
intent: .custom(
verb: "my-plugin-verb",
description: "my-plugin-description")),
dependencies: ["ExecutableTarget"]
),
// Transitevly exported targets
.executableTarget(
name: "ExecutableTarget",
dependencies: ["MyTarget"]
),
// Not exported (internal) targets
.executableTarget(
name: "InternalExecutableTarget",
dependencies: ["InternalRegularTarget"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import PackagePlugin

@main
struct MyPlugin: CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) async throws {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@main
public struct ExecutableTarget {
static func main() {}
}

0 comments on commit 2d2f361

Please sign in to comment.