From 41081a3dc49c89cb0ae443a0f7c416cce1353649 Mon Sep 17 00:00:00 2001 From: Vitek Karas Date: Tue, 30 Jul 2019 14:23:43 -0700 Subject: [PATCH] Fix how we determine line/column from a JsonReaderState (dotnet/core-setup#7510) Previously these fields were public and so simple `.GetField("name")` worked. Now they are internal, so we need to use the overload which takes `BindingFlags` and ask for private reflection. Commit migrated from https://github.com/dotnet/core-setup/commit/907f46239dd63de667797504fdb7fadbf8424a37 --- .../UnifiedJsonReader.Utf8JsonReader.cs | 5 +++-- .../DependencyContextJsonReaderTest.cs | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/installer/managed/Microsoft.Extensions.DependencyModel/UnifiedJsonReader.Utf8JsonReader.cs b/src/installer/managed/Microsoft.Extensions.DependencyModel/UnifiedJsonReader.Utf8JsonReader.cs index 961706a..0c3e3ef 100644 --- a/src/installer/managed/Microsoft.Extensions.DependencyModel/UnifiedJsonReader.Utf8JsonReader.cs +++ b/src/installer/managed/Microsoft.Extensions.DependencyModel/UnifiedJsonReader.Utf8JsonReader.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Reflection; using System.Text.Json; namespace Microsoft.Extensions.DependencyModel @@ -148,8 +149,8 @@ namespace Microsoft.Extensions.DependencyModel { // Replace with public API once https://github.com/dotnet/corefx/issues/34768 is fixed object boxedState = reader.CurrentState; - long lineNumber = (long)(typeof(JsonReaderState).GetField("_lineNumber")?.GetValue(boxedState) ?? -1); - long bytePositionInLine = (long)(typeof(JsonReaderState).GetField("_bytePositionInLine")?.GetValue(boxedState) ?? -1); + long lineNumber = (long)(typeof(JsonReaderState).GetField("_lineNumber", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(boxedState) ?? -1); + long bytePositionInLine = (long)(typeof(JsonReaderState).GetField("_bytePositionInLine", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(boxedState) ?? -1); return new FormatException($"Unexpected character encountered, excepted '{expected}' " + $"at line {lineNumber} position {bytePositionInLine}"); diff --git a/src/installer/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/src/installer/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs index 8c5c5fa..3bc0c8b 100644 --- a/src/installer/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs +++ b/src/installer/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -698,6 +698,20 @@ namespace Microsoft.Extensions.DependencyModel.Tests } [Fact] + public void FailsToReadInvalidDefines() + { + Assert.Throws(() => Read( +@"{ + ""compilationOptions"": { + ""defines"": ""MY"", + }, + ""targets"": { + "".NETCoreApp,Version=v1.0/osx.10.10-x64"": {} + } +}")).Message.Should().Contain("line 2 position 23"); + } + + [Fact] public void IgnoresUnknownPropertiesInCompilationOptions() { var context = Read( -- 2.7.4