@@ -90,43 +90,66 @@ extension SwiftPackageCommand {
90
90
features. append ( feature)
91
91
}
92
92
93
- var targets = self . options. targets
94
-
95
93
let buildSystem = try await createBuildSystem (
96
94
swiftCommandState,
97
- targets: targets,
95
+ targets: self . options . targets,
98
96
features: features
99
97
)
100
98
101
- // Next, let's build all of the individual targets or the
102
- // whole project to get diagnostic files.
99
+ // Compute the set of targets to migrate.
100
+ let targetsToMigrate : Set < String >
101
+ if self . options. targets. isEmpty {
102
+ let graph = try await buildSystem. getPackageGraph ( )
103
+ targetsToMigrate = Set (
104
+ graph. rootPackages. lazy. map { package in
105
+ package . manifest. targets. lazy. filter { target in
106
+ // FIXME: Plugin target init does not have Swift settings.
107
+ // Exclude them from migration.
108
+ target. type != . plugin
109
+ } . map ( \. name)
110
+ } . joined ( )
111
+ )
112
+ } else {
113
+ targetsToMigrate = Set ( self . options. targets. elements)
114
+ }
115
+
116
+ // Next, let's build the requested targets or, if none were given,
117
+ // the whole project to get diagnostic files.
103
118
print ( " > Starting the build " )
104
- var diagnosticsPaths : [ String : [ AbsolutePath ] ] = [ : ]
105
- if !targets. isEmpty {
106
- for target in targets {
107
- let buildResult = try await buildSystem. build ( subset: . target( target) )
108
- diagnosticsPaths. merge ( try buildResult. serializedDiagnosticPathsByTargetName. get ( ) , uniquingKeysWith: { $0 + $1 } )
119
+ var diagnosticFiles : [ [ AbsolutePath ] ] = [ ]
120
+ if self . options. targets. isEmpty {
121
+ // No targets were requested. Build everything.
122
+ let buildResult = try await buildSystem. build ( subset: . allIncludingTests)
123
+ for (target, files) in try buildResult. serializedDiagnosticPathsByTargetName. get ( ) {
124
+ if targetsToMigrate. contains ( target) {
125
+ diagnosticFiles. append ( files)
126
+ }
109
127
}
110
128
} else {
111
- diagnosticsPaths = try await buildSystem. build ( subset: . allIncludingTests) . serializedDiagnosticPathsByTargetName. get ( )
129
+ // Build only requested targets.
130
+ for target in self . options. targets. elements {
131
+ // TODO: It would be nice if BuildSubset had a case for an
132
+ // array of targets so that we can move the build out of
133
+ // this enclosing if/else and avoid repetition.
134
+ let buildResult = try await buildSystem. build ( subset: . target( target) )
135
+ for (target, files) in try buildResult. serializedDiagnosticPathsByTargetName. get ( ) {
136
+ if targetsToMigrate. contains ( target) {
137
+ diagnosticFiles. append ( files)
138
+ }
139
+ }
140
+ }
112
141
}
113
142
114
- var summary = SwiftFixIt . Summary ( numberOfFixItsApplied: 0 , numberOfFilesChanged: 0 )
115
- let graph = try await buildSystem. getPackageGraph ( )
116
- if targets. isEmpty {
117
- targets = OrderedSet ( graph. rootPackages. flatMap { $0. manifest. targets. filter { $0. type != . plugin } . map ( \. name) } )
118
- }
119
143
print ( " > Applying fix-its " )
144
+ var summary = SwiftFixIt . Summary ( numberOfFixItsApplied: 0 , numberOfFilesChanged: 0 )
120
145
let fixItDuration = try ContinuousClock ( ) . measure {
121
- for target in targets {
122
- let fixit = try SwiftFixIt (
123
- diagnosticFiles: Array ( diagnosticsPaths [ target] ?? [ ] ) ,
124
- categories: Set ( features. flatMap ( \. categories) ) ,
125
- excludedSourceDirectories: [ swiftCommandState. scratchDirectory] ,
126
- fileSystem: swiftCommandState. fileSystem
127
- )
128
- summary += try fixit. applyFixIts ( )
129
- }
146
+ let applier = try SwiftFixIt (
147
+ diagnosticFiles: diagnosticFiles. joined ( ) ,
148
+ categories: Set ( features. flatMap ( \. categories) ) ,
149
+ excludedSourceDirectories: [ swiftCommandState. scratchDirectory] ,
150
+ fileSystem: swiftCommandState. fileSystem
151
+ )
152
+ summary = try applier. applyFixIts ( )
130
153
}
131
154
132
155
// Report the changes.
@@ -154,9 +177,8 @@ extension SwiftPackageCommand {
154
177
155
178
// Once the fix-its were applied, it's time to update the
156
179
// manifest with newly adopted feature settings.
157
-
158
180
print ( " > Updating manifest " )
159
- for target in targets {
181
+ for target in targetsToMigrate {
160
182
swiftCommandState. observabilityScope. emit ( debug: " Adding feature(s) to ' \( target) ' " )
161
183
try self . updateManifest (
162
184
for: target,
0 commit comments