From 6c27a03a00364906ea8c6f3833725da083e8c241 Mon Sep 17 00:00:00 2001 From: Jimmy Byrd Date: Sat, 28 Oct 2023 14:47:34 -0400 Subject: [PATCH] 1186 json serializer crash server (#1189) * Update LSP library * Port JsonSerializationException for JsonRpc overrides --- paket.dependencies | 2 +- paket.lock | 2 +- .../LspServers/AdaptiveFSharpLspServer.fs | 35 ++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/paket.dependencies b/paket.dependencies index 4de863338..d41372b11 100644 --- a/paket.dependencies +++ b/paket.dependencies @@ -53,7 +53,7 @@ nuget Expecto.Diff nuget YoloDev.Expecto.TestSdk nuget AltCover nuget GitHubActionsTestLogger -nuget Ionide.LanguageServerProtocol >= 0.4.18 +nuget Ionide.LanguageServerProtocol >= 0.4.19 nuget Microsoft.Extensions.Caching.Memory nuget OpenTelemetry.Api >= 1.3.2 nuget OpenTelemetry.Exporter.OpenTelemetryProtocol >= 1.3.2 # 1.4 bumps to 7.0 versions of System.Diagnostics libs, so can't use it diff --git a/paket.lock b/paket.lock index 6b4bab215..dca10a3a2 100644 --- a/paket.lock +++ b/paket.lock @@ -130,7 +130,7 @@ NUGET System.Collections.Immutable (>= 5.0) System.Reflection.Metadata (>= 5.0) Ionide.KeepAChangelog.Tasks (0.1.8) - copy_local: true - Ionide.LanguageServerProtocol (0.4.18) + Ionide.LanguageServerProtocol (0.4.19) FSharp.Core (>= 6.0) Newtonsoft.Json (>= 13.0.1) StreamJsonRpc (>= 2.10.44) diff --git a/src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs b/src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs index 170cff38f..ebba809ec 100644 --- a/src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs +++ b/src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs @@ -3098,6 +3098,7 @@ module AdaptiveFSharpLspServer = | :? LocalRpcException -> Some() | :? TaskCanceledException -> Some() | :? OperationCanceledException -> Some() + | :? Newtonsoft.Json.JsonSerializationException -> Some() | :? System.AggregateException as aex -> if aex.InnerExceptions.Count = 1 then (|HandleableException|_|) aex.InnerException @@ -3107,11 +3108,43 @@ module AdaptiveFSharpLspServer = let strategy = StreamJsonRpcTracingStrategy(Tracing.fsacActivitySource) + let (|Flatten|_|) (e: exn) = + match e with + | :? AggregateException as aex -> + let aex = aex.Flatten() + + if aex.InnerExceptions.Count = 1 then + Some aex.InnerException + else + Some e + | _ -> Some e + + let strategy = StreamJsonRpcTracingStrategy(Tracing.fsacActivitySource) + { new JsonRpc(handler, ActivityTracingStrategy = strategy) with member this.IsFatalException(ex: Exception) = match ex with | HandleableException -> false - | _ -> true } + | _ -> true + + member this.CreateErrorDetails(request: Protocol.JsonRpcRequest, ex: Exception) = + let isSerializable = this.ExceptionStrategy = ExceptionProcessing.ISerializable + + match ex with + | Flatten(:? Newtonsoft.Json.JsonSerializationException as ex) -> + + let data: obj = if isSerializable then ex else Protocol.CommonErrorData(ex) + + Protocol.JsonRpcError.ErrorDetail( + Code = Protocol.JsonRpcErrorCode.ParseError, + Message = ex.Message, + Data = data + ) + | _ -> base.CreateErrorDetails(request, ex) + + } + + let startCore toolsPath workspaceLoaderFactory sourceTextFactory = use input = Console.OpenStandardInput()