Skip to content

Commit cfe1c75

Browse files
committed
Fix create symbol graph plugin entrypoint
The functionality of `createSymbolGraphForPlugin` broke when the internal SwiftPM build graph was split into target and host graphs. A client running `createSymbolGraphForPlugin` on a host target would fail with `unspecified("could not find a target named “<Target-Name>”")` This PR fixes the plugin entry point to search for targets in the host build graph if not found in the target graph. This PR also includes a cosmetic fix for the above error so it renders as `Unspecified: Could not find a target named “MMIOMacros”.`
1 parent 73b0b26 commit cfe1c75

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

Sources/Commands/Utilities/PluginDelegate.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import Basics
1414
import CoreCommands
1515
import Foundation
16+
import PackageGraph
1617
import PackageModel
1718
import SPMBuildCore
1819

@@ -383,14 +384,38 @@ final class PluginDelegate: PluginInvocationDelegate {
383384
// Create a build system for building the target., skipping the the cache because we need the build plan.
384385
let buildSystem = try swiftCommandState.createBuildSystem(explicitBuildSystem: .native, cacheBuildManifest: false)
385386

386-
// Find the target in the build operation's package graph; it's an error if we don't find it.
387+
// Find the target in the build operation's package graph
388+
// 1. First look for a matching destination module
389+
// 2. If not found, search for a matching tools module, only allowing
390+
// modules that should be built for the host (macro, plugin, test).
391+
// 3. Error if no matching targets are found.
392+
//
393+
// FIXME: dynamic graph
394+
// This should work by requesting a build for a target w/ destination:
395+
// .destination and generating the graph needed on demand instead of
396+
// looking for a preexisting matching target.
397+
let target: ResolvedModule
398+
let destination: BuildParameters.Destination
387399
let packageGraph = try buildSystem.getPackageGraph()
388-
guard let target = packageGraph.target(for: targetName, destination: .destination) else {
389-
throw StringError("could not find a target named “\(targetName)")
400+
if let _target = packageGraph.target(for: targetName, destination: .destination) {
401+
target = _target
402+
destination = .target
403+
} else if let _target = packageGraph.target(for: targetName, destination: .tools),
404+
(_target.type == .macro || _target.type == .plugin || _target.type == .test) {
405+
target = _target
406+
destination = .host
407+
} else {
408+
throw StringError("Could not find a target named “\(targetName)")
390409
}
391410

392411
// Build the target, if needed.
393-
try buildSystem.build(subset: .target(target.name))
412+
try buildSystem.build(subset: .target(target.name, for: destination))
413+
414+
let buildParameters: BuildParameters = try if destination == .target {
415+
buildSystem.buildPlan.destinationBuildParameters
416+
} else {
417+
buildSystem.buildPlan.toolsBuildParameters
418+
}
394419

395420
// Configure the symbol graph extractor.
396421
var symbolGraphExtractor = try SymbolGraphExtract(
@@ -419,7 +444,7 @@ final class PluginDelegate: PluginInvocationDelegate {
419444
guard let package = packageGraph.package(for: target) else {
420445
throw StringError("could not determine the package for target “\(target.name)")
421446
}
422-
let outputDir = try buildSystem.buildPlan.toolsBuildParameters.dataPath.appending(
447+
let outputDir = buildParameters.dataPath.appending(
423448
components: "extracted-symbols",
424449
package.identity.description,
425450
target.name
@@ -430,7 +455,7 @@ final class PluginDelegate: PluginInvocationDelegate {
430455
let result = try symbolGraphExtractor.extractSymbolGraph(
431456
module: target,
432457
buildPlan: try buildSystem.buildPlan,
433-
buildParameters: buildSystem.buildPlan.destinationBuildParameters,
458+
buildParameters: buildParameters,
434459
outputRedirection: .collect,
435460
outputDirectory: outputDir,
436461
verboseOutput: self.swiftCommandState.logLevel <= .info

Sources/PackagePlugin/PackageManagerProxy.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ public enum PackageManagerProxyError: Error {
301301
case unspecified(_ message: String)
302302
}
303303

304+
extension PackageManagerProxyError: CustomStringConvertible {
305+
public var description: String {
306+
switch self {
307+
case .unimplemented(let message):
308+
"Unimplemented: \(message)"
309+
case .unspecified(let message):
310+
"Unspecified: \(message)"
311+
}
312+
}
313+
}
314+
304315
fileprivate extension PluginToHostMessage.BuildSubset {
305316
init(_ subset: PackageManager.BuildSubset) {
306317
switch subset {

0 commit comments

Comments
 (0)