Skip to content

Commit b33a42f

Browse files
committed
When using SOURCEKIT_LSP_TEST_PLUGIN_PATHS=RELATIVE_TO_SOURCEKITD infer plugin paths from default testing toolchain
Otherwise, we infer the SourceKit plugin paths from the toolchain when creating a `SourceKitLSPServer` during testing because we don’t override the plugin paths in the SourceKitLSPOptions. But when running `SourceKitDTests`, we pass `pluginPaths: nil`, which would not load any plugins. If both of the tests run in the same process, this causes a fault to get logged because sourcekitd can only loaded once per process and we can’t modify which plugins are loaded after the fact.
1 parent bd55793 commit b33a42f

File tree

10 files changed

+43
-27
lines changed

10 files changed

+43
-27
lines changed

Sources/SKTestSupport/IndexedSingleSwiftFileTestProject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ package struct IndexedSingleSwiftFileTestProject {
147147
}
148148

149149
// Create the test client
150-
var options = SourceKitLSPOptions.testDefault()
150+
var options = try await SourceKitLSPOptions.testDefault()
151151
options.indexOrDefault = SourceKitLSPOptions.IndexOptions(
152152
indexStorePath: try indexURL.filePath,
153153
indexDatabasePath: try indexDBURL.filePath

Sources/SKTestSupport/MultiFileTestProject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ package class MultiFileTestProject {
138138
},
139139
initializationOptions: LSPAny? = nil,
140140
capabilities: ClientCapabilities = ClientCapabilities(),
141-
options: SourceKitLSPOptions = .testDefault(),
141+
options: SourceKitLSPOptions? = nil,
142142
toolchainRegistry: ToolchainRegistry = .forTesting,
143143
hooks: Hooks = Hooks(),
144144
enableBackgroundIndexing: Bool = false,

Sources/SKTestSupport/PluginPaths.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14+
import ToolchainRegistry
1415

1516
#if compiler(>=6)
1617
package import SourceKitD
@@ -109,8 +110,8 @@ private func pluginPaths(relativeTo base: URL) -> PluginPaths? {
109110

110111
/// Returns the paths from which the SourceKit plugins should be loaded or throws an error if the plugins cannot be
111112
/// found.
112-
package var sourceKitPluginPaths: PluginPaths? {
113-
get throws {
113+
package var sourceKitPluginPaths: PluginPaths {
114+
get async throws {
114115
struct PluginLoadingError: Error, CustomStringConvertible {
115116
let searchBase: URL
116117
var description: String {
@@ -129,7 +130,12 @@ package var sourceKitPluginPaths: PluginPaths? {
129130
var base: URL
130131
if let pluginPaths = ProcessInfo.processInfo.environment["SOURCEKIT_LSP_TEST_PLUGIN_PATHS"] {
131132
if pluginPaths == "RELATIVE_TO_SOURCEKITD" {
132-
return nil
133+
let toolchain = await ToolchainRegistry.forTesting.default
134+
guard let clientPlugin = toolchain?.sourceKitClientPlugin, let servicePlugin = toolchain?.sourceKitServicePlugin
135+
else {
136+
throw PluginLoadingError(searchBase: URL(string: "relative-to-sourcekitd://")!)
137+
}
138+
return PluginPaths(clientPlugin: clientPlugin, servicePlugin: servicePlugin)
133139
} else {
134140
base = URL(fileURLWithPath: pluginPaths)
135141
}

Sources/SKTestSupport/SwiftPMTestProject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ package class SwiftPMTestProject: MultiFileTestProject {
196196
},
197197
initializationOptions: LSPAny? = nil,
198198
capabilities: ClientCapabilities = ClientCapabilities(),
199-
options: SourceKitLSPOptions = .testDefault(),
199+
options: SourceKitLSPOptions? = nil,
200200
hooks: Hooks = Hooks(),
201201
enableBackgroundIndexing: Bool = false,
202202
usePullDiagnostics: Bool = true,

Sources/SKTestSupport/TestSourceKitLSPClient.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ extension SourceKitLSPOptions {
3636
package static func testDefault(
3737
backgroundIndexing: Bool = true,
3838
experimentalFeatures: Set<ExperimentalFeature>? = nil
39-
) -> SourceKitLSPOptions {
39+
) async throws -> SourceKitLSPOptions {
40+
let pluginPaths = try await sourceKitPluginPaths
4041
return SourceKitLSPOptions(
4142
sourcekitd: SourceKitDOptions(
42-
clientPlugin: try! sourceKitPluginPaths?.clientPlugin.filePath,
43-
servicePlugin: try! sourceKitPluginPaths?.servicePlugin.filePath
43+
clientPlugin: try pluginPaths.clientPlugin.filePath,
44+
servicePlugin: try pluginPaths.servicePlugin.filePath
4445
),
4546
backgroundIndexing: backgroundIndexing,
4647
experimentalFeatures: experimentalFeatures,
@@ -137,7 +138,7 @@ package final class TestSourceKitLSPClient: MessageHandler, Sendable {
137138
/// This allows e.g. a `IndexedSingleSwiftFileTestProject` to delete its temporary files when they are no longer
138139
/// needed.
139140
package init(
140-
options: SourceKitLSPOptions = .testDefault(),
141+
options: SourceKitLSPOptions? = nil,
141142
hooks: Hooks = Hooks(),
142143
initialize: Bool = true,
143144
initializationOptions: LSPAny? = nil,
@@ -149,7 +150,12 @@ package final class TestSourceKitLSPClient: MessageHandler, Sendable {
149150
preInitialization: ((TestSourceKitLSPClient) -> Void)? = nil,
150151
cleanUp: @Sendable @escaping () -> Void = {}
151152
) async throws {
152-
var options = options
153+
var options =
154+
if let options {
155+
options
156+
} else {
157+
try await SourceKitLSPOptions.testDefault()
158+
}
153159
if let globalModuleCache = try globalModuleCache {
154160
options.swiftPMOrDefault.swiftCompilerFlags =
155161
(options.swiftPMOrDefault.swiftCompilerFlags ?? []) + ["-module-cache-path", try globalModuleCache.filePath]

Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final class SwiftPMBuildSystemTests: XCTestCase {
5353
]
5454
)
5555
let packageRoot = tempDir.appendingPathComponent("pkg")
56-
let buildSystemSpec = SwiftPMBuildSystem.searchForConfig(in: packageRoot, options: .testDefault())
56+
let buildSystemSpec = SwiftPMBuildSystem.searchForConfig(in: packageRoot, options: try await .testDefault())
5757
XCTAssertNil(buildSystemSpec)
5858
}
5959
}
@@ -779,7 +779,9 @@ final class SwiftPMBuildSystemTests: XCTestCase {
779779
withDestinationURL: URL(fileURLWithPath: tempDir.appendingPathComponent("pkg_real").filePath)
780780
)
781781

782-
let buildSystemSpec = try XCTUnwrap(SwiftPMBuildSystem.searchForConfig(in: packageRoot, options: .testDefault()))
782+
let buildSystemSpec = try unwrap(
783+
SwiftPMBuildSystem.searchForConfig(in: packageRoot, options: await .testDefault())
784+
)
783785
let buildSystemManager = await BuildSystemManager(
784786
buildSystemSpec: buildSystemSpec,
785787
toolchainRegistry: .forTesting,
@@ -864,7 +866,9 @@ final class SwiftPMBuildSystemTests: XCTestCase {
864866
withDestinationURL: URL(fileURLWithPath: tempDir.appendingPathComponent("pkg_real").filePath)
865867
)
866868

867-
let buildSystemSpec = try XCTUnwrap(SwiftPMBuildSystem.searchForConfig(in: symlinkRoot, options: .testDefault()))
869+
let buildSystemSpec = try unwrap(
870+
SwiftPMBuildSystem.searchForConfig(in: symlinkRoot, options: await .testDefault())
871+
)
868872
let buildSystemManager = await BuildSystemManager(
869873
buildSystemSpec: buildSystemSpec,
870874
toolchainRegistry: .forTesting,
@@ -961,7 +965,7 @@ final class SwiftPMBuildSystemTests: XCTestCase {
961965
.appendingPathComponent("Sources")
962966
.appendingPathComponent("lib")
963967

964-
let buildSystemSpec = SwiftPMBuildSystem.searchForConfig(in: workspaceRoot, options: .testDefault())
968+
let buildSystemSpec = SwiftPMBuildSystem.searchForConfig(in: workspaceRoot, options: try await .testDefault())
965969
XCTAssertNil(buildSystemSpec)
966970
}
967971
}

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ final class BackgroundIndexingTests: XCTestCase {
969969
}
970970

971971
func testUseBuildFlagsDuringPreparation() async throws {
972-
var options = SourceKitLSPOptions.testDefault()
972+
var options = try await SourceKitLSPOptions.testDefault()
973973
options.swiftPMOrDefault.swiftCompilerFlags = ["-D", "MY_FLAG"]
974974
let project = try await SwiftPMTestProject(
975975
files: [
@@ -1016,7 +1016,7 @@ final class BackgroundIndexingTests: XCTestCase {
10161016
func testUseSwiftSDKFlagsDuringPreparation() async throws {
10171017
try await SkipUnless.canSwiftPMCompileForIOS()
10181018

1019-
var options = SourceKitLSPOptions.testDefault()
1019+
var options = try await SourceKitLSPOptions.testDefault()
10201020
options.swiftPMOrDefault.swiftSDK = "arm64-apple-ios"
10211021
let project = try await SwiftPMTestProject(
10221022
files: [
@@ -1108,7 +1108,7 @@ final class BackgroundIndexingTests: XCTestCase {
11081108
}
11091109

11101110
func testCrossModuleFunctionalityEvenIfLowLevelModuleHasErrors() async throws {
1111-
var options = SourceKitLSPOptions.testDefault()
1111+
var options = try await SourceKitLSPOptions.testDefault()
11121112
options.backgroundPreparationMode = .enabled
11131113
let project = try await SwiftPMTestProject(
11141114
files: [
@@ -1154,7 +1154,7 @@ final class BackgroundIndexingTests: XCTestCase {
11541154
}
11551155

11561156
func testCrossModuleFunctionalityWithPreparationNoSkipping() async throws {
1157-
var options = SourceKitLSPOptions.testDefault()
1157+
var options = try await SourceKitLSPOptions.testDefault()
11581158
options.backgroundPreparationMode = .noLazy
11591159
let project = try await SwiftPMTestProject(
11601160
files: [
@@ -1438,7 +1438,7 @@ final class BackgroundIndexingTests: XCTestCase {
14381438
func testCancelIndexing() async throws {
14391439
try SkipUnless.longTestsEnabled()
14401440

1441-
var options = SourceKitLSPOptions.testDefault()
1441+
var options = try await SourceKitLSPOptions.testDefault()
14421442
options.backgroundPreparationMode = .enabled
14431443
options.indexOrDefault.updateIndexStoreTimeout = 1 /* second */
14441444

Tests/SourceKitLSPTests/BuildSystemTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ final class BuildSystemTests: XCTestCase {
6767
configPath: URL(fileURLWithPath: "/")
6868
),
6969
toolchainRegistry: .forTesting,
70-
options: .testDefault(),
70+
options: try .testDefault(),
7171
connectionToClient: DummyBuildSystemManagerConnectionToClient(),
7272
buildSystemHooks: BuildSystemHooks()
7373
)
7474
buildSystem = try await unwrap(buildSystemInjector.testBuildSystem)
7575

7676
self.workspace = await Workspace.forTesting(
77-
options: SourceKitLSPOptions.testDefault(),
77+
options: try .testDefault(),
7878
testHooks: Hooks(),
7979
buildSystemManager: buildSystemManager,
8080
indexTaskScheduler: .forTesting

Tests/SourceKitLSPTests/LocalSwiftTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ final class LocalSwiftTests: XCTestCase {
14251425
func testSourceKitdTimeout() async throws {
14261426
try SkipUnless.longTestsEnabled()
14271427

1428-
var options = SourceKitLSPOptions.testDefault()
1428+
var options = try await SourceKitLSPOptions.testDefault()
14291429
// This is how long we wait until implicitly cancelling the first diagnostics request.
14301430
// It needs to be long enough so we can compute diagnostics for the second requests.
14311431
// 1s is not sufficient on Windows for that.

Tests/SwiftSourceKitPluginTests/PluginSwiftPMTestProject.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class PluginSwiftPMTestProject {
2828

2929
private var _buildSystemManager: BuildSystemManager?
3030
private var buildSystemManager: BuildSystemManager {
31-
get async {
31+
get async throws {
3232
if let _buildSystemManager {
3333
return _buildSystemManager
3434
}
@@ -39,7 +39,7 @@ final class PluginSwiftPMTestProject {
3939
configPath: scratchDirectory.appendingPathComponent("Package.swift")
4040
),
4141
toolchainRegistry: .forTesting,
42-
options: .testDefault(backgroundIndexing: false),
42+
options: try .testDefault(backgroundIndexing: false),
4343
connectionToClient: DummyBuildSystemManagerConnectionToClient(),
4444
buildSystemHooks: BuildSystemHooks()
4545
)
@@ -98,8 +98,8 @@ final class PluginSwiftPMTestProject {
9898
}
9999

100100
package func compilerArguments(for fileName: String) async throws -> [String] {
101-
await buildSystemManager.waitForUpToDateBuildGraph()
102-
let buildSettings = await buildSystemManager.buildSettingsInferredFromMainFile(
101+
try await buildSystemManager.waitForUpToDateBuildGraph()
102+
let buildSettings = try await buildSystemManager.buildSettingsInferredFromMainFile(
103103
for: try uri(for: fileName),
104104
language: .swift,
105105
fallbackAfterTimeout: false

0 commit comments

Comments
 (0)