-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
RoslynCodeTaskFactory fails to load System.Text.Json #5737
Comments
At the moment this is by design. The RoslynCodeTaskFactory compiles your code against .NETStandard2.0 for maximum compatibility. .NET Standard 2.0 is supported on .NET Framework v4.7.2+ and .NET Core 2.0+. If RoslynCodeTaskFactory compiled your task against .NET Core 3.0 so it could use System.Text.Json, the task would fail to run on .NET Framework MSBuild ( This could get your code base into a state where it builds for you but not on someone else's machines with a different runtime. https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support
Even if you compiled an actual task assembly against For example: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="16.6.0" ExcludeAssets="Runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.6.0" ExcludeAssets="Runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.6.0" ExcludeAssets="Runtime" />
<PackageReference Include="System.Text.Json" Version="4.7.2" ExcludeAssets="Runtime" />
</ItemGroup>
</Project> This fails to restore even since using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Text.Json;
namespace netstandard21tasklibrary
{
public class MyTask : Task
{
public override bool Execute()
{
JsonDocument document = JsonDocument.Parse("{\"Foo\": \"Bar\"}");
foreach (JsonProperty jsonProperty in document.RootElement.EnumerateObject())
{
Log.LogMessage(MessageImportance.High, $"{jsonProperty.Name}: {jsonProperty.Value}");
}
return !Log.HasLoggedErrors;
}
}
} C:\>dotnet msbuild netstandard21task.proj
Microsoft (R) Build Engine version 16.8.0-preview-20451-02+51a1071f8 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Foo: Bar It fails under .NET Framework MSBuild.exe since that only targets .NET Framework v4.7.2:
So until .NET Framework has the necessary assemblies for .NET Standard 2.1, we can't update RoslynCodeTaskFactory to compile task assemblies against anything but .NET Standard 2.0. The only workaround is to compile your task assembly and ship System.Text.Json in a package along side it. This will make MSBuild load System.Text.Json when loading your task. |
Ah right. Thanks for the very detailed explanation. |
Issue Description
RoslynCodeTaskFactory fails to load System.Text.Json.
From my understanding, System.Text.Json is part of the .net core shared framework. Using the RoslynCodeTaskFactory with System.Text.Json should just work out of the box. This would enable a custom build step to output a valid JSON file.
I have .net Core 3.1.302 sdk installed, along with msbuild 16.6
Steps to reproduce
Create a custom inline task which attempts to use System.Text.Json serializer.
dotnet new console --language c# -o csprojthrowaway
.csproj
filedotnet build -t:rebuild -v minimal .\csprojthrowaway.csproj
Expected behaviour
msbuild should correctly compile the auto generated csharp, execute the .cs file and return a JSON string.
Actual behaviour
msbuild reports error, Json namespace cannot be resolved:
If we attempt to add reference
<Reference Include="System.Text.Json" />
results in a different error:If we attempt to add reference
<Reference Include="System.Runtime" />
we reach the end of the line:Background context
MSDN Documentation for MSBuild states
MSDN Documentation for System.Text.Json states
Version information
Binlog
msbuild.binlog.zip
The text was updated successfully, but these errors were encountered: