Files
sourcekit-lsp/Sources/SwiftSourceKitClientPlugin/ClientPlugin.swift
T
Rintaro Ishizaki 4b2fa3193a Allow injection of a pre-initialized sourcekitd connection
Introduce `SourceKitDCore` as the protocol boundary between dylib
lifecycle management and the high-level `SourceKitD` API. Its single
lifecycle entry point, `initializeService(api:notificationCallback:)`,
receives the already-loaded `sourcekitd_api_functions_t` from
`SourceKitD.init(core:)`.

`SourceKitDCoreImpl` is the standard implementation: `init` opens the
dylib; `initializeService` registers any plugin paths, calls
`api.initialize()`, and wires the notification handler; `deinit` calls
`shutdown()` and closes the handle. Pre-initialized conformances
implement `initializeService` as a no-op.

Wire a `sourcekitdCoreInjector` hook through `Hooks` so an embedding
host can return a pre-initialized `SourceKitDCore` for a given toolchain
path, preventing `sourcekitd_initialize()` from being called a second
time.

Declare `SourceKitDCoreForPlugin` at its use sites so each call site
can express the exact deinit behavior it needs: `dlclose` for handles
acquired via `RTLD_NOLOAD`, and `leak` for externally-owned handles.
2026-05-07 09:58:47 -07:00

86 lines
3.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
public import Csourcekitd
import Foundation
@_spi(SourceKitLSP) import SKLogging
import SourceKitD
import SwiftExtensions
import SwiftSourceKitPluginCommon
/// Legacy plugin initialization logic in which sourcekitd does not inform the plugin about the sourcekitd path it was
/// loaded from.
@_cdecl("sourcekitd_plugin_initialize")
public func sourcekitd_plugin_initialize(_ params: sourcekitd_api_plugin_initialize_params_t) {
LoggingScope.configureDefaultLoggingSubsystem("org.swift.sourcekit-lsp.client-plugin")
logger.fault("sourcekitd_plugin_initialize has been removed in favor of sourcekitd_plugin_initialize_2")
}
@_cdecl("sourcekitd_plugin_initialize_2")
public func sourcekitd_plugin_initialize_2(
_ params: sourcekitd_api_plugin_initialize_params_t,
_ sourcekitdPath: UnsafePointer<CChar>
) {
let pluginPath = URL(fileURLWithPath: String(cString: sourcekitdPath))
if SourceKitD.isPluginLoaded {
// When `DYLD_(FRAMEWORK|LIBRARY)_PATH` is set, `dlopen` will first check if the basename of the provided path is
// within any of its search paths. Thus it's possible that only a single library is loaded for each toolchain,
// rather than a separate like we expect. The paths should be equal in this case, since the client plugin is loaded
// based on the path of `sourcekitd.framework` (and we should only have one for the same reason). Allow this case
// and just avoid re-initializing.
precondition(
SourceKitD.forPlugin.path == pluginPath,
"SourceKitD path does not equal pluginPath, SourceKitD.forPlugin.path = \(SourceKitD.forPlugin.path), pluginPath = \(pluginPath)"
)
return
}
SourceKitD.forPlugin = try! SourceKitD(core: SourceKitDCoreForPlugin(dylibPath: pluginPath))
let sourcekitd = SourceKitD.forPlugin
let customBufferStart = sourcekitd.pluginApi.plugin_initialize_custom_buffer_start(params)
let arrayBuffKind = customBufferStart
sourcekitd.pluginApi.plugin_initialize_register_custom_buffer(
params,
arrayBuffKind,
CompletionResultsArray.arrayFuncs.rawValue
)
}
private final class SourceKitDCoreForPlugin: SourceKitDCore, Sendable {
let dlHandle: DLHandle
let path: URL
init(dylibPath: URL) throws {
#if os(Windows)
let dlopenModes: DLOpenFlags = []
#else
let dlopenModes: DLOpenFlags = [.lazy, .local, .noLoad]
#endif
self.dlHandle = try dlopen(dylibPath.filePath, mode: dlopenModes)
self.path = dylibPath
}
deinit {
try? dlHandle.close()
}
func initializeService(
api: sourcekitd_api_functions_t,
notificationCallback: @escaping @Sendable (sourcekitd_api_response_t) -> Void
) {
// Borrowed handle — sourcekitd is already initialized.
}
}