Skip to content

Commit 46e57fc

Browse files
PackageToPlugin: Fix build when the repo is cloned as a different name
1 parent d9b5e3a commit 46e57fc

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Plugins/PackageToJS/Sources/PackageToJSPlugin.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ struct PackageToJSPlugin: CommandPlugin {
166166
}
167167
}
168168

169-
static let JAVASCRIPTKIT_PACKAGE_ID: Package.ID = "javascriptkit"
169+
static let JAVASCRIPTKIT_PRODUCT_ID: Product.ID = "JavaScriptKit"
170170

171171
func performBuildCommand(context: PluginContext, arguments: [String]) throws {
172172
if arguments.contains(where: { ["-h", "--help"].contains($0) }) {
@@ -396,7 +396,7 @@ struct PackageToJSPlugin: CommandPlugin {
396396
guard
397397
let selfPackage = findPackageInDependencies(
398398
package: package,
399-
id: Self.JAVASCRIPTKIT_PACKAGE_ID
399+
including: Self.JAVASCRIPTKIT_PRODUCT_ID
400400
)
401401
else {
402402
throw PackageToJSError("Failed to find JavaScriptKit in dependencies!?")
@@ -649,12 +649,24 @@ extension PackageManager.BuildResult {
649649
}
650650
}
651651

652-
private func findPackageInDependencies(package: Package, id: Package.ID) -> Package? {
652+
/// Find the package that contains the product with the given name
653+
/// - Parameters:
654+
/// - package: The package to search in
655+
/// - productName: The name of the product to find
656+
/// - Returns: The package that contains the product with the given name
657+
/// - Note: Why not use `Package.ID`? `Package.ID` is not always equal to the package repository name
658+
/// as it's derived from the directory name when the package is dependent on another package as a
659+
// local package.
660+
private func findPackageInDependencies(package: Package, including productName: String) -> Package? {
653661
var visited: Set<Package.ID> = []
654662
func visit(package: Package) -> Package? {
655663
if visited.contains(package.id) { return nil }
656664
visited.insert(package.id)
657-
if package.id == id { return package }
665+
for product in package.products {
666+
if product.name == productName {
667+
return package
668+
}
669+
}
658670
for dependency in package.dependencies {
659671
if let found = visit(package: dependency.package) {
660672
return found

0 commit comments

Comments
 (0)