Skip to content

Preserve language service when closing generated interface#2598

Closed
edwardlee wants to merge 1 commit into
swiftlang:mainfrom
edwardlee:handle-interface-close
Closed

Preserve language service when closing generated interface#2598
edwardlee wants to merge 1 commit into
swiftlang:mainfrom
edwardlee:handle-interface-close

Conversation

@edwardlee
Copy link
Copy Markdown
Contributor

Closing a generated interface was removing the language service for the originating file because both share the same buildSettingsFile key. https://github.com/swiftlang/sourcekit-lsp/blob/main/Sources/SourceKitLSP/ReferenceDocumentURL.swift#L125
Guard the removal by when no other open document shares that key.
See #2209 and 7437d92

@rintaro
Copy link
Copy Markdown
Member

rintaro commented Apr 9, 2026

Good find, thanks! Could you add a test case that fails without this change?

@rintaro
Copy link
Copy Markdown
Member

rintaro commented Apr 9, 2026

On second thought, what if the documentManager document is closed first while the related generated interfaces are still open? Do they lose the language service?
Perhaps the language service should be tied to the uri itself, not the buildSettingsFile?

Closing a generated interface document (sourcekit-lsp:// URI) was
removing the language service for the originating source file because
both share the same buildSettingsFile key. Guard the removal so it
only happens when no other open document shares that key.

Relates to swiftlang#2209
@edwardlee edwardlee force-pushed the handle-interface-close branch from 30d1d29 to 0c3a2f9 Compare April 16, 2026 15:49
@edwardlee
Copy link
Copy Markdown
Contributor Author

Added test similar to testNoDiagnosticsInGeneratedInterface. Since the logic checks for any remaining open file with the key before shutting down LSP, both cases work. Tying to the uri makes sense, this was just simpler.

@rintaro
Copy link
Copy Markdown
Member

rintaro commented Apr 23, 2026

@swift-ci Please test

Copy link
Copy Markdown
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the test case!
I think the logic is fine. Just a code placement concern.

}

workspace.removeLanguageServices(for: uri)
if !documentManager.openDocuments.contains(where: { $0.buildSettingsFile == uri.buildSettingsFile }) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .buildSettingsFile as the key is an implementation detail of Workspace, so ideally this logic should remain encapsulated there.
Could you move this into Workspace.removeLanguageServices(for:)? You should be able to access the document manager via self.sourceKitLSPServer?.documentManager.
Also, maybe removeLanguageServices(for:) should be renamed to unsetLanguageServices(for:) to pair better with setLanguageServices(for:_:).

@rintaro
Copy link
Copy Markdown
Member

rintaro commented Apr 23, 2026

@swift-ci Please test Windows

@rintaro
Copy link
Copy Markdown
Member

rintaro commented Apr 23, 2026

[2026-04-23T06:10:58.845Z] <EXPR>:0: error: SwiftInterfaceTests.testClosingGeneratedInterfacePreservesOriginatingLanguageService : threw error "ResponseError(code: LanguageServerProtocol.ErrorCode(rawValue: -32001), message: "Unknown error: Invalid Scheme for reference document", data: nil)"
[2026-04-23T06:10:58.845Z] Test Case 'SwiftInterfaceTests.testClosingGeneratedInterfacePreservesOriginatingLanguageService' failed (0.218 seconds)

🤔

@rintaro
Copy link
Copy Markdown
Member

rintaro commented May 14, 2026

@swift-ci Please test

@rintaro
Copy link
Copy Markdown
Member

rintaro commented May 14, 2026

Hi @edwardlee, again, great catch and thanks for the fix! Since this is a serious issue we need to fix asap, I've opened #2648 to get this merged sooner.
I made a small adjustment to move the guard into Workspace and updated the test to pass on CI (hopefully).

@rintaro rintaro closed this May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants