From 053ccf6c5259eca5895cd57ce0a0c82af8fcd89d Mon Sep 17 00:00:00 2001 From: David Barbet Date: Fri, 7 Jun 2024 17:07:03 -0700 Subject: [PATCH 1/2] Throw when there is a syntax version mismatch in codelens so the client will re-request --- .../Protocol/Handler/CodeLens/CodeLensResolveHandler.cs | 9 ++++++--- .../ProtocolUnitTests/CodeLens/CSharpCodeLensTests.cs | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs b/src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs index 0d9a44337ffad..a8a74fb455e12 100644 --- a/src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs +++ b/src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis.Shared.Extensions; using LSP = Roslyn.LanguageServer.Protocol; using System.Text.Json; +using StreamJsonRpc; namespace Microsoft.CodeAnalysis.LanguageServer.Handler.CodeLens; @@ -48,11 +49,13 @@ public LSP.TextDocumentIdentifier GetTextDocumentIdentifier(LSP.CodeLens request ] }; - // If the request is for an older version of the document, return a request with '- references' + // If the request is for an older version of the document, throw an exception so the client knows to re-query us. if (resolveData.SyntaxVersion != currentDocumentSyntaxVersion.ToString()) { - context.TraceInformation($"Requested syntax version {resolveData.SyntaxVersion} does not match current version {currentDocumentSyntaxVersion}"); - return request; + throw new LocalRpcException($"Resolve version {resolveData.SyntaxVersion} does not match current version {currentDocumentSyntaxVersion}") + { + ErrorCode = LspErrorCodes.ContentModified + }; } var codeLensMemberFinder = document.GetRequiredLanguageService(); diff --git a/src/LanguageServer/ProtocolUnitTests/CodeLens/CSharpCodeLensTests.cs b/src/LanguageServer/ProtocolUnitTests/CodeLens/CSharpCodeLensTests.cs index 0472b4349df1b..853cd7ef7225e 100644 --- a/src/LanguageServer/ProtocolUnitTests/CodeLens/CSharpCodeLensTests.cs +++ b/src/LanguageServer/ProtocolUnitTests/CodeLens/CSharpCodeLensTests.cs @@ -395,8 +395,8 @@ void UseM() await testLspServer.InsertTextAsync(documentUri, (0, 0, "A")); // Assert that we don't crash when sending an old request to a new document - var firstDocumentResult2 = await testLspServer.ExecuteRequestAsync(LSP.Methods.CodeLensResolveName, firstCodeLens, CancellationToken.None); - Assert.NotNull(firstDocumentResult2?.Command?.Title); + var firstDocumentResult2 = Assert.ThrowsAsync(async () => await testLspServer.ExecuteRequestAsync(LSP.Methods.CodeLensResolveName, firstCodeLens, CancellationToken.None)); + Assert.False(testLspServer.GetServerAccessor().HasShutdownStarted()); } [Theory, CombinatorialData] From e22711179a9632ea6c074adef0e3cb81820b23ff Mon Sep 17 00:00:00 2001 From: David Barbet Date: Mon, 10 Jun 2024 12:10:59 -0700 Subject: [PATCH 2/2] Update src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs Co-authored-by: Sam Harwell --- .../Protocol/Handler/CodeLens/CodeLensResolveHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs b/src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs index a8a74fb455e12..a36bc0f6d91e9 100644 --- a/src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs +++ b/src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandler.cs @@ -52,7 +52,7 @@ public LSP.TextDocumentIdentifier GetTextDocumentIdentifier(LSP.CodeLens request // If the request is for an older version of the document, throw an exception so the client knows to re-query us. if (resolveData.SyntaxVersion != currentDocumentSyntaxVersion.ToString()) { - throw new LocalRpcException($"Resolve version {resolveData.SyntaxVersion} does not match current version {currentDocumentSyntaxVersion}") + throw new LocalRpcException($"Resolve version '{resolveData.SyntaxVersion}' does not match current version '{currentDocumentSyntaxVersion}'") { ErrorCode = LspErrorCodes.ContentModified };