Skip to content

Commit 297b487

Browse files
authored
[Commands] Migrate: Avoid injecting feature settings into every Swift… (#8700)
… module ### Motivation: Previously the feature flags in `:migrate` mode were injected globally into "destination" parameter and affected every module in the graph, this is technically harmless but creates a lot of noise and precludes some of the future optimizations i.e. avoid injecting `@concurrent` into some closure literals when context is already `@concurrent`. Instead of using build parameters, let's make the `Module.buildSettings` mutable in SwiftPM package and inject feature settings directly into `buildSettings` of the user-requested or all root package modules instead. ### Modifications: - Make `Module.buildSettings` to be a `package(set) var` instead of a `let` to allow for mutation my migration command. - Update `Migrate` command to inject the requested upcoming/experimental features in `:migrate` mode only into request targets or all non-plugin modules of root packages. ### Result: No more global build flags and extraneous warnings from dependency modules that shouldn't have migration enabled.
1 parent 43aa87a commit 297b487

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

Sources/Commands/PackageCommands/Migrate.swift

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extension SwiftPackageCommand {
8787

8888
let buildSystem = try await createBuildSystem(
8989
swiftCommandState,
90+
targets: self.options.targets,
9091
features: features
9192
)
9293

@@ -148,21 +149,35 @@ extension SwiftPackageCommand {
148149

149150
private func createBuildSystem(
150151
_ swiftCommandState: SwiftCommandState,
152+
targets: Set<String>? = .none,
151153
features: [SwiftCompilerFeature]
152154
) async throws -> BuildSystem {
153155
let toolsBuildParameters = try swiftCommandState.toolsBuildParameters
154-
var destinationBuildParameters = try swiftCommandState.productsBuildParameters
155-
156-
// Inject feature settings as flags. This is safe and not as invasive
157-
// as trying to update manifest because in adoption mode the features
158-
// can only produce warnings.
159-
for feature in features {
160-
destinationBuildParameters.flags.swiftCompilerFlags.append(contentsOf: [
161-
"-Xfrontend",
162-
"-enable-\(feature.upcoming ? "upcoming" : "experimental")-feature",
163-
"-Xfrontend",
164-
"\(feature.name):migrate",
165-
])
156+
let destinationBuildParameters = try swiftCommandState.productsBuildParameters
157+
158+
let modulesGraph = try await swiftCommandState.loadPackageGraph()
159+
160+
let addFeaturesToModule = { (module: ResolvedModule) in
161+
for feature in features {
162+
module.underlying.buildSettings.add(.init(values: [
163+
"-Xfrontend",
164+
"-enable-\(feature.upcoming ? "upcoming" : "experimental")-feature",
165+
"-Xfrontend",
166+
"\(feature.name):migrate",
167+
]), for: .OTHER_SWIFT_FLAGS)
168+
}
169+
}
170+
171+
if let targets {
172+
targets.lazy.compactMap {
173+
modulesGraph.module(for: $0)
174+
}.forEach(addFeaturesToModule)
175+
} else {
176+
for package in modulesGraph.rootPackages {
177+
package.modules.filter {
178+
$0.type != .plugin
179+
}.forEach(addFeaturesToModule)
180+
}
166181
}
167182

168183
return try await swiftCommandState.createBuildSystem(
@@ -171,7 +186,11 @@ extension SwiftPackageCommand {
171186
toolsBuildParameters: toolsBuildParameters,
172187
// command result output goes on stdout
173188
// ie "swift build" should output to stdout
174-
outputStream: TSCBasic.stdoutStream
189+
packageGraphLoader: {
190+
modulesGraph
191+
},
192+
outputStream: TSCBasic.stdoutStream,
193+
observabilityScope: swiftCommandState.observabilityScope
175194
)
176195
}
177196

Sources/PackageModel/Module/Module.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public class Module {
231231
public let others: [AbsolutePath]
232232

233233
/// The build settings assignments of this module.
234-
public let buildSettings: BuildSettings.AssignmentTable
234+
public package(set) var buildSettings: BuildSettings.AssignmentTable
235235

236236
@_spi(SwiftPMInternal)
237237
public let buildSettingsDescription: [TargetBuildSettingDescription.Setting]

0 commit comments

Comments
 (0)