Skip to content

Commit 8a8ec82

Browse files
committed
[PackageGraph] Direct attempts to find first package/target by name through the graph
First step on the way to hide and remove `all{Products, Targets}` from modules graph because with cross-compilation attempting to fetch data from them directly could produce surprising results since they don't always account for build triples.
1 parent 1c62623 commit 8a8ec82

File tree

8 files changed

+22
-12
lines changed

8 files changed

+22
-12
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,13 +894,13 @@ extension BuildSubset {
894894
case .allExcludingTests:
895895
return graph.reachableTargets.filter { $0.type != .test }
896896
case .product(let productName):
897-
guard let product = graph.allProducts.first(where: { $0.name == productName }) else {
897+
guard let product = graph.product(for: productName) else {
898898
observabilityScope.emit(error: "no product named '\(productName)'")
899899
return nil
900900
}
901901
return try product.recursiveTargetDependencies()
902902
case .target(let targetName):
903-
guard let target = graph.allTargets.first(where: { $0.name == targetName }) else {
903+
guard let target = graph.target(for: targetName) else {
904904
observabilityScope.emit(error: "no target named '\(targetName)'")
905905
return nil
906906
}
@@ -920,7 +920,7 @@ extension BuildSubset {
920920
case .allIncludingTests:
921921
return LLBuildManifestBuilder.TargetKind.test.targetName
922922
case .product(let productName):
923-
guard let product = graph.allProducts.first(where: { $0.name == productName }) else {
923+
guard let product = graph.product(for: productName) else {
924924
observabilityScope.emit(error: "no product named '\(productName)'")
925925
return nil
926926
}
@@ -937,7 +937,7 @@ extension BuildSubset {
937937
try product.getLLBuildTargetName(buildParameters: buildParameters)
938938
}
939939
case .target(let targetName):
940-
guard let target = graph.allTargets.first(where: { $0.name == targetName }) else {
940+
guard let target = graph.target(for: targetName) else {
941941
observabilityScope.emit(error: "no target named '\(targetName)'")
942942
return nil
943943
}

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
595595
arguments.append("-l" + replProductName)
596596

597597
// The graph should have the REPL product.
598-
assert(self.graph.allProducts.first(where: { $0.name == replProductName }) != nil)
598+
assert(self.graph.product(for: replProductName) != nil)
599599

600600
// Add the search path to the directory containing the modulemap file.
601601
for target in self.targets {

Sources/Commands/Snippets/Cards/SnippetCard.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct SnippetCard: Card {
9696
let buildSystem = try swiftCommandState.createBuildSystem(explicitProduct: snippet.name)
9797
try buildSystem.build(subset: .product(snippet.name))
9898
let executablePath = try swiftCommandState.productsBuildParameters.buildPath.appending(component: snippet.name)
99-
if let exampleTarget = try buildSystem.getPackageGraph().allTargets.first(where: { $0.name == snippet.name }) {
99+
if let exampleTarget = try buildSystem.getPackageGraph().target(for: snippet.name) {
100100
try ProcessEnv.chdir(exampleTarget.sources.paths[0].parentDirectory)
101101
}
102102
try exec(path: executablePath.pathString, args: [])

Sources/Commands/Utilities/PluginDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ final class PluginDelegate: PluginInvocationDelegate {
385385

386386
// Find the target in the build operation's package graph; it's an error if we don't find it.
387387
let packageGraph = try buildSystem.getPackageGraph()
388-
guard let target = packageGraph.allTargets.first(where: { $0.name == targetName }) else {
388+
guard let target = packageGraph.target(for: targetName) else {
389389
throw StringError("could not find a target named “\(targetName)")
390390
}
391391

Sources/PackageGraph/ModulesGraph.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ public struct ModulesGraph {
166166
package.dependencies.compactMap { self.package(for: $0) }
167167
}
168168

169+
public func product(for name: String) -> ResolvedProduct? {
170+
// FIXME: This is wrong since graph can contain products with the same name but different triples.
171+
self.allProducts.first { $0.name == name }
172+
}
173+
174+
public func target(for name: String) -> ResolvedModule? {
175+
// FIXME: This is wrong since graph can contain products with the same name but different triples.
176+
self.allTargets.first { $0.name == name }
177+
}
178+
169179
/// All root and root dependency packages provided as input to the graph.
170180
public let inputPackages: [ResolvedPackage]
171181

Sources/SPMBuildCore/Plugins/PluginInvocation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ public extension PluginTarget {
677677
executableOrBinaryTarget = target
678678
case .product(let productRef, _):
679679
guard
680-
let product = packageGraph.allProducts.first(where: { $0.name == productRef.name }),
680+
let product = packageGraph.product(for: productRef.name),
681681
let executableTarget = product.targets.map({ $0.underlying }).executables.spm_only
682682
else {
683683
throw StringError("no product named \(productRef.name)")

Tests/PackageGraphTests/ModulesGraphTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ final class ModulesGraphTests: XCTestCase {
8484
}
8585

8686
let fooPackage = try XCTUnwrap(g.package(for: .plain("Foo")))
87-
let fooTarget = try XCTUnwrap(g.allTargets.first{ $0.name == "Foo" })
88-
let fooDepTarget = try XCTUnwrap(g.allTargets.first{ $0.name == "FooDep" })
87+
let fooTarget = try XCTUnwrap(g.target(for: "Foo"))
88+
let fooDepTarget = try XCTUnwrap(g.target(for: "FooDep"))
8989
XCTAssertEqual(g.package(for: fooTarget)?.id, fooPackage.id)
9090
XCTAssertEqual(g.package(for: fooDepTarget)?.id, fooPackage.id)
9191
let barPackage = try XCTUnwrap(g.package(for: .plain("Bar")))
92-
let barTarget = try XCTUnwrap(g.allTargets.first{ $0.name == "Bar" })
92+
let barTarget = try XCTUnwrap(g.target(for: "Bar"))
9393
XCTAssertEqual(g.package(for: barTarget)?.id, barPackage.id)
9494
}
9595

Tests/SourceKitLSPAPITests/SourceKitLSPAPITests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ extension SourceKitLSPAPI.BuildDescription {
8787
partialArguments: [String],
8888
isPartOfRootPackage: Bool
8989
) throws -> Bool {
90-
let target = try XCTUnwrap(graph.allTargets.first(where: { $0.name == targetName }))
90+
let target = try XCTUnwrap(graph.target(for: targetName))
9191
let buildTarget = try XCTUnwrap(self.getBuildTarget(for: target, in: graph))
9292

9393
guard let file = buildTarget.sources.first else {

0 commit comments

Comments
 (0)