Skip to content

Commit 19f8638

Browse files
authored
Merge pull request #949 from ahoppen/ahoppen/fix-unbalanced-register-unregister-crash
Remove assertion that we can't register for change notifications twice in `SwiftPMWorkspace`
2 parents 9f7ff08 + 4a924c4 commit 19f8638

File tree

8 files changed

+17
-23
lines changed

8 files changed

+17
-23
lines changed

Sources/SKCore/BuildServerBuildSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ extension BuildServerBuildSystem: BuildSystem {
256256
return buildSettings[document]
257257
}
258258

259-
public func registerForChangeNotifications(for uri: DocumentURI, language: Language) {
259+
public func registerForChangeNotifications(for uri: DocumentURI) {
260260
let request = RegisterForChanges(uri: uri, action: .register)
261261
_ = self.buildServer?.send(request) { result in
262262
if let error = result.failure {

Sources/SKCore/BuildSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public protocol BuildSystem: AnyObject {
7070
/// IMPORTANT: When first receiving a register request, the `BuildSystem` MUST asynchronously
7171
/// inform its delegate of any initial settings for the given file via the
7272
/// `fileBuildSettingsChanged` method, even if unavailable.
73-
func registerForChangeNotifications(for: DocumentURI, language: Language) async
73+
func registerForChangeNotifications(for: DocumentURI) async
7474

7575
/// Unregister the given file for build-system level change notifications,
7676
/// such as command line flag changes, dependency changes, etc.

Sources/SKCore/BuildSystemManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ extension BuildSystemManager {
154154
// system. That way, iff the main file changes, we will also notify the
155155
// delegate about build setting changes of all header files that are based
156156
// on that main file.
157-
await buildSystem?.registerForChangeNotifications(for: mainFile, language: language)
157+
await buildSystem?.registerForChangeNotifications(for: mainFile)
158158
}
159159

160160
public func unregisterForChangeNotifications(for uri: DocumentURI) async {

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public actor CompilationDatabaseBuildSystem {
5151

5252
/// The URIs for which the delegate has registered for change notifications,
5353
/// mapped to the language the delegate specified when registering for change notifications.
54-
var watchedFiles: [DocumentURI: Language] = [:]
54+
var watchedFiles: Set<DocumentURI> = []
5555

5656
private var _indexStorePath: AbsolutePath?
5757
public var indexStorePath: AbsolutePath? {
@@ -109,13 +109,13 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
109109
)
110110
}
111111

112-
public func registerForChangeNotifications(for uri: DocumentURI, language: Language) async {
113-
self.watchedFiles[uri] = language
112+
public func registerForChangeNotifications(for uri: DocumentURI) async {
113+
self.watchedFiles.insert(uri)
114114
}
115115

116116
/// We don't support change watching.
117117
public func unregisterForChangeNotifications(for uri: DocumentURI) {
118-
self.watchedFiles[uri] = nil
118+
self.watchedFiles.remove(uri)
119119
}
120120

121121
private func database(for url: URL) -> CompilationDatabase? {
@@ -165,11 +165,7 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
165165
)
166166

167167
if let delegate = self.delegate {
168-
var changedFiles = Set<DocumentURI>()
169-
for (uri, _) in self.watchedFiles {
170-
changedFiles.insert(uri)
171-
}
172-
await delegate.fileBuildSettingsChanged(changedFiles)
168+
await delegate.fileBuildSettingsChanged(self.watchedFiles)
173169
}
174170
}
175171

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public actor SwiftPMWorkspace {
8080

8181
/// The URIs for which the delegate has registered for change notifications,
8282
/// mapped to the language the delegate specified when registering for change notifications.
83-
var watchedFiles: [DocumentURI: Language] = [:]
83+
var watchedFiles: Set<DocumentURI> = []
8484

8585
/// This callback is informed when `reloadPackage` starts and ends executing.
8686
var reloadPackageStatusCallback: (ReloadPackageStatus) async -> Void
@@ -245,8 +245,7 @@ extension SwiftPMWorkspace {
245245
await reloadPackageStatusCallback(.end)
246246
return
247247
}
248-
let changedFiles = Set<DocumentURI>(self.watchedFiles.keys)
249-
await delegate.fileBuildSettingsChanged(changedFiles)
248+
await delegate.fileBuildSettingsChanged(self.watchedFiles)
250249
await delegate.fileHandlingCapabilityChanged()
251250
await reloadPackageStatusCallback(.end)
252251
}
@@ -300,15 +299,14 @@ extension SwiftPMWorkspace: SKCore.BuildSystem {
300299
return nil
301300
}
302301

303-
public func registerForChangeNotifications(for uri: DocumentURI, language: Language) async {
304-
assert(self.watchedFiles[uri] == nil, "Registered twice for change notifications of the same URI")
305-
self.watchedFiles[uri] = language
302+
public func registerForChangeNotifications(for uri: DocumentURI) async {
303+
self.watchedFiles.insert(uri)
306304
}
307305

308306
/// Unregister the given file for build-system level change notifications, such as command
309307
/// line flag changes, dependency changes, etc.
310308
public func unregisterForChangeNotifications(for uri: DocumentURI) {
311-
self.watchedFiles[uri] = nil
309+
self.watchedFiles.remove(uri)
312310
}
313311

314312
/// Returns the resolved target description for the given file, if one is known.

Tests/SKCoreTests/BuildServerBuildSystemTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final class BuildServerBuildSystemTests: XCTestCase {
5353
_fixLifetime(buildSystemDelegate)
5454
}
5555
await buildSystem.setDelegate(buildSystemDelegate)
56-
await buildSystem.registerForChangeNotifications(for: DocumentURI(fileUrl), language: .swift)
56+
await buildSystem.registerForChangeNotifications(for: DocumentURI(fileUrl))
5757

5858
XCTAssertEqual(XCTWaiter.wait(for: [expectation], timeout: defaultTimeout), .completed)
5959
}
@@ -76,7 +76,7 @@ final class BuildServerBuildSystemTests: XCTestCase {
7676
_fixLifetime(buildSystemDelegate)
7777
}
7878
await buildSystem.setDelegate(buildSystemDelegate)
79-
await buildSystem.registerForChangeNotifications(for: DocumentURI(fileUrl), language: .swift)
79+
await buildSystem.registerForChangeNotifications(for: DocumentURI(fileUrl))
8080

8181
try await fulfillmentOfOrThrow([expectation])
8282
}

Tests/SKCoreTests/BuildSystemManagerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ class ManualBuildSystem: BuildSystem {
437437
return map[uri]
438438
}
439439

440-
func registerForChangeNotifications(for uri: DocumentURI, language: Language) async {
440+
func registerForChangeNotifications(for uri: DocumentURI) async {
441441
}
442442

443443
func unregisterForChangeNotifications(for: DocumentURI) {

Tests/SourceKitLSPTests/BuildSystemTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class TestBuildSystem: BuildSystem {
4848
return buildSettingsByFile[document]
4949
}
5050

51-
func registerForChangeNotifications(for uri: DocumentURI, language: Language) async {
51+
func registerForChangeNotifications(for uri: DocumentURI) async {
5252
watchedFiles.insert(uri)
5353
}
5454

0 commit comments

Comments
 (0)