@@ -59,10 +59,12 @@ public typealias BuildServerTarget = BuildServerProtocol.BuildTarget
5959
6060/// Same as `toolchainRegistry.default`.
6161///
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
6363/// `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+ ] )
6668}
6769
6870fileprivate extension BuildTriple {
@@ -123,7 +125,7 @@ public actor SwiftPMBuildSystem {
123125 @_spi ( Testing) public let toolsBuildParameters : BuildParameters
124126 @_spi ( Testing) public let destinationBuildParameters : BuildParameters
125127 private let fileSystem : FileSystem
126- private let toolchainRegistry : ToolchainRegistry
128+ private let toolchain : SKCore . Toolchain
127129
128130 private let swiftBuildSupportsPrepareForIndexingTask = SwiftExtensions . ThreadSafeBox < Task < Bool , Never > ? > (
129131 initialValue: nil
@@ -184,7 +186,11 @@ public actor SwiftPMBuildSystem {
184186 ) async throws {
185187 self . workspacePath = workspacePath
186188 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
188194 self . experimentalFeatures = experimentalFeatures
189195
190196 guard let packageRoot = findPackageDirectory ( containing: workspacePath, fileSystem) else {
@@ -193,12 +199,12 @@ public actor SwiftPMBuildSystem {
193199
194200 self . projectRoot = try resolveSymlinks ( packageRoot)
195201
196- guard let destinationToolchainBinDir = await getDefaultToolchain ( toolchainRegistry ) ? . swiftc? . parentDirectory else {
202+ guard let destinationToolchainBinDir = toolchain . swiftc? . parentDirectory else {
197203 throw Error . cannotDetermineHostToolchain
198204 }
199205
200206 let swiftSDK = try SwiftSDK . hostSwiftSDK ( AbsolutePath ( destinationToolchainBinDir) )
201- let toolchain = try UserToolchain ( swiftSDK: swiftSDK)
207+ let swiftPMToolchain = try UserToolchain ( swiftSDK: swiftSDK)
202208
203209 var location = try Workspace . Location (
204210 forRootPackage: AbsolutePath ( packageRoot) ,
@@ -217,7 +223,7 @@ public actor SwiftPMBuildSystem {
217223 fileSystem: fileSystem,
218224 location: location,
219225 configuration: configuration,
220- customHostToolchain: toolchain
226+ customHostToolchain: swiftPMToolchain
221227 )
222228
223229 let buildConfiguration : PackageModel . BuildConfiguration
@@ -230,17 +236,21 @@ public actor SwiftPMBuildSystem {
230236
231237 self . toolsBuildParameters = try BuildParameters (
232238 destination: . host,
233- dataPath: location. scratchDirectory. appending ( component: toolchain. targetTriple. platformBuildPathComponent) ,
239+ dataPath: location. scratchDirectory. appending (
240+ component: swiftPMToolchain. targetTriple. platformBuildPathComponent
241+ ) ,
234242 configuration: buildConfiguration,
235- toolchain: toolchain ,
243+ toolchain: swiftPMToolchain ,
236244 flags: buildSetup. flags
237245 )
238246
239247 self . destinationBuildParameters = try BuildParameters (
240248 destination: . target,
241- dataPath: location. scratchDirectory. appending ( component: toolchain. targetTriple. platformBuildPathComponent) ,
249+ dataPath: location. scratchDirectory. appending (
250+ component: swiftPMToolchain. targetTriple. platformBuildPathComponent
251+ ) ,
242252 configuration: buildConfiguration,
243- toolchain: toolchain ,
253+ toolchain: swiftPMToolchain ,
244254 flags: buildSetup. flags
245255 )
246256
@@ -411,7 +421,7 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
411421 #endif
412422 // Fix up compiler arguments that point to a `/Modules` subdirectory if the Swift version in the toolchain is less
413423 // 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 }
415425 guard let toolchainVersion, toolchainVersion < SwiftVersion ( 6 , 0 ) else {
416426 return compileArguments
417427 }
@@ -471,6 +481,10 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
471481 return nil
472482 }
473483
484+ public func toolchain( for uri: DocumentURI , _ language: Language ) async -> SKCore . Toolchain ? {
485+ return toolchain
486+ }
487+
474488 public func configuredTargets( for uri: DocumentURI ) -> [ ConfiguredTarget ] {
475489 guard let url = uri. fileURL, let path = try ? AbsolutePath ( validating: url. path) else {
476490 // We can't determine targets for non-file URIs.
@@ -535,7 +549,7 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
535549 // TODO (indexing): Support preparation of multiple targets at once.
536550 // https://github.com/apple/sourcekit-lsp/issues/1262
537551 for target in targets {
538- try await prepare ( singleTarget: target, logMessageToIndexLog: logMessageToIndexLog)
552+ await orLog ( " Preparing " ) { try await prepare ( singleTarget: target, logMessageToIndexLog: logMessageToIndexLog) }
539553 }
540554 let filesInPreparedTargets = targets. flatMap { self . targets [ $0] ? . buildTarget. sources ?? [ ] }
541555 await fileDependenciesUpdatedDebouncer. scheduleCall ( Set ( filesInPreparedTargets. map ( DocumentURI . init) ) )
@@ -552,16 +566,13 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
552566
553567 // TODO (indexing): Add a proper 'prepare' job in SwiftPM instead of building the target.
554568 // 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- }
559569 guard let swift = toolchain. swift else {
560570 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 "
562572 )
563573 return
564574 }
575+ logger. debug ( " Preparing ' \( target. targetID) ' using \( self . toolchain. identifier) " )
565576 var arguments = [
566577 swift. pathString, " build " ,
567578 " --package-path " , workspacePath. pathString,
0 commit comments