@@ -59,10 +59,12 @@ public typealias BuildServerTarget = BuildServerProtocol.BuildTarget
59
59
60
60
/// Same as `toolchainRegistry.default`.
61
61
///
62
- /// Needed to work around a compiler crash that prevents us from accessing `toolchainRegistry.default ` in
62
+ /// Needed to work around a compiler crash that prevents us from accessing `toolchainRegistry.preferredToolchain ` in
63
63
/// `SwiftPMWorkspace.init`.
64
- private func getDefaultToolchain( _ toolchainRegistry: ToolchainRegistry ) async -> SKCore . Toolchain ? {
65
- return await toolchainRegistry. default
64
+ private func preferredToolchain( _ toolchainRegistry: ToolchainRegistry ) async -> SKCore . Toolchain ? {
65
+ return await toolchainRegistry. preferredToolchain ( containing: [
66
+ \. clang, \. clangd, \. sourcekitd, \. swift, \. swiftc,
67
+ ] )
66
68
}
67
69
68
70
fileprivate extension BuildTriple {
@@ -123,7 +125,7 @@ public actor SwiftPMBuildSystem {
123
125
@_spi ( Testing) public let toolsBuildParameters : BuildParameters
124
126
@_spi ( Testing) public let destinationBuildParameters : BuildParameters
125
127
private let fileSystem : FileSystem
126
- private let toolchainRegistry : ToolchainRegistry
128
+ private let toolchain : SKCore . Toolchain
127
129
128
130
private let swiftBuildSupportsPrepareForIndexingTask = SwiftExtensions . ThreadSafeBox < Task < Bool , Never > ? > (
129
131
initialValue: nil
@@ -184,7 +186,11 @@ public actor SwiftPMBuildSystem {
184
186
) async throws {
185
187
self . workspacePath = workspacePath
186
188
self . fileSystem = fileSystem
187
- self . toolchainRegistry = toolchainRegistry
189
+ guard let toolchain = await preferredToolchain ( toolchainRegistry) else {
190
+ throw Error . cannotDetermineHostToolchain
191
+ }
192
+
193
+ self . toolchain = toolchain
188
194
self . experimentalFeatures = experimentalFeatures
189
195
190
196
guard let packageRoot = findPackageDirectory ( containing: workspacePath, fileSystem) else {
@@ -193,12 +199,12 @@ public actor SwiftPMBuildSystem {
193
199
194
200
self . projectRoot = try resolveSymlinks ( packageRoot)
195
201
196
- guard let destinationToolchainBinDir = await getDefaultToolchain ( toolchainRegistry ) ? . swiftc? . parentDirectory else {
202
+ guard let destinationToolchainBinDir = toolchain . swiftc? . parentDirectory else {
197
203
throw Error . cannotDetermineHostToolchain
198
204
}
199
205
200
206
let swiftSDK = try SwiftSDK . hostSwiftSDK ( AbsolutePath ( destinationToolchainBinDir) )
201
- let toolchain = try UserToolchain ( swiftSDK: swiftSDK)
207
+ let swiftPMToolchain = try UserToolchain ( swiftSDK: swiftSDK)
202
208
203
209
var location = try Workspace . Location (
204
210
forRootPackage: AbsolutePath ( packageRoot) ,
@@ -217,7 +223,7 @@ public actor SwiftPMBuildSystem {
217
223
fileSystem: fileSystem,
218
224
location: location,
219
225
configuration: configuration,
220
- customHostToolchain: toolchain
226
+ customHostToolchain: swiftPMToolchain
221
227
)
222
228
223
229
let buildConfiguration : PackageModel . BuildConfiguration
@@ -230,17 +236,21 @@ public actor SwiftPMBuildSystem {
230
236
231
237
self . toolsBuildParameters = try BuildParameters (
232
238
destination: . host,
233
- dataPath: location. scratchDirectory. appending ( component: toolchain. targetTriple. platformBuildPathComponent) ,
239
+ dataPath: location. scratchDirectory. appending (
240
+ component: swiftPMToolchain. targetTriple. platformBuildPathComponent
241
+ ) ,
234
242
configuration: buildConfiguration,
235
- toolchain: toolchain ,
243
+ toolchain: swiftPMToolchain ,
236
244
flags: buildSetup. flags
237
245
)
238
246
239
247
self . destinationBuildParameters = try BuildParameters (
240
248
destination: . target,
241
- dataPath: location. scratchDirectory. appending ( component: toolchain. targetTriple. platformBuildPathComponent) ,
249
+ dataPath: location. scratchDirectory. appending (
250
+ component: swiftPMToolchain. targetTriple. platformBuildPathComponent
251
+ ) ,
242
252
configuration: buildConfiguration,
243
- toolchain: toolchain ,
253
+ toolchain: swiftPMToolchain ,
244
254
flags: buildSetup. flags
245
255
)
246
256
@@ -411,7 +421,7 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
411
421
#endif
412
422
// Fix up compiler arguments that point to a `/Modules` subdirectory if the Swift version in the toolchain is less
413
423
// than 6.0 because it places the modules one level higher up.
414
- let toolchainVersion = await orLog ( " Getting Swift version " ) { try await toolchainRegistry . default ? . swiftVersion }
424
+ let toolchainVersion = await orLog ( " Getting Swift version " ) { try await toolchain . swiftVersion }
415
425
guard let toolchainVersion, toolchainVersion < SwiftVersion ( 6 , 0 ) else {
416
426
return compileArguments
417
427
}
@@ -471,6 +481,10 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
471
481
return nil
472
482
}
473
483
484
+ public func toolchain( for uri: DocumentURI , _ language: Language ) async -> SKCore . Toolchain ? {
485
+ return toolchain
486
+ }
487
+
474
488
public func configuredTargets( for uri: DocumentURI ) -> [ ConfiguredTarget ] {
475
489
guard let url = uri. fileURL, let path = try ? AbsolutePath ( validating: url. path) else {
476
490
// We can't determine targets for non-file URIs.
@@ -535,7 +549,7 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
535
549
// TODO (indexing): Support preparation of multiple targets at once.
536
550
// https://github.com/apple/sourcekit-lsp/issues/1262
537
551
for target in targets {
538
- try await prepare ( singleTarget: target, logMessageToIndexLog: logMessageToIndexLog)
552
+ await orLog ( " Preparing " ) { try await prepare ( singleTarget: target, logMessageToIndexLog: logMessageToIndexLog) }
539
553
}
540
554
let filesInPreparedTargets = targets. flatMap { self . targets [ $0] ? . buildTarget. sources ?? [ ] }
541
555
await fileDependenciesUpdatedDebouncer. scheduleCall ( Set ( filesInPreparedTargets. map ( DocumentURI . init) ) )
@@ -552,16 +566,13 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
552
566
553
567
// TODO (indexing): Add a proper 'prepare' job in SwiftPM instead of building the target.
554
568
// https://github.com/apple/sourcekit-lsp/issues/1254
555
- guard let toolchain = await toolchainRegistry. default else {
556
- logger. error ( " Not preparing because not toolchain exists " )
557
- return
558
- }
559
569
guard let swift = toolchain. swift else {
560
570
logger. error (
561
- " Not preparing because toolchain at \( toolchain. identifier) does not contain a Swift compiler "
571
+ " Not preparing because toolchain at \( self . toolchain. identifier) does not contain a Swift compiler "
562
572
)
563
573
return
564
574
}
575
+ logger. debug ( " Preparing ' \( target. targetID) ' using \( self . toolchain. identifier) " )
565
576
var arguments = [
566
577
swift. pathString, " build " ,
567
578
" --package-path " , workspacePath. pathString,
0 commit comments