Skip to content

Commit 6ff5cbd

Browse files
authored
Rename PackageGraph type to ModulesGraph (#7363)
We have to operate on two different graphs in the `PackageGraph` module: 1. A graph of packages in PubGrub package resolution code; 2. A graph of resolved products and modules that belong to those products. Currently the second graph is misleadingly called `PackageGraph`, although that name is much more suitable for the first graph. This naming is confusing and unfortunate, and can be especially misleading for first-time contributors. We should better document the SwiftPM resolution and build pipeline in the future, but cleaning up naming is the first step. I'm keeping old names as deprecated overloads or typealiases to make migration easier for SwiftPM clients. This renaming has no impact on any public stable modules like `PackageDescription`. In the short term we should also split the `PackageGraph` module into `PubGrub` and `ModulesGraph` modules, but for now I'm renaming a single type to keep the PR manageable.
1 parent d7878b0 commit 6ff5cbd

33 files changed

+288
-244
lines changed

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import Basics
1414
import PackageLoading
1515
import PackageModel
16-
import struct PackageGraph.PackageGraph
16+
import struct PackageGraph.ModulesGraph
1717
import struct PackageGraph.ResolvedTarget
1818
import struct SPMBuildCore.BuildParameters
1919
import struct SPMBuildCore.BuildToolPluginInvocationResult
@@ -134,7 +134,7 @@ public final class ClangTargetBuildDescription {
134134
if toolsVersion >= .v5_9 {
135135
self.buildToolPluginInvocationResults = buildToolPluginInvocationResults
136136

137-
(self.pluginDerivedSources, self.pluginDerivedResources) = PackageGraph.computePluginGeneratedFiles(
137+
(self.pluginDerivedSources, self.pluginDerivedResources) = ModulesGraph.computePluginGeneratedFiles(
138138
target: target,
139139
toolsVersion: toolsVersion,
140140
additionalFileRules: additionalFileRules,

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public final class SwiftTargetBuildDescription {
287287
self.fileSystem = fileSystem
288288
self.observabilityScope = observabilityScope
289289

290-
(self.pluginDerivedSources, self.pluginDerivedResources) = PackageGraph.computePluginGeneratedFiles(
290+
(self.pluginDerivedSources, self.pluginDerivedResources) = ModulesGraph.computePluginGeneratedFiles(
291291
target: target,
292292
toolsVersion: toolsVersion,
293293
additionalFileRules: additionalFileRules,

Sources/Build/BuildOperation.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
4747
let toolsBuildParameters: BuildParameters
4848

4949
/// The closure for loading the package graph.
50-
let packageGraphLoader: () throws -> PackageGraph
50+
let packageGraphLoader: () throws -> ModulesGraph
5151

5252
/// the plugin configuration for build plugins
5353
let pluginConfiguration: PluginConfiguration?
@@ -78,7 +78,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
7878
private let buildDescription = ThreadSafeBox<BuildDescription>()
7979

8080
/// The loaded package graph.
81-
private let packageGraph = ThreadSafeBox<PackageGraph>()
81+
private let packageGraph = ThreadSafeBox<ModulesGraph>()
8282

8383
/// The output stream for the build delegate.
8484
private let outputStream: OutputByteStream
@@ -112,7 +112,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
112112
productsBuildParameters: BuildParameters,
113113
toolsBuildParameters: BuildParameters,
114114
cacheBuildManifest: Bool,
115-
packageGraphLoader: @escaping () throws -> PackageGraph,
115+
packageGraphLoader: @escaping () throws -> ModulesGraph,
116116
pluginConfiguration: PluginConfiguration? = .none,
117117
additionalFileRules: [FileRuleDescription],
118118
pkgConfigDirectories: [AbsolutePath],
@@ -145,7 +145,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
145145
self.observabilityScope = observabilityScope.makeChildScope(description: "Build Operation")
146146
}
147147

148-
public func getPackageGraph() throws -> PackageGraph {
148+
public func getPackageGraph() throws -> ModulesGraph {
149149
try self.packageGraph.memoize {
150150
try self.packageGraphLoader()
151151
}
@@ -877,7 +877,7 @@ extension BuildDescription {
877877
}
878878

879879
extension BuildSubset {
880-
func recursiveDependencies(for graph: PackageGraph, observabilityScope: ObservabilityScope) throws -> [ResolvedTarget]? {
880+
func recursiveDependencies(for graph: ModulesGraph, observabilityScope: ObservabilityScope) throws -> [ResolvedTarget]? {
881881
switch self {
882882
case .allIncludingTests:
883883
return Array(graph.reachableTargets)
@@ -899,7 +899,7 @@ extension BuildSubset {
899899
}
900900

901901
/// Returns the name of the llbuild target that corresponds to the build subset.
902-
func llbuildTargetName(for graph: PackageGraph, config: String, observabilityScope: ObservabilityScope)
902+
func llbuildTargetName(for graph: ModulesGraph, config: String, observabilityScope: ObservabilityScope)
903903
-> String?
904904
{
905905
switch self {

Sources/Build/BuildPlan/BuildPlan+Test.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import struct Basics.InternalError
1515
import struct Basics.AbsolutePath
1616
import struct LLBuildManifest.TestDiscoveryTool
1717
import struct LLBuildManifest.TestEntryPointTool
18-
import struct PackageGraph.PackageGraph
18+
import struct PackageGraph.ModulesGraph
1919
import struct PackageGraph.ResolvedProduct
2020
import struct PackageGraph.ResolvedTarget
2121
import struct PackageModel.Sources
@@ -27,7 +27,7 @@ import protocol TSCBasic.FileSystem
2727
extension BuildPlan {
2828
static func makeDerivedTestTargets(
2929
_ buildParameters: BuildParameters,
30-
_ graph: PackageGraph,
30+
_ graph: ModulesGraph,
3131
_ disableSandbox: Bool,
3232
_ fileSystem: FileSystem,
3333
_ observabilityScope: ObservabilityScope

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
196196
}
197197

198198
/// The package graph.
199-
public let graph: PackageGraph
199+
public let graph: ModulesGraph
200200

201201
/// The target build description map.
202202
public let targetMap: [ResolvedTarget.ID: TargetBuildDescription]
@@ -249,7 +249,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
249249
@available(*, deprecated, renamed: "init(productsBuildParameters:toolsBuildParameters:graph:)")
250250
public convenience init(
251251
buildParameters: BuildParameters,
252-
graph: PackageGraph,
252+
graph: ModulesGraph,
253253
additionalFileRules: [FileRuleDescription] = [],
254254
buildToolPluginInvocationResults: [ResolvedTarget.ID: [BuildToolPluginInvocationResult]] = [:],
255255
prebuildCommandResults: [ResolvedTarget.ID: [PrebuildCommandResult]] = [:],
@@ -272,7 +272,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
272272
public init(
273273
productsBuildParameters: BuildParameters,
274274
toolsBuildParameters: BuildParameters,
275-
graph: PackageGraph,
275+
graph: ModulesGraph,
276276
additionalFileRules: [FileRuleDescription] = [],
277277
buildToolPluginInvocationResults: [ResolvedTarget.ID: [BuildToolPluginInvocationResult]] = [:],
278278
prebuildCommandResults: [ResolvedTarget.ID: [PrebuildCommandResult]] = [:],

Sources/Commands/PackageTools/APIDiff.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ struct APIDiff: SwiftCommand {
160160
}
161161
}
162162

163-
private func determineModulesToDiff(packageGraph: PackageGraph, observabilityScope: ObservabilityScope) throws -> Set<String> {
163+
private func determineModulesToDiff(packageGraph: ModulesGraph, observabilityScope: ObservabilityScope) throws -> Set<String> {
164164
var modulesToDiff: Set<String> = []
165165
if products.isEmpty && targets.isEmpty {
166166
modulesToDiff.formUnion(packageGraph.apiDigesterModules)

Sources/Commands/PackageTools/PluginCommand.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ struct PluginCommand: SwiftCommand {
211211
static func run(
212212
plugin: PluginTarget,
213213
package: ResolvedPackage,
214-
packageGraph: PackageGraph,
214+
packageGraph: ModulesGraph,
215215
options: PluginOptions,
216216
arguments: [String],
217217
swiftTool: SwiftTool
@@ -369,7 +369,7 @@ struct PluginCommand: SwiftCommand {
369369
// TODO: We should also emit a final line of output regarding the result.
370370
}
371371

372-
static func availableCommandPlugins(in graph: PackageGraph, limitedTo packageIdentity: String?) -> [PluginTarget] {
372+
static func availableCommandPlugins(in graph: ModulesGraph, limitedTo packageIdentity: String?) -> [PluginTarget] {
373373
// All targets from plugin products of direct dependencies are "available".
374374
let directDependencyPackages = graph.rootPackages.flatMap { $0.dependencies }.filter { $0.matching(identity: packageIdentity) }
375375
let directDependencyPluginTargets = directDependencyPackages.flatMap { $0.products.filter { $0.type == .plugin } }.flatMap { $0.targets }
@@ -383,7 +383,7 @@ struct PluginCommand: SwiftCommand {
383383
}
384384
}
385385

386-
static func findPlugins(matching verb: String, in graph: PackageGraph, limitedTo packageIdentity: String?) -> [PluginTarget] {
386+
static func findPlugins(matching verb: String, in graph: ModulesGraph, limitedTo packageIdentity: String?) -> [PluginTarget] {
387387
// Find and return the command plugins that match the command.
388388
Self.availableCommandPlugins(in: graph, limitedTo: packageIdentity).filter {
389389
// Filter out any non-command plugins and any whose verb is different.

Sources/Commands/SwiftRunTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public struct SwiftRunTool: SwiftCommand {
216216
}
217217

218218
/// Returns the path to the correct executable based on options.
219-
private func findProductName(in graph: PackageGraph) throws -> String {
219+
private func findProductName(in graph: ModulesGraph) throws -> String {
220220
if let executable = options.executable {
221221
let executableExists = graph.allProducts.contains { ($0.type == .executable || $0.type == .snippet) && $0.name == executable }
222222
guard executableExists else {

Sources/Commands/Utilities/APIDigester.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ extension BuildParameters {
310310
}
311311
}
312312

313-
extension PackageGraph {
313+
extension ModulesGraph {
314314
/// The list of modules that should be used as an input to the API digester.
315315
var apiDigesterModules: [String] {
316316
self.rootPackages

Sources/CoreCommands/BuildSystemSupport.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import XCBuildSupport
1818
#endif
1919

2020
import class Basics.ObservabilityScope
21-
import struct PackageGraph.PackageGraph
21+
import struct PackageGraph.ModulesGraph
2222
import struct PackageLoading.FileRuleDescription
2323
import protocol TSCBasic.OutputByteStream
2424

@@ -30,7 +30,7 @@ private struct NativeBuildSystemFactory: BuildSystemFactory {
3030
cacheBuildManifest: Bool,
3131
productsBuildParameters: BuildParameters?,
3232
toolsBuildParameters: BuildParameters?,
33-
packageGraphLoader: (() throws -> PackageGraph)?,
33+
packageGraphLoader: (() throws -> ModulesGraph)?,
3434
outputStream: OutputByteStream?,
3535
logLevel: Diagnostic.Severity?,
3636
observabilityScope: ObservabilityScope?
@@ -72,7 +72,7 @@ private struct XcodeBuildSystemFactory: BuildSystemFactory {
7272
cacheBuildManifest: Bool,
7373
productsBuildParameters: BuildParameters?,
7474
toolsBuildParameters: BuildParameters?,
75-
packageGraphLoader: (() throws -> PackageGraph)?,
75+
packageGraphLoader: (() throws -> ModulesGraph)?,
7676
outputStream: OutputByteStream?,
7777
logLevel: Diagnostic.Severity?,
7878
observabilityScope: ObservabilityScope?

Sources/CoreCommands/SwiftTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ public final class SwiftTool {
594594
public func loadPackageGraph(
595595
explicitProduct: String? = nil,
596596
testEntryPointPath: AbsolutePath? = nil
597-
) throws -> PackageGraph {
597+
) throws -> ModulesGraph {
598598
do {
599599
let workspace = try getActiveWorkspace()
600600

@@ -684,7 +684,7 @@ public final class SwiftTool {
684684
shouldLinkStaticSwiftStdlib: Bool = false,
685685
productsBuildParameters: BuildParameters? = .none,
686686
toolsBuildParameters: BuildParameters? = .none,
687-
packageGraphLoader: (() throws -> PackageGraph)? = .none,
687+
packageGraphLoader: (() throws -> ModulesGraph)? = .none,
688688
outputStream: OutputByteStream? = .none,
689689
logLevel: Basics.Diagnostic.Severity? = .none,
690690
observabilityScope: ObservabilityScope? = .none

Sources/PackageGraph/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ add_library(PackageGraph
1313
Diagnostics.swift
1414
GraphLoadingNode.swift
1515
ModuleAliasTracker.swift
16+
ModulesGraph.swift
17+
ModulesGraph+Loading.swift
1618
PackageContainer.swift
17-
PackageGraph.swift
18-
PackageGraph+Loading.swift
1919
PackageGraphRoot.swift
2020
PackageModel+Extensions.swift
2121
PackageRequirement.swift

Sources/PackageGraph/PackageGraph+Loading.swift renamed to Sources/PackageGraph/ModulesGraph+Loading.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import PackageModel
1717

1818
import func TSCBasic.bestMatch
1919

20-
extension PackageGraph {
20+
extension ModulesGraph {
2121
/// Load the package graph for the given package path.
2222
public static func load(
2323
root: PackageGraphRoot,
@@ -35,7 +35,7 @@ extension PackageGraph {
3535
availableLibraries: [LibraryMetadata],
3636
fileSystem: FileSystem,
3737
observabilityScope: ObservabilityScope
38-
) throws -> PackageGraph {
38+
) throws -> ModulesGraph {
3939

4040
let observabilityScope = observabilityScope.makeChildScope(description: "Loading Package Graph")
4141

@@ -168,7 +168,7 @@ extension PackageGraph {
168168
let rootPackages = resolvedPackages.filter{ root.manifests.values.contains($0.manifest) }
169169
checkAllDependenciesAreUsed(rootPackages, observabilityScope: observabilityScope)
170170

171-
return try PackageGraph(
171+
return try ModulesGraph(
172172
rootPackages: rootPackages,
173173
rootDependencies: resolvedPackages.filter{ rootDependencies.contains($0.manifest) },
174174
dependencies: requiredDependencies,

Sources/PackageGraph/PackageGraph.swift renamed to Sources/PackageGraph/ModulesGraph.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ enum PackageGraphError: Swift.Error {
4949
aliases: [String])
5050
}
5151

52+
@available(*,
53+
deprecated,
54+
renamed: "ModulesGraph",
55+
message: "PackageGraph had a misleading name, it's a graph of dependencies between modules, not just packages"
56+
)
57+
public typealias PackageGraph = ModulesGraph
58+
5259
/// A collection of packages.
53-
public struct PackageGraph {
60+
public struct ModulesGraph {
5461
/// The root packages.
5562
public let rootPackages: IdentifiableSet<ResolvedPackage>
5663

Sources/SPMBuildCore/BuildSystem/BuildSystem.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public protocol BuildSystem: Cancellable {
4242
var builtTestProducts: [BuiltTestProduct] { get }
4343

4444
/// Returns the package graph used by the build system.
45-
func getPackageGraph() throws -> PackageGraph
45+
func getPackageGraph() throws -> ModulesGraph
4646

4747
/// Builds a subset of the package graph.
4848
/// - Parameters:
@@ -120,7 +120,7 @@ public protocol BuildSystemFactory {
120120
cacheBuildManifest: Bool,
121121
productsBuildParameters: BuildParameters?,
122122
toolsBuildParameters: BuildParameters?,
123-
packageGraphLoader: (() throws -> PackageGraph)?,
123+
packageGraphLoader: (() throws -> ModulesGraph)?,
124124
outputStream: OutputByteStream?,
125125
logLevel: Diagnostic.Severity?,
126126
observabilityScope: ObservabilityScope?
@@ -146,7 +146,7 @@ public struct BuildSystemProvider {
146146
cacheBuildManifest: Bool = true,
147147
productsBuildParameters: BuildParameters? = .none,
148148
toolsBuildParameters: BuildParameters? = .none,
149-
packageGraphLoader: (() throws -> PackageGraph)? = .none,
149+
packageGraphLoader: (() throws -> ModulesGraph)? = .none,
150150
outputStream: OutputByteStream? = .none,
151151
logLevel: Diagnostic.Severity? = .none,
152152
observabilityScope: ObservabilityScope? = .none

Sources/SPMBuildCore/Plugins/PluginInvocation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ fileprivate extension PluginToHostMessage {
366366
}
367367
}
368368

369-
extension PackageGraph {
369+
extension ModulesGraph {
370370

371371
/// Traverses the graph of reachable targets in a package graph, and applies plugins to targets as needed. Each
372372
/// plugin is passed an input context that provides information about the target to which it is being applied
@@ -660,7 +660,7 @@ public extension PluginTarget {
660660
}
661661

662662
/// The set of tools that are accessible to this plugin.
663-
private func accessibleTools(packageGraph: PackageGraph, fileSystem: FileSystem, environment: BuildEnvironment, for hostTriple: Triple) throws -> Set<PluginAccessibleTool> {
663+
private func accessibleTools(packageGraph: ModulesGraph, fileSystem: FileSystem, environment: BuildEnvironment, for hostTriple: Triple) throws -> Set<PluginAccessibleTool> {
664664
return try Set(self.dependencies(satisfying: environment).flatMap { dependency -> [PluginAccessibleTool] in
665665
let builtToolName: String
666666
let executableOrBinaryTarget: Target
@@ -695,7 +695,7 @@ public extension PluginTarget {
695695
})
696696
}
697697

698-
func processAccessibleTools(packageGraph: PackageGraph, fileSystem: FileSystem, environment: BuildEnvironment, for hostTriple: Triple, builtToolHandler: (_ name: String, _ path: RelativePath) throws -> AbsolutePath?) throws -> [String: (path: AbsolutePath, triples: [String]?)] {
698+
func processAccessibleTools(packageGraph: ModulesGraph, fileSystem: FileSystem, environment: BuildEnvironment, for hostTriple: Triple, builtToolHandler: (_ name: String, _ path: RelativePath) throws -> AbsolutePath?) throws -> [String: (path: AbsolutePath, triples: [String]?)] {
699699
var pluginAccessibleTools: [String: (path: AbsolutePath, triples: [String]?)] = [:]
700700

701701
for dep in try accessibleTools(packageGraph: packageGraph, fileSystem: fileSystem, environment: environment, for: hostTriple) {

Sources/SPMTestSupport/MockPackageGraphs.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import struct Basics.AbsolutePath
1414
import class Basics.ObservabilitySystem
1515
import class Basics.ObservabilityScope
16-
import struct PackageGraph.PackageGraph
16+
import struct PackageGraph.ModulesGraph
1717
import class PackageModel.Manifest
1818
import struct PackageModel.ProductDescription
1919
import struct PackageModel.TargetDescription
@@ -22,7 +22,7 @@ import class TSCBasic.InMemoryFileSystem
2222

2323
@_spi(SwiftPMInternal)
2424
public typealias MockPackageGraph = (
25-
graph: PackageGraph,
25+
graph: ModulesGraph,
2626
fileSystem: any FileSystem,
2727
observabilityScope: ObservabilityScope
2828
)

Sources/SPMTestSupport/MockWorkspace.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public final class MockWorkspace {
445445
public func checkPackageGraph(
446446
roots: [String] = [],
447447
deps: [MockDependency],
448-
_ result: (PackageGraph, [Basics.Diagnostic]) -> Void
448+
_ result: (ModulesGraph, [Basics.Diagnostic]) -> Void
449449
) throws {
450450
let dependencies = try deps.map { try $0.convert(baseURL: packagesDir, identityResolver: self.identityResolver) }
451451
try self.checkPackageGraph(roots: roots, dependencies: dependencies, result)
@@ -456,7 +456,7 @@ public final class MockWorkspace {
456456
dependencies: [PackageDependency] = [],
457457
forceResolvedVersions: Bool = false,
458458
expectedSigningEntities: [PackageIdentity: RegistryReleaseMetadata.SigningEntity] = [:],
459-
_ result: (PackageGraph, [Basics.Diagnostic]) throws -> Void
459+
_ result: (ModulesGraph, [Basics.Diagnostic]) throws -> Void
460460
) throws {
461461
let observability = ObservabilitySystem.makeForTesting()
462462
let rootInput = PackageGraphRootInput(

Sources/SPMTestSupport/PackageGraphTester.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import struct Basics.IdentifiableSet
1717
import PackageModel
1818
import PackageGraph
1919

20-
public func PackageGraphTester(_ graph: PackageGraph, _ result: (PackageGraphResult) -> Void) {
20+
public func PackageGraphTester(_ graph: ModulesGraph, _ result: (PackageGraphResult) -> Void) {
2121
result(PackageGraphResult(graph))
2222
}
2323

2424
public final class PackageGraphResult {
25-
public let graph: PackageGraph
25+
public let graph: ModulesGraph
2626

27-
public init(_ graph: PackageGraph) {
27+
public init(_ graph: ModulesGraph) {
2828
self.graph = graph
2929
}
3030

0 commit comments

Comments
 (0)