Skip to content

Commit dc42110

Browse files
[WIP] Example commit (#23)
Provide an example commit with changes that seem to fix the regression I was encountering. It seems for some reason buildTarget/prepare is called multiple times for me, so adding back the original caching fixes it... Also, removing "chmod -R 777" seems to have performance benefits for me?
1 parent 1116cc0 commit dc42110

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

Example/.sourcekit-lsp/config.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"backgroundIndexing": true,
33
"backgroundPreparationMode": "build",
4-
"defaultWorkspaceType": "buildServer"
4+
"defaultWorkspaceType": "buildServer",
5+
"logging": {
6+
"level": "error",
7+
"privacyLevel": "sensitive"
8+
}
59
}

Sources/SourceKitBazelBSP/RequestHandlers/PrepareHandler.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ private let logger = makeFileLevelBSPLogger()
2727
///
2828
/// Builds the provided list of targets upon request.
2929
final class PrepareHandler {
30-
3130
private let initializedConfig: InitializedServerConfig
3231
private let targetStore: BazelTargetStore
3332
private let commandRunner: CommandRunner
3433
private weak var connection: LSPConnection?
3534

35+
// Prevent redundant builds of the same targets
36+
private var didRun = false
37+
3638
init(
3739
initializedConfig: InitializedServerConfig,
3840
targetStore: BazelTargetStore,
@@ -46,9 +48,15 @@ final class PrepareHandler {
4648
}
4749

4850
func prepareTarget(
49-
_ request: BuildTargetPrepareRequest,
51+
_: BuildTargetPrepareRequest,
5052
_ id: RequestID
5153
) throws -> VoidResponse {
54+
// FIXME: Invalidate on changes
55+
guard !didRun else {
56+
logger.info("Build already completed, skipping redundant build")
57+
return VoidResponse()
58+
}
59+
5260
let taskId = TaskId(id: "buildPrepare-\(id.description)")
5361
connection?.startWorkTask(id: taskId, title: "Indexing: Building targets")
5462
do {
@@ -58,6 +66,7 @@ final class PrepareHandler {
5866
// and these are lost if you build the libs directly. We need those transitions here too.
5967
try build(bazelLabels: initializedConfig.baseConfig.targets)
6068
connection?.finishTask(id: taskId, status: .ok)
69+
didRun = true
6170
return VoidResponse()
6271
} catch {
6372
connection?.finishTask(id: taskId, status: .error)
@@ -78,6 +87,7 @@ final class PrepareHandler {
7887
bazelLabels labelsToBuild: [String]
7988
) throws {
8089
logger.info("Will build \(labelsToBuild.count) targets")
90+
logger.info("Target to build: \(labelsToBuild)")
8191

8292
// Build the provided targets, on our special output base and taking into account special index flags.
8393
_ = try commandRunner.bazelIndexAction(
@@ -86,7 +96,7 @@ final class PrepareHandler {
8696
)
8797

8898
// FIXME: This might not be necessary anymore
89-
_ = try commandRunner.run("chmod -R 777 \(initializedConfig.outputBase)")
99+
// _ = try commandRunner.run("chmod -R 777 \(initializedConfig.outputBase)")
90100

91101
logger.info("Finished building targets!")
92102
}

Sources/SourceKitBazelBSP/Server/SourceKitBazelBSPServer.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ private let logger = makeFileLevelBSPLogger()
2626

2727
/// The higher-level class that bootstraps and manages the BSP server.
2828
package final class SourceKitBazelBSPServer {
29-
3029
let connection: LSPConnection
3130
let handler: MessageHandler
3231

@@ -62,16 +61,16 @@ package final class SourceKitBazelBSPServer {
6261
connection: JSONRPCConnection
6362
) {
6463
// First, deal with the no-op handlers we cannot or do not want to handle directly.
65-
registry.register(notificationHandler: { (notification: OnBuildInitializedNotification) in
64+
registry.register(notificationHandler: { (_: OnBuildInitializedNotification) in
6665
// no-op
6766
})
68-
registry.register(notificationHandler: { (notification: CancelRequestNotification) in
67+
registry.register(notificationHandler: { (_: CancelRequestNotification) in
6968
// no-op, no request canceling since the code today is not async
7069
})
7170
registry.register(requestHandler: {
72-
(request: WorkspaceWaitForBuildSystemUpdatesRequest, id: RequestID) in
71+
(_: WorkspaceWaitForBuildSystemUpdatesRequest, _: RequestID) in
7372
// FIXME: no-op, no special handling since the code today is not async, but I might be wrong here.
74-
return VoidResponse()
73+
VoidResponse()
7574
})
7675

7776
// Then, register the things we are interested in.
@@ -96,7 +95,20 @@ package final class SourceKitBazelBSPServer {
9695
targetStore: targetStore,
9796
connection: connection
9897
)
99-
registry.register(requestHandler: skOptionsHandler.textDocumentSourceKitOptions)
98+
registry.register(requestHandler: { (request: TextDocumentSourceKitOptionsRequest, id: RequestID) in
99+
// Handle the optional response type properly - if nil is returned for valid reasons
100+
// (like missing module entries), return an empty response instead of nil to prevent connection closure
101+
if let response = try skOptionsHandler.textDocumentSourceKitOptions(request, id) {
102+
return response
103+
} else {
104+
// Return an empty response with no compiler arguments rather than nil
105+
// This prevents SourceKit LSP from closing the connection
106+
return TextDocumentSourceKitOptionsResponse(
107+
compilerArguments: [],
108+
workingDirectory: initializedConfig.rootUri
109+
)
110+
}
111+
})
100112

101113
// buildTarget/prepare
102114
let prepareHandler = PrepareHandler(

0 commit comments

Comments
 (0)