@@ -51,6 +51,16 @@ package typealias SwiftBuildTarget = SourceKitLSPAPI.BuildTarget
51
51
/// A build target in `BuildServerProtocol`
52
52
package typealias BuildServerTarget = BuildServerProtocol . BuildTarget
53
53
54
+ fileprivate extension Basics . Diagnostic . Severity {
55
+ var asLogLevel : LogLevel {
56
+ switch self {
57
+ case . error, . warning: return . default
58
+ case . info: return . info
59
+ case . debug: return . debug
60
+ }
61
+ }
62
+ }
63
+
54
64
fileprivate extension BuildDestination {
55
65
/// A string that can be used to identify the build triple in a `BuildTargetIdentifier`.
56
66
///
@@ -132,14 +142,6 @@ fileprivate extension TSCBasic.AbsolutePath {
132
142
133
143
fileprivate let preparationTaskID : AtomicUInt32 = AtomicUInt32 ( initialValue: 0 )
134
144
135
- package struct BuildSystemTestHooks : Sendable {
136
- package var swiftPMTestHooks : SwiftPMTestHooks
137
-
138
- package init ( swiftPMTestHooks: SwiftPMTestHooks = SwiftPMTestHooks ( ) ) {
139
- self . swiftPMTestHooks = swiftPMTestHooks
140
- }
141
- }
142
-
143
145
package struct SwiftPMTestHooks : Sendable {
144
146
package var reloadPackageDidStart : ( @Sendable ( ) async -> Void ) ?
145
147
package var reloadPackageDidFinish : ( @Sendable ( ) async -> Void ) ?
@@ -155,14 +157,11 @@ package struct SwiftPMTestHooks: Sendable {
155
157
156
158
/// Swift Package Manager build system and workspace support.
157
159
///
158
- /// This class implements the `BuildSystem ` interface to provide the build settings for a Swift
160
+ /// This class implements the `BuiltInBuildSystem ` interface to provide the build settings for a Swift
159
161
/// Package Manager (SwiftPM) package. The settings are determined by loading the Package.swift
160
162
/// manifest using `libSwiftPM` and constructing a build plan using the default (debug) parameters.
161
- package actor SwiftPMBuildSystem {
163
+ package actor SwiftPMBuildSystem : BuiltInBuildSystem {
162
164
package enum Error : Swift . Error {
163
- /// Could not find a manifest (Package.swift file). This is not a package.
164
- case noManifest( workspacePath: TSCAbsolutePath )
165
-
166
165
/// Could not determine an appropriate toolchain for swiftpm to use for manifest loading.
167
166
case cannotDetermineHostToolchain
168
167
}
@@ -192,9 +191,8 @@ package actor SwiftPMBuildSystem {
192
191
package let toolsBuildParameters : BuildParameters
193
192
package let destinationBuildParameters : BuildParameters
194
193
195
- private let fileSystem : FileSystem
196
194
private let toolchain : Toolchain
197
- private let workspace : Workspace
195
+ private let swiftPMWorkspace : Workspace
198
196
199
197
/// A `ObservabilitySystem` from `SwiftPM` that logs.
200
198
private let observabilitySystem = ObservabilitySystem ( { scope, diagnostic in
@@ -248,14 +246,12 @@ package actor SwiftPMBuildSystem {
248
246
package init (
249
247
projectRoot: TSCAbsolutePath ,
250
248
toolchainRegistry: ToolchainRegistry ,
251
- fileSystem: FileSystem = localFileSystem,
252
249
options: SourceKitLSPOptions ,
253
250
connectionToSourceKitLSP: any Connection ,
254
251
testHooks: SwiftPMTestHooks
255
252
) async throws {
256
253
self . projectRoot = projectRoot
257
254
self . options = options
258
- self . fileSystem = fileSystem
259
255
let toolchain = await toolchainRegistry. preferredToolchain ( containing: [
260
256
\. clang, \. clangd, \. sourcekitd, \. swift, \. swiftc,
261
257
] )
@@ -280,22 +276,22 @@ package actor SwiftPMBuildSystem {
280
276
customCompileTriple: options. swiftPMOrDefault. triple. map { try Triple ( $0) } ,
281
277
swiftSDKSelector: options. swiftPMOrDefault. swiftSDK,
282
278
store: SwiftSDKBundleStore (
283
- swiftSDKsDirectory: fileSystem . getSharedSwiftSDKsDirectory (
279
+ swiftSDKsDirectory: localFileSystem . getSharedSwiftSDKsDirectory (
284
280
explicitDirectory: options. swiftPMOrDefault. swiftSDKsDirectory. map { try AbsolutePath ( validating: $0) }
285
281
) ,
286
- fileSystem: fileSystem ,
282
+ fileSystem: localFileSystem ,
287
283
observabilityScope: observabilitySystem. topScope,
288
284
outputHandler: { _ in }
289
285
) ,
290
286
observabilityScope: observabilitySystem. topScope,
291
- fileSystem: fileSystem
287
+ fileSystem: localFileSystem
292
288
)
293
289
294
290
let destinationSwiftPMToolchain = try UserToolchain ( swiftSDK: destinationSDK)
295
291
296
292
var location = try Workspace . Location (
297
293
forRootPackage: AbsolutePath ( projectRoot) ,
298
- fileSystem: fileSystem
294
+ fileSystem: localFileSystem
299
295
)
300
296
if options. backgroundIndexingOrDefault {
301
297
location. scratchDirectory = AbsolutePath ( projectRoot. appending ( component: " .index-build " ) )
@@ -308,8 +304,8 @@ package actor SwiftPMBuildSystem {
308
304
var configuration = WorkspaceConfiguration . default
309
305
configuration. skipDependenciesUpdates = true
310
306
311
- self . workspace = try Workspace (
312
- fileSystem: fileSystem ,
307
+ self . swiftPMWorkspace = try Workspace (
308
+ fileSystem: localFileSystem ,
313
309
location: location,
314
310
configuration: configuration,
315
311
customHostToolchain: hostSwiftPMToolchain,
@@ -367,35 +363,6 @@ package actor SwiftPMBuildSystem {
367
363
}
368
364
}
369
365
370
- /// Creates a build system using the Swift Package Manager, if this workspace is a package.
371
- ///
372
- /// - Returns: nil if `workspacePath` is not part of a package or there is an error.
373
- package init ? (
374
- projectRoot: TSCBasic . AbsolutePath ,
375
- toolchainRegistry: ToolchainRegistry ,
376
- options: SourceKitLSPOptions ,
377
- connectionToSourceKitLSP: any Connection ,
378
- testHooks: SwiftPMTestHooks
379
- ) async {
380
- do {
381
- try await self . init (
382
- projectRoot: projectRoot,
383
- toolchainRegistry: toolchainRegistry,
384
- fileSystem: localFileSystem,
385
- options: options,
386
- connectionToSourceKitLSP: connectionToSourceKitLSP,
387
- testHooks: testHooks
388
- )
389
- } catch Error . noManifest {
390
- return nil
391
- } catch {
392
- logger. error ( " Failed to create SwiftPMWorkspace at \( projectRoot. pathString) : \( error. forLogging) " )
393
- return nil
394
- }
395
- }
396
- }
397
-
398
- extension SwiftPMBuildSystem {
399
366
/// (Re-)load the package settings by parsing the manifest and resolving all the targets and
400
367
/// dependencies.
401
368
///
@@ -416,7 +383,7 @@ extension SwiftPMBuildSystem {
416
383
}
417
384
}
418
385
419
- let modulesGraph = try await self . workspace . loadPackageGraph (
386
+ let modulesGraph = try await self . swiftPMWorkspace . loadPackageGraph (
420
387
rootInput: PackageGraphRootInput ( packages: [ AbsolutePath ( projectRoot) ] ) ,
421
388
forceResolvedVersions: !isForIndexBuild,
422
389
observabilityScope: observabilitySystem. topScope
@@ -427,7 +394,7 @@ extension SwiftPMBuildSystem {
427
394
toolsBuildParameters: toolsBuildParameters,
428
395
graph: modulesGraph,
429
396
disableSandbox: options. swiftPMOrDefault. disableSandbox ?? false ,
430
- fileSystem: fileSystem ,
397
+ fileSystem: localFileSystem ,
431
398
observabilityScope: observabilitySystem. topScope
432
399
)
433
400
let buildDescription = BuildDescription ( buildPlan: plan)
@@ -461,16 +428,7 @@ extension SwiftPMBuildSystem {
461
428
462
429
connectionToSourceKitLSP. send ( OnBuildTargetDidChangeNotification ( changes: nil ) )
463
430
}
464
- }
465
-
466
- fileprivate struct NonFileURIError : Error , CustomStringConvertible {
467
- let uri : DocumentURI
468
- var description : String {
469
- " Trying to get build settings for non-file URI: \( uri) "
470
- }
471
- }
472
431
473
- extension SwiftPMBuildSystem : BuildSystemIntegration . BuiltInBuildSystem {
474
432
package nonisolated var supportsPreparation : Bool { true }
475
433
476
434
package var buildPath : TSCAbsolutePath {
@@ -490,6 +448,13 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
490
448
/// account for differences in the SwiftPM versions being linked into SwiftPM and being installed in the toolchain.
491
449
private func compilerArguments( for file: DocumentURI , in buildTarget: any SwiftBuildTarget ) async throws -> [ String ] {
492
450
guard let fileURL = file. fileURL else {
451
+ struct NonFileURIError : Swift . Error , CustomStringConvertible {
452
+ let uri : DocumentURI
453
+ var description : String {
454
+ " Trying to get build settings for non-file URI: \( uri) "
455
+ }
456
+ }
457
+
493
458
throw NonFileURIError ( uri: file)
494
459
}
495
460
let compileArguments = try buildTarget. compileArguments ( for: fileURL)
@@ -504,7 +469,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
504
469
return compileArguments
505
470
}
506
471
return compileArguments. map { argument in
507
- if argument. hasSuffix ( " /Modules " ) , argument. contains ( self . workspace . location. scratchDirectory. pathString) {
472
+ if argument. hasSuffix ( " /Modules " ) , argument. contains ( self . swiftPMWorkspace . location. scratchDirectory. pathString) {
508
473
return String ( argument. dropLast ( 8 ) )
509
474
}
510
475
return argument
@@ -596,7 +561,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
596
561
}
597
562
598
563
guard let swiftPMTarget = self . swiftPMTargets [ request. target] else {
599
- logger. fault ( " Did not find target \( request. target. forLogging) " )
564
+ logger. error ( " Did not find target \( request. target. forLogging) " )
600
565
return nil
601
566
}
602
567
@@ -642,8 +607,6 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
642
607
if path. basename == " Package.swift "
643
608
&& projectRoot == ( try ? TSCBasic . resolveSymlinks ( TSCBasic . AbsolutePath ( path. parentDirectory) ) )
644
609
{
645
- // We use an empty target name to represent the package manifest since an empty target name is not valid for any
646
- // user-defined target.
647
610
return [ BuildTargetIdentifier . forPackageManifest]
648
611
}
649
612
@@ -675,7 +638,6 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
675
638
return
676
639
}
677
640
678
- // TODO: Add a proper 'prepare' job in SwiftPM instead of building the target. (https://github.com/swiftlang/sourcekit-lsp/issues/1254)
679
641
guard let swift = toolchain. swift else {
680
642
logger. error (
681
643
" Not preparing because toolchain at \( self . toolchain. identifier) does not contain a Swift compiler "
@@ -686,7 +648,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
686
648
var arguments = [
687
649
swift. pathString, " build " ,
688
650
" --package-path " , projectRoot. pathString,
689
- " --scratch-path " , self . workspace . location. scratchDirectory. pathString,
651
+ " --scratch-path " , self . swiftPMWorkspace . location. scratchDirectory. pathString,
690
652
" --disable-index-store " ,
691
653
" --target " , try target. targetProperties. target,
692
654
]
@@ -810,17 +772,7 @@ extension SwiftPMBuildSystem: BuildSystemIntegration.BuiltInBuildSystem {
810
772
811
773
/// Retrieve settings for a package manifest (Package.swift).
812
774
private func settings( forPackageManifest path: AbsolutePath ) throws -> TextDocumentSourceKitOptionsResponse ? {
813
- let compilerArgs = workspace . interpreterFlags ( for: path. parentDirectory) + [ path. pathString]
775
+ let compilerArgs = swiftPMWorkspace . interpreterFlags ( for: path. parentDirectory) + [ path. pathString]
814
776
return TextDocumentSourceKitOptionsResponse ( compilerArguments: compilerArgs)
815
777
}
816
778
}
817
-
818
- extension Basics . Diagnostic . Severity {
819
- var asLogLevel : LogLevel {
820
- switch self {
821
- case . error, . warning: return . default
822
- case . info: return . info
823
- case . debug: return . debug
824
- }
825
- }
826
- }
0 commit comments