X Tutup
Skip to content

LSP: Don't respond to non-GDScript files#103186

Closed
Na-r wants to merge 1 commit intogodotengine:masterfrom
Na-r:master
Closed

LSP: Don't respond to non-GDScript files#103186
Na-r wants to merge 1 commit intogodotengine:masterfrom
Na-r:master

Conversation

@Na-r
Copy link

@Na-r Na-r commented Feb 22, 2025

Background

When an LSP client saves, Godot's LSP server receives a textDocument/didSave notification. Depending on how the LSP client is implemented, it may send this notification for any file, not necessarily only GDScript files. Godot's LSP server wouldn't do any checking on the file type received from this notification, so it would send back syntax errors for whatever file was saved.

According to the LSP spec, unless the server has previously sent TextDocumentSaveRegistrationOptions, the LSP client should send the textDocument/didSave notification for all files saved. See: the LSP spec and zed-industries/zed/pull/19183. (Can someone more familiar with LSP spec confirm this?)

Godot's LSP mostly uses static registrations during initialization for its LSP server capabilities, but TextDocumentSaveRegistrationOptions is only available as a dynamic registration. This makes implementing the fix that way a bit uglier than just checking file types when receiving notifications.

Fix

We will now check for a file being a valid GDScript file, via the language ID or file extension, before processing it any further. While this fix was specifically for the textDocument/didSave notification, I added checks for other related notifications as well.

@Na-r Na-r requested a review from a team as a code owner February 22, 2025 19:25

bool GDScriptTextDocument::is_valid_gd_file(const lsp::TextDocumentItem &p_doc) {
// LanguageId isn't always set, but check it for good practice
return p_doc.languageId == "gdscript" || p_doc.uri.ends_with(".gd");
Copy link
Contributor

Choose a reason for hiding this comment

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

this could need a change after #97657 is merged, unles p_doc.languageId == "gdscript"is good enough

Copy link
Author

Choose a reason for hiding this comment

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

This would definitely need a change. Some of the notifications (including didSave) don't return anything besides the URI and text content. Should a check against .gdt be put in now to get ahead of it? There's also a similar update that would be needed at here.

Copy link
Contributor

Choose a reason for hiding this comment

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

well, for now both prs are open, i just wanted to reference this PR in the other one 👍

Copy link
Member

@HolonProduction HolonProduction Feb 28, 2025

Choose a reason for hiding this comment

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

I don't think checking the file extension on our side is a good idea. Editors like vscode allow changing the language mode for documents and with this PR, this will not work anymore (it receives initial diagnostics, but they never get updated). This might become a problem with custom tooling (e.g. preprocessor script which uses custom extensions). Also it might become a roadblock for implementing language embeddings (not sure if VSCode has such a capability, but for Idea IDE's a plugin could embed GDScript blocks into scene files for builtin scripts).

Interpretation of the content type should be up to the editor IMO, so we should only be using the language id. As pointed out this is not sent with every request, but that is intended, since LSP is not stateless. When we receive a text document identifier we should already have received a text document item for the uri which contains this information.

(Well that's the case for most notifications, sadly didSave seems under specified in this regard. I opened an issue against the specification to clear this up.)

Copy link
Member

Choose a reason for hiding this comment

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

Based on the answer to the issue over at the LSP repo, I think we can work under the assumption that didSave requires taking ownership. So the correct approach would be to save the content type the editor provides with didOpen and then use it for other notifications.

@HolonProduction
Copy link
Member

The above mentioned persisting of language ids was implemented in #105236 along with some basic safeguards in common endpoints. Closing as superseded, but thanks for your contribution nevertheless!

@HolonProduction HolonProduction removed this from the 4.6 milestone Jan 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GDScript errors are reported in other filetypes (e.g. .rs files)

4 participants

X Tutup