Skip to content

Commit 7bf63be

Browse files
committed
Load the SourceKit plugins when launching SourceKit-LSP
Previously, we were only loading the SourceKit plugins during tests. Make sure we find them in the toolchain as well.
1 parent bb699c9 commit 7bf63be

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ package actor SwiftLanguageService: LanguageService, Sendable {
231231
clientPlugin: URL(fileURLWithPath: clientPlugin),
232232
servicePlugin: URL(fileURLWithPath: servicePlugin)
233233
)
234+
} else if let clientPlugin = toolchain.sourceKitClientPlugin, let servicePlugin = toolchain.sourceKitServicePlugin {
235+
pluginPaths = PluginPaths(clientPlugin: clientPlugin, servicePlugin: servicePlugin)
234236
} else {
237+
logger.fault("Failed to find SourceKit plugin for toolchain at \(toolchain.path?.path ?? "nil")")
235238
pluginPaths = nil
236239
}
237240
self.sourcekitd = try await DynamicallyLoadedSourceKitD.getOrCreate(

Sources/ToolchainRegistry/Toolchain.swift

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public final class Toolchain: Sendable {
9999
/// The path to the Swift language server if available.
100100
package let sourcekitd: URL?
101101

102+
/// The path to the SourceKit client plugin if available.
103+
package let sourceKitClientPlugin: URL?
104+
105+
/// The path to the SourceKit plugin if available.
106+
package let sourceKitServicePlugin: URL?
107+
102108
/// The path to the indexstore library if available.
103109
package let libIndexStore: URL?
104110

@@ -153,6 +159,8 @@ public final class Toolchain: Sendable {
153159
swiftFormat: URL? = nil,
154160
clangd: URL? = nil,
155161
sourcekitd: URL? = nil,
162+
sourceKitClientPlugin: URL? = nil,
163+
sourceKitServicePlugin: URL? = nil,
156164
libIndexStore: URL? = nil
157165
) {
158166
self.identifier = identifier
@@ -164,6 +172,8 @@ public final class Toolchain: Sendable {
164172
self.swiftFormat = swiftFormat
165173
self.clangd = clangd
166174
self.sourcekitd = sourcekitd
175+
self.sourceKitClientPlugin = sourceKitClientPlugin
176+
self.sourceKitServicePlugin = sourceKitServicePlugin
167177
self.libIndexStore = libIndexStore
168178
}
169179

@@ -225,6 +235,8 @@ extension Toolchain {
225235
var swiftc: URL? = nil
226236
var swiftFormat: URL? = nil
227237
var sourcekitd: URL? = nil
238+
var sourceKitClientPlugin: URL? = nil
239+
var sourceKitServicePlugin: URL? = nil
228240
var libIndexStore: URL? = nil
229241

230242
if let (infoPlist, xctoolchainPath) = containingXCToolchain(path) {
@@ -282,34 +294,53 @@ extension Toolchain {
282294
}
283295

284296
// If 'currentPlatform' is nil it's most likely an unknown linux flavor.
285-
let dylibExt: String
297+
let dylibExtension: String
286298
if let dynamicLibraryExtension = Platform.current?.dynamicLibraryExtension {
287-
dylibExt = dynamicLibraryExtension
299+
dylibExtension = dynamicLibraryExtension
288300
} else {
289301
logger.fault("Could not determine host OS. Falling back to using '.so' as dynamic library extension")
290-
dylibExt = ".so"
302+
dylibExtension = ".so"
291303
}
292304

293-
let sourcekitdPath = libPath.appendingPathComponent("sourcekitd.framework").appendingPathComponent("sourcekitd")
294-
if FileManager.default.isFile(at: sourcekitdPath) {
295-
sourcekitd = sourcekitdPath
296-
foundAny = true
297-
} else {
305+
func findDylib(named name: String, searchFramework: Bool = false) -> URL? {
306+
let frameworkPath = libPath.appendingPathComponent("\(name).framework").appendingPathComponent(name)
307+
if FileManager.default.isFile(at: frameworkPath) {
308+
return frameworkPath
309+
}
310+
let libSearchPath = libPath.appendingPathComponent("lib\(name)\(dylibExtension)")
311+
if FileManager.default.isFile(at: libSearchPath) {
312+
return libSearchPath
313+
}
298314
#if os(Windows)
299-
let sourcekitdPath = binPath.appendingPathComponent("sourcekitdInProc\(dylibExt)")
300-
#else
301-
let sourcekitdPath = libPath.appendingPathComponent("libsourcekitdInProc\(dylibExt)")
302-
#endif
303-
if FileManager.default.isFile(at: sourcekitdPath) {
304-
sourcekitd = sourcekitdPath
305-
foundAny = true
315+
let binSearchPath = binPath.appendingPathComponent("\(name)\(dylibExtension)")
316+
if FileManager.default.isFile(at: binSearchPath) {
317+
return binSearchPath
306318
}
319+
#endif
320+
return nil
321+
}
322+
323+
if let sourcekitdPath = findDylib(named: "sourcekitd", searchFramework: true)
324+
?? findDylib(named: "sourcekitdInProc")
325+
{
326+
sourcekitd = sourcekitdPath
327+
foundAny = true
328+
}
329+
330+
if let clientPluginPath = findDylib(named: "SwiftSourceKitClientPlugin", searchFramework: true) {
331+
sourceKitClientPlugin = clientPluginPath
332+
foundAny = true
333+
}
334+
335+
if let servicePluginPath = findDylib(named: "SwiftSourceKitPlugin", searchFramework: true) {
336+
sourceKitServicePlugin = servicePluginPath
337+
foundAny = true
307338
}
308339

309340
#if os(Windows)
310-
let libIndexStorePath = binPath.appendingPathComponent("libIndexStore\(dylibExt)")
341+
let libIndexStorePath = binPath.appendingPathComponent("libIndexStore\(dylibExtension)")
311342
#else
312-
let libIndexStorePath = libPath.appendingPathComponent("libIndexStore\(dylibExt)")
343+
let libIndexStorePath = libPath.appendingPathComponent("libIndexStore\(dylibExtension)")
313344
#endif
314345
if FileManager.default.isFile(at: libIndexStorePath) {
315346
libIndexStore = libIndexStorePath
@@ -334,6 +365,8 @@ extension Toolchain {
334365
swiftFormat: swiftFormat,
335366
clangd: clangd,
336367
sourcekitd: sourcekitd,
368+
sourceKitClientPlugin: sourceKitClientPlugin,
369+
sourceKitServicePlugin: sourceKitServicePlugin,
337370
libIndexStore: libIndexStore
338371
)
339372
}

0 commit comments

Comments
 (0)