44namespace 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
0 commit comments