Skip to content

textDocument/didOpen sent more than once, without textDocument/didClose inbetween #4985

@grendello

Description

@grendello

Thank you for the bug report

  • I am using the latest version of lsp-mode related packages.
  • I checked FAQ and Troubleshooting sections
  • You may also try reproduce the issue using clean environment using the following command: M-x lsp-start-plain

Bug description

I work on a rather complex C# codebase and I usually run csharp-ls as the LSP server, but recently we made a change that breaks both csharp-ls and OmniSharp. The only server that appears to work properly, at first, is the Roslyn LanguageServer. I'm able to load a file (for repro, I opened this file), it loads without errors, everything is fine, navigation, actions, lenses, the works.
Alas, the server throws an exception the moment I use lsp-ui-peek-find-definitions on a symbol, which action leads to opening a new file (for repro, I navigated to line 250 and tried to find definitions of the MonoAndroidHelper.GetExecutablePath method). In this case, the action immediately opens this file which leads to the following server exception:

[textDocument/didOpen] [LSP] System.InvalidOperationException: didOpen received for file:///.../src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Nullable.cs which is already open. - file LspWorkspaceManager.cs line 109
   at Microsoft.CodeAnalysis.Contract.Fail(String message, Int32 lineNumber, String filePath) in /_/src/Dependencies/Contracts/Contract.cs:line 161
   at Microsoft.CodeAnalysis.LanguageServer.LspWorkspaceManager.StartTrackingAsync(DocumentUri uri, SourceText documentText, String languageId, Int32 lspVersion, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.LanguageServer.Handler.DocumentChanges.DidOpenHandler.HandleNotificationAsync(DidOpenTextDocumentParams request, RequestContext context, CancellationToken cancellationToken) in /_/src/LanguageServer/Protocol/Handler/DocumentChanges/DidOpenHandler.cs:line 38
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`1.StartRequestAsync[TRequest,TResponse](TRequest request, TRequestContext context, IMethodHandler handler, String language, CancellationToken cancellationToken) in /_/src/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 204
[LSP] System.InvalidOperationException: didOpen received for file:///home/grendel/vc/xamarin/xamarin-android-worktrees/ndk-api-minimums/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Nullable.cs which is already open. - file LspWorkspaceManager.cs line 109
   at Microsoft.CodeAnalysis.Contract.Fail(String message, Int32 lineNumber, String filePath) in /_/src/Dependencies/Contracts/Contract.cs:line 161
   at Microsoft.CodeAnalysis.LanguageServer.LspWorkspaceManager.StartTrackingAsync(DocumentUri uri, SourceText documentText, String languageId, Int32 lspVersion, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.LanguageServer.Handler.DocumentChanges.DidOpenHandler.HandleNotificationAsync(DidOpenTextDocumentParams request, RequestContext context, CancellationToken cancellationToken) in /_/src/LanguageServer/Protocol/Handler/DocumentChanges/DidOpenHandler.cs:line 38
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`1.StartRequestAsync[TRequest,TResponse](TRequest request, TRequestContext context, IMethodHandler handler, String language, CancellationToken cancellationToken) in /_/src/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 204
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`1.StartRequestAsync[TRequest,TResponse](TRequest request, TRequestContext context, IMethodHandler handler, String language, CancellationToken cancellationToken) in /_/src/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 258
   at Microsoft.CodeAnalysis.LanguageServer.RoslynRequestExecutionQueue.WrapStartRequestTaskAsync(Task requestTask, Boolean rethrowExceptions) in /_/src/LanguageServer/Protocol/RoslynRequestExecutionQueue.cs:line 32
   at Microsoft.CommonLanguageServerProtocol.Framework.RequestExecutionQueue`1.ProcessQueueCoreAsync[TRequest,TResponse](IQueueItem`1 work, IMethodHandler handler, RequestHandlerMetadata metadata, ConcurrentDictionary`2 concurrentlyExecutingTasks, CancellationTokenSource currentWorkCts, CancellationToken cancellationToken) in /_/src/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/RequestExecutionQueue.cs:line 369
   at Microsoft.CommonLanguageServerProtocol.Framework.RequestExecutionQueue`1.InvokeProcessCoreAsync(IQueueItem`1 work, RequestHandlerMetadata metadata, IMethodHandler handler, MethodInfo methodInfo, ConcurrentDictionary`2 concurrentlyExecutingTasks, CancellationTokenSource currentWorkCts, CancellationToken cancellationToken) in /_/src/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/RequestExecutionQueue.cs:line 322
   at Microsoft.CommonLanguageServerProtocol.Framework.RequestExecutionQueue`1.ProcessQueueAsync() in /_/src/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/RequestExecutionQueue.cs:line 274

This is caused by lsp-mode sending two textDocument/didOpen notifications for MonoAndroidHelper.Nullable.cswithout separating them with textDocument/didClose, as required by the LSP specification.

I logged the client/server I/O and it shows this (source code and intervening message bodies omitted, file paths shortened for brevity), shown below.

Steps to reproduce

  • Clone https://github.com/dotnet/android
  • Open src/Xamarin.Android.Build.Tasks/Utilities/NdkTools/NdkTools.cs
  • Go to line 250 in that file, the line in the current main branch reads
executablePath = Path.Combine (toolDir, MonoAndroidHelper.GetExecutablePath (toolDir, Path.GetFileName (toolPath)));
  • Position cursor on the GetExecutablePath method
  • Press M-. or run lsp-ui-peek-find-definitions

Expected behavior

lsp-mode should send just a single textDocument/didOpen for the document being open, or it should send textDocument/didClose before sending the 2nd didOpen notification.

Which Language Server did you use?

Roslyn LanguageServer (several versions).

OS

Linux

Error callstack

[Trace - 09:24:41 PM] Sending notification 'textDocument/didOpen'.
Params: {
  "textDocument": {
    "uri": "file:///.../src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Nullable.cs",
    "languageId": "csharp",
    "version": 0,
    "text": ""
  }
}

[Trace - 09:24:41 PM] Sending request 'textDocument/codeLens - (33)'.
[Trace - 09:24:41 PM] Sending request 'textDocument/diagnostic - (34)'.
[Trace - 09:24:41 PM] Sending request 'textDocument/documentSymbol - (35)'.
[Trace - 09:24:41 PM] Sending notification '$/cancelRequest'.
[Trace - 09:24:41 PM] Sending request 'textDocument/documentSymbol - (36)'.
[Trace - 09:24:41 PM] Sending request 'textDocument/codeAction - (37)'.

[Trace - 09:24:41 PM] Sending notification 'textDocument/didOpen'.
Params: {
  "textDocument": {
    "uri": "file:///.../src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Nullable.cs",
    "languageId": "csharp",
    "version": 0,
    "text": ""
  }
}

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions