Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 27551ab

Browse files
authored
Upgrade StreamJsonRpc library to 2.0 beta for error message modification (#303)
* try porting up to newest StreamJsonRpc * update to latest streamjsonrpc with param fixes, keep old serializer defaults, replace error data with stack trace * shift around initialization * format document * add rulesets to deal with transitive dependency analyzer * move ruleset to src, enable all but Async suffix message
1 parent 3c8cfb8 commit 27551ab

File tree

5 files changed

+138
-6
lines changed

5 files changed

+138
-6
lines changed

src/Analysis/Engine/Test/Microsoft.Python.Analysis.Engine.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
-->
1212
<NoWarn>1701;1702;1998;$(NoWarn)</NoWarn>
1313
</PropertyGroup>
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
15+
<CodeAnalysisRuleSet>..\..\..\PLS.ruleset</CodeAnalysisRuleSet>
16+
</PropertyGroup>
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
18+
<CodeAnalysisRuleSet>..\..\..\PLS.ruleset</CodeAnalysisRuleSet>
19+
</PropertyGroup>
1420
<Import Project="..\..\..\..\build\NetStandard.settings" />
1521
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
1622
<ItemGroup>

src/LanguageServer/Impl/LanguageServer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public sealed partial class LanguageServer : IDisposable {
4747
private ITelemetryService _telemetry;
4848

4949
private JsonRpc _rpc;
50+
private JsonSerializer _jsonSerializer;
5051
private bool _filesLoaded;
5152
private PathsWatcher _pathsWatcher;
5253
private IdleTimeTracker _idleTimeTracker;
@@ -59,6 +60,7 @@ public CancellationToken Start(IServiceContainer services, JsonRpc rpc) {
5960
_ui = services.GetService<IUIService>();
6061
_telemetry = services.GetService<ITelemetryService>();
6162
_rpc = rpc;
63+
_jsonSerializer = services.GetService<JsonSerializer>();
6264

6365
var progress = services.GetService<IProgressService>();
6466

@@ -381,7 +383,7 @@ public Task LoadExtension(JToken token, CancellationToken cancellationToken)
381383

382384
#endregion
383385

384-
private T ToObject<T>(JToken token) => token.ToObject<T>(_rpc.JsonSerializer);
386+
private T ToObject<T>(JToken token) => token.ToObject<T>(_jsonSerializer);
385387

386388
private T GetSetting<T>(JToken section, string settingName, T defaultValue) {
387389
var value = section?[settingName];

src/LanguageServer/Impl/Microsoft.Python.LanguageServer.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
<DebugType>portable</DebugType>
1212
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
1313
</PropertyGroup>
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
15+
<CodeAnalysisRuleSet>..\..\PLS.ruleset</CodeAnalysisRuleSet>
16+
</PropertyGroup>
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
18+
<CodeAnalysisRuleSet>..\..\PLS.ruleset</CodeAnalysisRuleSet>
19+
</PropertyGroup>
1420
<ItemGroup>
1521
<Compile Remove="obj\**" />
1622
<EmbeddedResource Remove="obj\**" />
@@ -22,8 +28,8 @@
2228
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2329
</PackageReference>
2430
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="2.1.0" />
25-
<PackageReference Include="NewtonSoft.Json" Version="10.0.1" />
26-
<PackageReference Include="StreamJsonRpc" Version="1.4.121" />
31+
<PackageReference Include="NewtonSoft.Json" Version="11.0.2" />
32+
<PackageReference Include="StreamJsonRpc" Version="2.0.102-beta" />
2733
</ItemGroup>
2834
<ItemGroup Condition="$(AnalysisReference) == ''">
2935
<ProjectReference Include="../../Analysis/Engine/Impl/Microsoft.Python.Analysis.Engine.csproj" />

src/LanguageServer/Impl/Program.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
// #define WAIT_FOR_DEBUGGER
1818

19-
using System;
2019
using Microsoft.Python.LanguageServer.Services;
2120
using Microsoft.PythonTools.Analysis.Infrastructure;
2221
using Newtonsoft.Json;
2322
using StreamJsonRpc;
23+
using StreamJsonRpc.Protocol;
24+
using System;
25+
using System.IO;
2426

2527
namespace Microsoft.Python.LanguageServer.Server {
2628
internal static class Program {
@@ -29,16 +31,23 @@ public static void Main(string[] args) {
2931
using (CoreShell.Create()) {
3032
var services = CoreShell.Current.ServiceManager;
3133

34+
var messageFormatter = new JsonMessageFormatter();
35+
// StreamJsonRpc v1.4 serializer defaults
36+
messageFormatter.JsonSerializer.NullValueHandling = NullValueHandling.Ignore;
37+
messageFormatter.JsonSerializer.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
38+
messageFormatter.JsonSerializer.Converters.Add(new UriConverter());
39+
3240
using (var cin = Console.OpenStandardInput())
3341
using (var cout = Console.OpenStandardOutput())
3442
using (var server = new Implementation.LanguageServer())
35-
using (var rpc = new JsonRpc(cout, cin, server)) {
43+
using (var rpc = new LanguageServerJsonRpc(cout, cin, messageFormatter, server)) {
3644
rpc.SynchronizationContext = new SingleThreadSynchronizationContext();
37-
rpc.JsonSerializer.Converters.Add(new UriConverter());
3845

3946
services.AddService(new UIService(rpc));
4047
services.AddService(new ProgressService(rpc));
4148
services.AddService(new TelemetryService(rpc));
49+
services.AddService(messageFormatter.JsonSerializer);
50+
4251
var token = server.Start(services, rpc);
4352
rpc.StartListening();
4453

@@ -59,10 +68,26 @@ private static void CheckDebugMode() {
5968
}
6069
#endif
6170
}
71+
72+
private class LanguageServerJsonRpc : JsonRpc {
73+
public LanguageServerJsonRpc(Stream sendingStream, Stream receivingStream, IJsonRpcMessageFormatter formatter, object target)
74+
: base(new HeaderDelimitedMessageHandler(sendingStream, receivingStream, formatter), target) { }
75+
76+
protected override JsonRpcError.ErrorDetail CreateErrorDetails(JsonRpcRequest request, Exception exception) {
77+
var localRpcEx = exception as LocalRpcException;
78+
79+
return new JsonRpcError.ErrorDetail {
80+
Code = (JsonRpcErrorCode?)localRpcEx?.ErrorCode ?? JsonRpcErrorCode.InvocationError,
81+
Message = exception.Message,
82+
Data = exception.StackTrace,
83+
};
84+
}
85+
}
6286
}
6387

6488
sealed class UriConverter : JsonConverter {
6589
public override bool CanConvert(Type objectType) => objectType == typeof(Uri);
90+
6691
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
6792
if (reader.TokenType == JsonToken.String) {
6893
var str = (string)reader.Value;
@@ -75,6 +100,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
75100

76101
throw new InvalidOperationException($"UriConverter: unsupported token type {reader.TokenType}");
77102
}
103+
78104
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
79105
if (null == value) {
80106
writer.WriteNull();

src/PLS.ruleset

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RuleSet Name="Microsoft Managed Recommended Rules" Description="These rules focus on the most critical problems in your code, including potential security holes, application crashes, and other important logic and design errors. It is recommended to include this rule set in any custom rule set you create for your projects." ToolsVersion="10.0">
3+
<Localization ResourceAssembly="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.dll" ResourceBaseName="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.Localized">
4+
<Name Resource="MinimumRecommendedRules_Name" />
5+
<Description Resource="MinimumRecommendedRules_Description" />
6+
</Localization>
7+
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
8+
<Rule Id="CA1001" Action="Warning" />
9+
<Rule Id="CA1009" Action="Warning" />
10+
<Rule Id="CA1016" Action="Warning" />
11+
<Rule Id="CA1033" Action="Warning" />
12+
<Rule Id="CA1049" Action="Warning" />
13+
<Rule Id="CA1060" Action="Warning" />
14+
<Rule Id="CA1061" Action="Warning" />
15+
<Rule Id="CA1063" Action="Warning" />
16+
<Rule Id="CA1065" Action="Warning" />
17+
<Rule Id="CA1301" Action="Warning" />
18+
<Rule Id="CA1400" Action="Warning" />
19+
<Rule Id="CA1401" Action="Warning" />
20+
<Rule Id="CA1403" Action="Warning" />
21+
<Rule Id="CA1404" Action="Warning" />
22+
<Rule Id="CA1405" Action="Warning" />
23+
<Rule Id="CA1410" Action="Warning" />
24+
<Rule Id="CA1415" Action="Warning" />
25+
<Rule Id="CA1821" Action="Warning" />
26+
<Rule Id="CA1900" Action="Warning" />
27+
<Rule Id="CA1901" Action="Warning" />
28+
<Rule Id="CA2002" Action="Warning" />
29+
<Rule Id="CA2100" Action="Warning" />
30+
<Rule Id="CA2101" Action="Warning" />
31+
<Rule Id="CA2108" Action="Warning" />
32+
<Rule Id="CA2111" Action="Warning" />
33+
<Rule Id="CA2112" Action="Warning" />
34+
<Rule Id="CA2114" Action="Warning" />
35+
<Rule Id="CA2116" Action="Warning" />
36+
<Rule Id="CA2117" Action="Warning" />
37+
<Rule Id="CA2122" Action="Warning" />
38+
<Rule Id="CA2123" Action="Warning" />
39+
<Rule Id="CA2124" Action="Warning" />
40+
<Rule Id="CA2126" Action="Warning" />
41+
<Rule Id="CA2131" Action="Warning" />
42+
<Rule Id="CA2132" Action="Warning" />
43+
<Rule Id="CA2133" Action="Warning" />
44+
<Rule Id="CA2134" Action="Warning" />
45+
<Rule Id="CA2137" Action="Warning" />
46+
<Rule Id="CA2138" Action="Warning" />
47+
<Rule Id="CA2140" Action="Warning" />
48+
<Rule Id="CA2141" Action="Warning" />
49+
<Rule Id="CA2146" Action="Warning" />
50+
<Rule Id="CA2147" Action="Warning" />
51+
<Rule Id="CA2149" Action="Warning" />
52+
<Rule Id="CA2200" Action="Warning" />
53+
<Rule Id="CA2202" Action="Warning" />
54+
<Rule Id="CA2207" Action="Warning" />
55+
<Rule Id="CA2212" Action="Warning" />
56+
<Rule Id="CA2213" Action="Warning" />
57+
<Rule Id="CA2214" Action="Warning" />
58+
<Rule Id="CA2216" Action="Warning" />
59+
<Rule Id="CA2220" Action="Warning" />
60+
<Rule Id="CA2229" Action="Warning" />
61+
<Rule Id="CA2231" Action="Warning" />
62+
<Rule Id="CA2232" Action="Warning" />
63+
<Rule Id="CA2235" Action="Warning" />
64+
<Rule Id="CA2236" Action="Warning" />
65+
<Rule Id="CA2237" Action="Warning" />
66+
<Rule Id="CA2238" Action="Warning" />
67+
<Rule Id="CA2240" Action="Warning" />
68+
<Rule Id="CA2241" Action="Warning" />
69+
<Rule Id="CA2242" Action="Warning" />
70+
</Rules>
71+
<Rules AnalyzerId="Microsoft.VisualStudio.Threading.Analyzers" RuleNamespace="Microsoft.VisualStudio.Threading.Analyzers">
72+
<Rule Id="VSTHRD001" Action="Info" />
73+
<Rule Id="VSTHRD002" Action="Info" />
74+
<Rule Id="VSTHRD003" Action="Info" />
75+
<Rule Id="VSTHRD004" Action="Info" />
76+
<Rule Id="VSTHRD010" Action="Info" />
77+
<Rule Id="VSTHRD011" Action="Info" />
78+
<Rule Id="VSTHRD012" Action="Info" />
79+
<Rule Id="VSTHRD100" Action="Info" />
80+
<Rule Id="VSTHRD101" Action="Info" />
81+
<Rule Id="VSTHRD102" Action="Info" />
82+
<Rule Id="VSTHRD103" Action="Info" />
83+
<Rule Id="VSTHRD104" Action="Info" />
84+
<Rule Id="VSTHRD105" Action="Info" />
85+
<Rule Id="VSTHRD106" Action="Info" />
86+
<Rule Id="VSTHRD107" Action="Info" />
87+
<Rule Id="VSTHRD108" Action="Info" />
88+
<Rule Id="VSTHRD109" Action="Info" />
89+
<Rule Id="VSTHRD110" Action="Info" />
90+
<Rule Id="VSTHRD200" Action="None" />
91+
</Rules>
92+
</RuleSet>

0 commit comments

Comments
 (0)