@@ -67,12 +67,16 @@ final class WatchedFileChangeHandler {
6767
6868 logger. info ( " Received \( changes. count) file changes " )
6969
70- // First, calculate deleted targets before we clear them from the targetStore
71- let deletedTargets = {
70+ let deletedFiles = changes. filter { $0. type == . deleted }
71+ let createdFiles = changes. filter { $0. type == . created }
72+ let changedFiles = changes. filter { $0. type == . changed }
73+
74+ // First, determine which targets had removed files.
75+ let targetsAffectedByDeletions : [ InvalidatedTarget ] = {
7276 do {
73- return try changes . filter { $0 . type == . deleted } . flatMap { change -> [ AffectedTarget ] in
77+ return try deletedFiles . flatMap { change in
7478 try targetStore. bspURIs ( containingSrc: change. uri) . map {
75- AffectedTarget ( uri: $0, kind : change. type )
79+ InvalidatedTarget ( uri: $0, fileUri : change. uri , kind : . deleted )
7680 }
7781 }
7882 } catch {
@@ -81,9 +85,10 @@ final class WatchedFileChangeHandler {
8185 }
8286 } ( )
8387
84- // If there are any 'created' files, we need to clear the targetStore and fetch targets again
85- // Otherwise, the targetStore won't know about them
86- if changes. contains ( where: { $0. type == . created } ) {
88+ // If there are any 'created' files, we need to clear the targetStore immediately and fetch targets again.
89+ // Otherwise, the targetStore won't know about them.
90+ // FIXME: This is quite expensive, but the easier thing to do. We can try improving this later.
91+ if !createdFiles. isEmpty {
8792 let taskId = TaskId ( id: " watchedFiles- \( UUID ( ) . uuidString) " )
8893 connection? . startWorkTask ( id: taskId, title: " Indexing: Re-processing build graph " )
8994 targetStore. clearCache ( )
@@ -96,12 +101,13 @@ final class WatchedFileChangeHandler {
96101 connection? . finishTask ( id: taskId, status: . ok)
97102 }
98103
99- // Now that the targetStore knows about the newly created files, we can calculate the created targets
100- let createdTargets = {
104+ // Now that the targetStore knows about the newly created files, we can determine which targets
105+ // were affected by those creations.
106+ let targetsAffectedByCreations : [ InvalidatedTarget ] = {
101107 do {
102- return try changes . filter { $0 . type == . created } . flatMap { change -> [ AffectedTarget ] in
108+ return try createdFiles . flatMap { change in
103109 try targetStore. bspURIs ( containingSrc: change. uri) . map {
104- AffectedTarget ( uri: $0, kind : change. type )
110+ InvalidatedTarget ( uri: $0, fileUri : change. uri , kind : . created )
105111 }
106112 }
107113 } catch {
@@ -110,12 +116,12 @@ final class WatchedFileChangeHandler {
110116 }
111117 } ( )
112118
113- // Finally, calculate the changed targets
114- let changedTargets = {
119+ // Finally, calculate the targets affected by regular changes.
120+ let targetsAffectedByChanges : [ InvalidatedTarget ] = {
115121 do {
116- return try changes . filter { $0 . type == . changed } . flatMap { change -> [ AffectedTarget ] in
122+ return try changedFiles . flatMap { change in
117123 try targetStore. bspURIs ( containingSrc: change. uri) . map {
118- AffectedTarget ( uri: $0, kind : change. type )
124+ InvalidatedTarget ( uri: $0, fileUri : change. uri , kind : . changed )
119125 }
120126 }
121127 } catch {
@@ -124,24 +130,20 @@ final class WatchedFileChangeHandler {
124130 }
125131 } ( )
126132
127- let affectedTargets : Set < AffectedTarget > = Set ( deletedTargets + createdTargets + changedTargets )
133+ let invalidatedTargets = targetsAffectedByDeletions + targetsAffectedByCreations + targetsAffectedByChanges
128134
129- // Invalidate our observers about the affected targets
135+ // Notify our observers about the affected targets
130136 for observer in observers {
131- do {
132- try observer. invalidate ( targets: affectedTargets)
133- } catch {
134- logger. error ( " Error invalidating observer: \( error) " )
135- // Continue with other observers
136- }
137+ try ? observer. invalidate ( targets: invalidatedTargets)
137138 }
138139
139140 // Notify SK-LSP about the affected targets
141+ let uniqueInvalidatedTargets = Set ( invalidatedTargets. map { $0. uri } )
140142 let response = OnBuildTargetDidChangeNotification (
141- changes: affectedTargets . map { target in
143+ changes: uniqueInvalidatedTargets . map { targetUri in
142144 BuildTargetEvent (
143- target: BuildTargetIdentifier ( uri: target . uri ) ,
144- kind: target . kind . buildTargetEventKind ,
145+ target: BuildTargetIdentifier ( uri: targetUri ) ,
146+ kind: . changed , // FIXME: We should eventually detect here also if the target is new/deleted.
145147 dataKind: nil ,
146148 data: nil
147149 )
@@ -160,14 +162,3 @@ final class WatchedFileChangeHandler {
160162 return supportedFileExtensions. contains ( String ( result. reversed ( ) ) )
161163 }
162164}
163-
164- extension FileChangeType {
165- fileprivate var buildTargetEventKind : BuildTargetEventKind ? {
166- switch self {
167- case . changed: return . changed
168- case . created: return . created
169- case . deleted: return . deleted
170- default : return nil
171- }
172- }
173- }
0 commit comments