@@ -46,28 +46,41 @@ final class BuildTargetsHandler {
4646 let taskId = TaskId ( id: " buildTargets- \( id. description) " )
4747 connection? . startWorkTask ( id: taskId, title: " sourcekit-bazel-bsp: Processing the build graph... " )
4848 do {
49- nonisolated ( unsafe) var shouldDispatchNotification = false
49+ nonisolated ( unsafe) var shouldReplyEmpty = false
5050 let result = try targetStore. stateLock. withLockUnchecked {
51- shouldDispatchNotification = isFirstTime
51+ shouldReplyEmpty = isFirstTime
5252 isFirstTime = false
53- return try targetStore. fetchTargets ( )
53+ // If this is the first time we're responding to buildTargets, sourcekit-lsp will expect us to return an empty list
54+ // and then later send a notification containing the changes for performance reasons.
55+ // See https://github.com/spotify/sourcekit-bazel-bsp/issues/102
56+ if shouldReplyEmpty {
57+ reply ( . success( WorkspaceBuildTargetsResponse ( targets: [ ] ) ) )
58+ }
59+ let result = try targetStore. fetchTargets ( )
60+ logger. debug ( " Found \( result. count, privacy: . public) targets " )
61+ logger. logFullObjectInMultipleLogMessages (
62+ level: . debug,
63+ header: " Target list " ,
64+ result. map { $0. id. uri. stringValue } . joined ( separator: " , " ) ,
65+ )
66+ return result
5467 }
55- logger. debug ( " Found \( result. count, privacy: . public) targets " )
56- logger. logFullObjectInMultipleLogMessages (
57- level: . debug,
58- header: " Target list " ,
59- result. map { $0. id. uri. stringValue } . joined ( separator: " , " ) ,
60- )
6168 connection? . finishTask ( id: taskId, status: . ok)
62- reply ( . success( WorkspaceBuildTargetsResponse ( targets: result) ) )
63- // If this is the first time we're responding to buildTargets, send an empty notification.
64- // This triggers sourcekit-lsp to calculate the file mappings which enables jump-to-definition to work.
65- // We only need to do this because we're replying to this request incorrectly.
66- // We should also be able to drop this if we figure out how to make the actual LSP --index-prefix-map flag work.
67- // See https://github.com/spotify/sourcekit-bazel-bsp/issues/102
68- if shouldDispatchNotification {
69- let notification = OnBuildTargetDidChangeNotification ( changes: [ ] )
69+ if shouldReplyEmpty {
70+ let targetEvents = result. map { target in
71+ BuildTargetEvent (
72+ target: target. id,
73+ kind: . created,
74+ dataKind: nil ,
75+ data: nil
76+ )
77+ }
78+ let notification = OnBuildTargetDidChangeNotification (
79+ changes: targetEvents
80+ )
7081 connection? . send ( notification)
82+ } else {
83+ reply ( . success( WorkspaceBuildTargetsResponse ( targets: result) ) )
7184 }
7285 } catch {
7386 connection? . finishTask ( id: taskId, status: . error)
0 commit comments