Skip to content

Commit 9c145fd

Browse files
authored
Merge pull request #2375 from bnbarham/pass-toolset-through
Pass toolsets through to preparation for the SwiftPM build server
2 parents 4f76792 + 9952a11 commit 9c145fd

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

Sources/BuildServerIntegration/SwiftPMBuildServer.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ package actor SwiftPMBuildServer: BuiltInBuildServer {
126126
private let pluginConfiguration: PluginConfiguration
127127
private let traitConfiguration: TraitConfiguration
128128

129+
/// Paths to any toolsets provided in `SourceKitLSPOptions`, with any relative paths resolved based on the project
130+
/// root.
131+
private let toolsets: [AbsolutePath]
132+
129133
/// A `ObservabilitySystem` from `SwiftPM` that logs.
130134
private let observabilitySystem: ObservabilitySystem
131135

@@ -212,6 +216,11 @@ package actor SwiftPMBuildServer: BuiltInBuildServer {
212216
}
213217

214218
let absProjectRoot = try AbsolutePath(validating: projectRoot.filePath)
219+
self.toolsets =
220+
try options.swiftPMOrDefault.toolsets?.map {
221+
try AbsolutePath(validating: $0, relativeTo: absProjectRoot)
222+
} ?? []
223+
215224
let hostSDK = try SwiftSDK.hostSwiftSDK(AbsolutePath(validating: destinationToolchainBinDir.filePath))
216225
let hostSwiftPMToolchain = try UserToolchain(swiftSDK: hostSDK)
217226

@@ -230,9 +239,7 @@ package actor SwiftPMBuildServer: BuiltInBuildServer {
230239
let destinationSDK = try SwiftSDK.deriveTargetSwiftSDK(
231240
hostSwiftSDK: hostSDK,
232241
hostTriple: hostSwiftPMToolchain.targetTriple,
233-
customToolsets: options.swiftPMOrDefault.toolsets?.map {
234-
try AbsolutePath(validating: $0, relativeTo: absProjectRoot)
235-
} ?? [],
242+
customToolsets: toolsets,
236243
customCompileTriple: triple,
237244
swiftSDKSelector: options.swiftPMOrDefault.swiftSDK,
238245
store: SwiftSDKBundleStore(
@@ -753,6 +760,7 @@ package actor SwiftPMBuildServer: BuiltInBuildServer {
753760
if let traits = options.swiftPMOrDefault.traits {
754761
arguments += ["--traits", traits.joined(separator: ",")]
755762
}
763+
arguments += toolsets.flatMap { ["--toolset", $0.pathString] }
756764
arguments += options.swiftPMOrDefault.cCompilerFlags?.flatMap { ["-Xcc", $0] } ?? []
757765
arguments += options.swiftPMOrDefault.cxxCompilerFlags?.flatMap { ["-Xcxx", $0] } ?? []
758766
arguments += options.swiftPMOrDefault.swiftCompilerFlags?.flatMap { ["-Xswiftc", $0] } ?? []

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
632632
// Indexing will frequently fail if the source code is in an invalid state. Thus, log the failure at a low level.
633633
logger.debug(
634634
"""
635-
Updating index store for terminated with non-zero exit code \(code) for \(indexFiles)
635+
Updating index store terminated with non-zero exit code \(code) for \(indexFiles)
636636
Stderr:
637637
\(stderr)
638638
Stdout:
@@ -645,7 +645,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
645645
// The indexing job finished with a signal. Could be because the compiler crashed.
646646
// Ignore signal exit codes if this task has been cancelled because the compiler exits with SIGINT if it gets
647647
// interrupted.
648-
logger.error("Updating index store for signaled \(signal) for \(indexFiles)")
648+
logger.error("Updating index store signaled \(signal) for \(indexFiles)")
649649
BuildSettingsLogger.log(level: .error, settings: buildSettings, for: indexFiles)
650650
}
651651
case .abnormal(let exception):

Sources/SourceKitLSP/Workspace.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,14 @@ package final class Workspace: Sendable, BuildServerManagerDelegate {
524524
await buildServerManager.scheduleRecomputeCopyFileMap().value
525525
}
526526
if request.index ?? false {
527-
await semanticIndexManager?.waitForUpToDateIndex()
528-
await uncheckedIndex?.pollForUnitChangesAndWait()
527+
if let semanticIndexManager = await semanticIndexManager {
528+
await semanticIndexManager.waitForUpToDateIndex()
529+
} else {
530+
logger.debug("Skipping wait for background index in synchronize as it's disabled")
531+
532+
// Might have index while building, so still need to poll for any changes
533+
await uncheckedIndex?.pollForUnitChangesAndWait()
534+
}
529535
}
530536
}
531537
}

Tests/BuildServerIntegrationTests/SwiftPMBuildServerTests.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,74 @@ struct SwiftPMBuildServerTests {
11111111
#expect(compilerArgs.contains(try manifestURL.filePath))
11121112
}
11131113
}
1114+
1115+
@Test(
1116+
.enabled(if: Platform.current != .windows, "Toolsets are not working on Windows, see swift-package-manager#9438.")
1117+
)
1118+
func testToolsets() async throws {
1119+
let project = try await SwiftPMTestProject(
1120+
files: [
1121+
"Foo/foo.swift": """
1122+
import Bar
1123+
1124+
func foo() {
1125+
bar()
1126+
}
1127+
""",
1128+
"Bar/bar.swift": """
1129+
#if BAR
1130+
public func bar() {}
1131+
#endif
1132+
""",
1133+
"/toolset.json": """
1134+
{
1135+
"schemaVersion": "1.0",
1136+
"swiftCompiler": {
1137+
"extraCLIOptions": [
1138+
"-DBAR"
1139+
]
1140+
}
1141+
}
1142+
""",
1143+
"/.sourcekit-lsp/config.json": """
1144+
{
1145+
"swiftPM": {
1146+
"toolsets": ["toolset.json"]
1147+
}
1148+
}
1149+
""",
1150+
],
1151+
manifest: """
1152+
let package = Package(
1153+
name: "MyLibrary",
1154+
targets: [
1155+
.target(name: "Foo", dependencies: ["Bar"]),
1156+
.target(name: "Bar"),
1157+
]
1158+
)
1159+
""",
1160+
options: .testDefault(experimentalFeatures: [.sourceKitOptionsRequest]),
1161+
enableBackgroundIndexing: true,
1162+
)
1163+
1164+
let (uri, _) = try project.openDocument("foo.swift")
1165+
1166+
let options = try await project.testClient.send(
1167+
SourceKitOptionsRequest(
1168+
textDocument: TextDocumentIdentifier(uri),
1169+
prepareTarget: false,
1170+
allowFallbackSettings: false
1171+
)
1172+
)
1173+
#expect(options.compilerArguments.contains("-DBAR"))
1174+
1175+
let diagnostics = try #require(
1176+
await project.testClient.send(
1177+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
1178+
).fullReport?.items
1179+
)
1180+
#expect(diagnostics.isEmpty)
1181+
}
11141182
}
11151183

11161184
private func expectArgumentsDoNotContain(

0 commit comments

Comments
 (0)