Skip to content

Commit 636f069

Browse files
committed
Read newtonsoft file version, default to Load in event of error
1 parent a99cd6d commit 636f069

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/Microsoft.VisualStudio.Jdt/JdtUtilities.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Microsoft.VisualStudio.Jdt
55
{
66
using System;
7+
using System.Diagnostics;
78
using System.Reflection;
89
using Newtonsoft.Json.Linq;
910

@@ -17,6 +18,10 @@ public static class JdtUtilities
1718
/// </summary>
1819
internal const string JdtSyntaxPrefix = "@jdt.";
1920

21+
/// <summary>
22+
/// The cached line info handling to use, based on Newtonsoft.Json version
23+
/// https://github.com/JamesNK/Newtonsoft.Json/issues/1249
24+
/// </summary>
2025
private static LineInfoHandling? lineInfoHandling = null;
2126

2227
/// <summary>
@@ -53,15 +58,28 @@ internal static LineInfoHandling GetLineInfoHandling()
5358
{
5459
if (lineInfoHandling == null)
5560
{
56-
Version newtonsoftVersion = typeof(JObject).GetTypeInfo().Assembly.GetName().Version;
57-
58-
if (newtonsoftVersion >= new Version("10.0.2"))
61+
try
5962
{
60-
lineInfoHandling = LineInfoHandling.Load;
63+
string newtonsoftLocation = typeof(JObject).GetTypeInfo().Assembly.Location;
64+
FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(newtonsoftLocation);
65+
66+
// The version the line ending bug was fixed in. We want to target a lower
67+
// version of Newtonsoft to give consumers more flexibility in what version
68+
// they consume theirselves. However, we still want line endings to work in
69+
// both new and old versions.
70+
if (new Version(fileVersion.ProductVersion) < new Version("10.0.2"))
71+
{
72+
lineInfoHandling = LineInfoHandling.Ignore;
73+
}
74+
else
75+
{
76+
lineInfoHandling = LineInfoHandling.Load;
77+
}
6178
}
62-
else
79+
catch (Exception e) when (!e.IsCriticalException())
6380
{
64-
lineInfoHandling = LineInfoHandling.Ignore;
81+
// we will default to the "correct" value in the instance of any issues.
82+
lineInfoHandling = LineInfoHandling.Load;
6583
}
6684
}
6785

src/Microsoft.VisualStudio.Jdt/Microsoft.VisualStudio.Jdt.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
2525
</ItemGroup>
2626

27+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.5'">
28+
<PackageReference Include="System.Diagnostics.FileVersionInfo" Version="4.3.0" />
29+
</ItemGroup>
30+
2731
<ItemGroup>
2832
<Compile Update="Resources.Designer.cs">
2933
<DesignTime>True</DesignTime>
@@ -39,4 +43,4 @@
3943
</EmbeddedResource>
4044
</ItemGroup>
4145

42-
</Project>
46+
</Project>

0 commit comments

Comments
 (0)