@@ -90,44 +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.
103
- print ( " > Starting the build " )
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
+ }
104
115
105
- var diagnosticsPaths : [ String : [ AbsolutePath ] ] = [ : ]
106
- if !targets. isEmpty {
107
- for target in targets {
108
- let buildResult = try await buildSystem. build ( subset: . target( target) , buildOutputs: [ ] )
109
- diagnosticsPaths. merge ( try buildResult. serializedDiagnosticPathsByTargetName. get ( ) , uniquingKeysWith: { $0 + $1 } )
116
+ // Next, let's build the requested targets or, if none were given,
117
+ // the whole project to get diagnostic files.
118
+ print ( " > Starting the build " )
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, buildOutputs: [ ] )
123
+ for (target, files) in try buildResult. serializedDiagnosticPathsByTargetName. get ( ) {
124
+ if targetsToMigrate. contains ( target) {
125
+ diagnosticFiles. append ( files)
126
+ }
110
127
}
111
128
} else {
112
- diagnosticsPaths = try await buildSystem. build ( subset: . allIncludingTests, buildOutputs: [ ] ) . 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) , buildOutputs: [ ] )
135
+ for (target, files) in try buildResult. serializedDiagnosticPathsByTargetName. get ( ) {
136
+ if targetsToMigrate. contains ( target) {
137
+ diagnosticFiles. append ( files)
138
+ }
139
+ }
140
+ }
113
141
}
114
142
115
- var summary = SwiftFixIt . Summary ( numberOfFixItsApplied: 0 , numberOfFilesChanged: 0 )
116
- let graph = try await buildSystem. getPackageGraph ( )
117
- if targets. isEmpty {
118
- targets = OrderedSet ( graph. rootPackages. flatMap { $0. manifest. targets. filter { $0. type != . plugin } . map ( \. name) } )
119
- }
120
143
print ( " > Applying fix-its " )
144
+ var summary = SwiftFixIt . Summary ( numberOfFixItsApplied: 0 , numberOfFilesChanged: 0 )
121
145
let fixItDuration = try ContinuousClock ( ) . measure {
122
- for target in targets {
123
- let fixit = try SwiftFixIt (
124
- diagnosticFiles: Array ( diagnosticsPaths [ target] ?? [ ] ) ,
125
- categories: Set ( features. flatMap ( \. categories) ) ,
126
- excludedSourceDirectories: [ swiftCommandState. scratchDirectory] ,
127
- fileSystem: swiftCommandState. fileSystem
128
- )
129
- summary += try fixit. applyFixIts ( )
130
- }
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 ( )
131
153
}
132
154
133
155
// Report the changes.
@@ -155,9 +177,8 @@ extension SwiftPackageCommand {
155
177
156
178
// Once the fix-its were applied, it's time to update the
157
179
// manifest with newly adopted feature settings.
158
-
159
180
print ( " > Updating manifest " )
160
- for target in targets {
181
+ for target in targetsToMigrate {
161
182
swiftCommandState. observabilityScope. emit ( debug: " Adding feature(s) to ' \( target) ' " )
162
183
try self . updateManifest (
163
184
for: target,
0 commit comments