Skip to content

[6.2] Cherry-pick of all of the recent swift package migrate related fixes #8710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 49 additions & 31 deletions Sources/Commands/PackageCommands/Migrate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ extension SwiftPackageCommand {

let buildSystem = try await createBuildSystem(
swiftCommandState,
targets: self.options.targets,
features: features
)

Expand Down Expand Up @@ -137,34 +138,46 @@ extension SwiftPackageCommand {

print("> Updating manifest.")
for module in modules.map(\.module) {
print("> Adding feature(s) to '\(module.name)'.")
for feature in features {
self.updateManifest(
for: module.name,
add: feature,
using: swiftCommandState
)
}
swiftCommandState.observabilityScope.emit(debug: "Adding feature(s) to '\(module.name)'.")
self.updateManifest(
for: module.name,
add: features,
using: swiftCommandState
)
}
}

private func createBuildSystem(
_ swiftCommandState: SwiftCommandState,
targets: Set<String>? = .none,
features: [SwiftCompilerFeature]
) async throws -> BuildSystem {
let toolsBuildParameters = try swiftCommandState.toolsBuildParameters
var destinationBuildParameters = try swiftCommandState.productsBuildParameters

// Inject feature settings as flags. This is safe and not as invasive
// as trying to update manifest because in adoption mode the features
// can only produce warnings.
for feature in features {
destinationBuildParameters.flags.swiftCompilerFlags.append(contentsOf: [
"-Xfrontend",
"-enable-\(feature.upcoming ? "upcoming" : "experimental")-feature",
"-Xfrontend",
"\(feature.name):migrate",
])
let destinationBuildParameters = try swiftCommandState.productsBuildParameters

let modulesGraph = try await swiftCommandState.loadPackageGraph()

let addFeaturesToModule = { (module: ResolvedModule) in
for feature in features {
module.underlying.buildSettings.add(.init(values: [
"-Xfrontend",
"-enable-\(feature.upcoming ? "upcoming" : "experimental")-feature",
"-Xfrontend",
"\(feature.name):migrate",
]), for: .OTHER_SWIFT_FLAGS)
}
}

if let targets {
targets.lazy.compactMap {
modulesGraph.module(for: $0)
}.forEach(addFeaturesToModule)
} else {
for package in modulesGraph.rootPackages {
package.modules.filter {
$0.type != .plugin
}.forEach(addFeaturesToModule)
}
}

return try await swiftCommandState.createBuildSystem(
Expand All @@ -173,34 +186,39 @@ extension SwiftPackageCommand {
toolsBuildParameters: toolsBuildParameters,
// command result output goes on stdout
// ie "swift build" should output to stdout
outputStream: TSCBasic.stdoutStream
packageGraphLoader: {
modulesGraph
},
outputStream: TSCBasic.stdoutStream,
observabilityScope: swiftCommandState.observabilityScope
)
}

private func updateManifest(
for target: String,
add feature: SwiftCompilerFeature,
add features: [SwiftCompilerFeature],
using swiftCommandState: SwiftCommandState
) {
typealias SwiftSetting = SwiftPackageCommand.AddSetting.SwiftSetting

let setting: (SwiftSetting, String) = switch feature {
case .upcoming(name: let name, migratable: _, enabledIn: _):
(.upcomingFeature, "\(name)")
case .experimental(name: let name, migratable: _):
(.experimentalFeature, "\(name)")
let settings: [(SwiftSetting, String)] = features.map {
switch $0 {
case .upcoming(name: let name, migratable: _, enabledIn: _):
(.upcomingFeature, "\(name)")
case .experimental(name: let name, migratable: _):
(.experimentalFeature, "\(name)")
}
}

do {
try SwiftPackageCommand.AddSetting.editSwiftSettings(
of: target,
using: swiftCommandState,
[setting]
settings,
verbose: !self.globalOptions.logging.quiet
)
} catch {
print(
"! Couldn't update manifest due to - \(error); Please add '.enable\(feature.upcoming ? "Upcoming" : "Experimental")Feature(\"\(feature.name)\")' to target '\(target)' settings manually."
)
swiftCommandState.observabilityScope.emit(error: "Could not update manifest for '\(target)' (\(error)). Please enable '\(features.map(\.name).joined(separator: ", "))' features manually.")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageModel/Module/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public class Module {
public let others: [AbsolutePath]

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

@_spi(SwiftPMInternal)
public let buildSettingsDescription: [TargetBuildSettingDescription.Setting]
Expand Down
24 changes: 16 additions & 8 deletions Sources/PackageModelSyntax/AddSwiftSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,23 @@ public enum AddSwiftSetting {
throw ManifestEditError.cannotFindTargets
}

guard let targetCall = FunctionCallExprSyntax.findFirst(in: targetArray, matching: {
if let nameArgument = $0.findArgument(labeled: "name"),
let nameLiteral = nameArgument.expression.as(StringLiteralExprSyntax.self),
nameLiteral.representedLiteralValue == target
{
return true
let targetCall = targetArray
.elements
.lazy
.compactMap {
$0.expression.as(FunctionCallExprSyntax.self)
}.first { targetCall in
if let nameArgument = targetCall.findArgument(labeled: "name"),
let nameLiteral = nameArgument.expression.as(StringLiteralExprSyntax.self),
nameLiteral.representedLiteralValue == target
{
return true
}

return false
}
return false
}) else {

guard let targetCall else {
throw ManifestEditError.cannotFindTarget(targetName: target)
}

Expand Down
Loading