Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use System.Text.Json in Netfx build of MSBuildSdkResolver #39573

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

<Nullable>Enable</Nullable>

<UseSystemTextJson Condition="'$(TargetFramework)'!='netstandard2.0' And '$(TargetFramework)'!='net472'">True</UseSystemTextJson>
<UseSystemTextJson Condition="'$(TargetFramework)'!='netstandard2.0'">True</UseSystemTextJson>
<DefineConstants Condition="'$(UseSystemTextJson)'=='True'">$(DefineConstants);USE_SYSTEM_TEXT_JSON</DefineConstants>

<!-- Netfx version of the resolver builds against the lowest version of System.Text.Json that's guaranteed to be shipped with MSBuild in VS -->
<SystemTextJsonVersionOverride>8.0.0</SystemTextJsonVersionOverride>

<!-- Create FileDefinitions items for ResolveHostfxrCopyLocalContent target -->
<EmitLegacyAssetsFileItems>true</EmitLegacyAssetsFileItems>

Expand Down Expand Up @@ -100,7 +103,8 @@
</ItemGroup>

<ItemGroup>
<!-- No PackageReference to System.Text.Json necessary, because it's included in .NET 5.0 and higher -->
<!-- No PackageReference to System.Text.Json necessary when targeting .NET, because it's included in .NET 5.0 and higher -->
<PackageReference Include="System.Text.Json" VersionOverride="$(SystemTextJsonVersionOverride)" Condition="'$(UseSystemTextJson)'=='True' and '$(TargetFramework)'=='net472'"/>
<PackageReference Include="Newtonsoft.Json" Condition="'$(UseSystemTextJson)'!='True'"/>

<!-- Reference this package to avoid package downgrade errors. See https://github.com/dotnet/sdk/issues/3044 for details -->
Expand Down Expand Up @@ -139,13 +143,16 @@

<ItemGroup>
<ExpectedDependencies Include="Microsoft.Deployment.DotNet.Releases, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<ExpectedDependencies Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" />
<ExpectedDependencies Include="System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
<ExpectedDependencies Include="System.Text.Encodings.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
Comment on lines +146 to +147
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering why you specify System.Text.Json 8.0.0.0 unconditionally here even though 9.0.0.0 should be used on .NET 9. But apparently these items are in a target that runs only when targeting .NET Framework, so it's fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, the target runs only when building the .NET Framework version of the resolver.

<ExpectedDependencies Include="Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<ExpectedDependencies Include="System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<ExpectedDependencies Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
<ExpectedDependencies Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<ExpectedDependencies Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<ExpectedDependencies Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
<ExpectedDependencies Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
<ExpectedDependencies Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
</ItemGroup>

<!-- Check that the dependencies of the output assembly match our expectations -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

#if USE_SYSTEM_TEXT_JSON

using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;

namespace Microsoft.NET.Sdk.WorkloadManifestReader
Expand Down Expand Up @@ -47,17 +44,17 @@ internal ref struct Utf8JsonStreamReader
Utf8JsonReader reader;
readonly Stream stream;

IMemoryOwner<byte> buffer;
byte[]? buffer;

Span<byte> span;

public Utf8JsonStreamReader(Stream stream, JsonReaderOptions readerOptions)
{
this.stream = stream;

buffer = MemoryPool<byte>.Shared.Rent(segmentSize);
var readCount = stream.Read(buffer.Memory.Span);
span = buffer.Memory.Slice(0, readCount).Span;
buffer = ArrayPool<byte>.Shared.Rent(segmentSize);
var readCount = stream.Read(buffer, 0, buffer.Length);
span = buffer.AsSpan().Slice(0, readCount);

if (span.StartsWith(utf8Bom))
{
Expand All @@ -73,6 +70,11 @@ public bool Read()
{
if (reader.IsFinalBlock)
{
if (buffer != null)
{
ArrayPool<byte>.Shared.Return(buffer);
buffer = null;
}
return false;
}

Expand All @@ -86,18 +88,21 @@ public bool Read()

int remaining = (int)(span.Length - reader.BytesConsumed);

var newBuffer = MemoryPool<byte>.Shared.Rent(newSegmentSize);
var newBuffer = ArrayPool<byte>.Shared.Rent(newSegmentSize);

if (remaining > 0)
{
span.Slice((int)reader.BytesConsumed).CopyTo(newBuffer.Memory.Span);
span.Slice((int)reader.BytesConsumed).CopyTo(newBuffer);
}

var readCount = stream.Read(newBuffer.Memory.Span.Slice(remaining));
var readCount = stream.Read(newBuffer, remaining, newBuffer.Length - remaining);

buffer.Dispose();
if (buffer != null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
buffer = newBuffer;
span = newBuffer.Memory.Slice(0, remaining + readCount).Span;
span = newBuffer.AsSpan().Slice(0, remaining + readCount);

reader = new Utf8JsonReader(span, stream.Position >= stream.Length, reader.CurrentState);
}
Expand Down
Loading