Incompatible versions of Mono MSBuild and .NET Core SDK (MSB4018)

Problem description

Some combinations of the Mono MSBuild and .NET Core SDK are incompatible on Linux and macOS.

If you use such combinations, it will be impossible to build your project in JetBrains Rider or the terminal.

Instead of a successful build, you will get an error like this:

Microsoft.NET.Sdk.targets(124, 5): [MSB4018] The "GenerateDepsFile" task failed unexpectedly.
System.TypeLoadException: Could not load type of field 'Microsoft.NET.Build.Tasks.DependencyContextBuilder:_filteredPackages' (13) due to: Could not resolve type with token 01000020 from typeref (expected class 'NuGet.Packaging.Core.PackageIdentity' in assembly 'NuGet.Packaging, Version=5.0.0.4, Culture=neutral, PublicKeyToken=31bf3856ad364e35') assembly:NuGet.Packaging, Version=5.0.0.4, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:NuGet.Packaging.Core.PackageIdentity member:(null)
at Microsoft.NET.Build.Tasks.TaskBase.Execute () [0x00000] in <9524e6fbf1724027a475d15b01fab866>:0
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x00029] in <7b80049ba212460da3a78bdfcc799ef4>:0
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask (Microsoft.Build.BackEnd.ITaskExecutionHost taskExecutionHost, Microsoft.Build.BackEnd.Logging.TaskLoggingContext taskLoggingContext, Microsoft.Build.BackEnd.TaskHost taskHost, Microsoft.Build.BackEnd.ItemBucket bucket, Microsoft.Build.BackEnd.TaskExecutionMode howToExecuteTask) [0x001f6] in <7b80049ba212460da3a78bdfcc799ef4>:0

or this

Microsoft.PackageDependencyResolution.targets(234, 5): [MSB4018] The "ResolvePackageAssets" task failed unexpectedly.
System.MissingMethodException: Method not found: System.Collections.Generic.IList`1<string> NuGet.ProjectModel.LockFileTargetLibrary.get_FrameworkReferences()
at Microsoft.NET.Build.Tasks.ResolvePackageAssets+CacheWriter.WriteItemGroup (System.Action writeItems) [0x0000e] in <465433aecc4e4125ae090de9fa6902e7>:0
at Microsoft.NET.Build.Tasks.ResolvePackageAssets+CacheWriter.WriteItemGroups () [0x0005a] in <465433aecc4e4125ae090de9fa6902e7>:0
at Microsoft.NET.Build.Tasks.ResolvePackageAssets+CacheWriter.Write () [0x00006] in <465433aecc4e4125ae090de9fa6902e7>:0

How to resolve the problem

There are three primary known ways to resolve this problem depending on your current environment and solution configuration. 

If your projects do not target the full .NET Framework (e.g., net46 or net472)

In case all of your projects do not target the full .NET Framework, for example, all of your csproj files contain the following:

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

you need to change MSBuild version in settings "File | Settings | Build, Execution, Deployment | Toolset and Build | Use MSBuild version" to MSBuild from .NET Core SDK (e.g., /usr/local/share/dotnet/sdk/2.2.100/MSBuild.dll).

Screenshot_2019-07-10_at_14.08.29.png

If you have at least one csproj file in your solution which targets .NET Framework like this

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup> 

please use one of the further described ways.

If you use mono 5.x and you want to continue using it

In case, you use mono 5.x and you want to continue using it, you need to change the .NET Core SDK version. It is recommended to use .NET Core SDK 2.1.5xx or 2.2.1xx (for example, 2.1.505 or 2.2.108). 

In order to check installed .NET Core SDK versions on your machine, you can use the following command:

dotnet --list-sdks

an output example:

2.2.100 [/usr/local/share/dotnet/sdk]
2.2.300 [/usr/local/share/dotnet/sdk]

If you discovered that you do not have a necessary version, follow the link and install SDK. If you do have compatible SDK on your machine, follow one of the options described below to change the currently used SDK version.

Option 1. Changing the .NET Core SDK version using global.json file

  • check that necessary .NET Core version is installed on your machine (as was described above)
  • in the root of your solution (or one of its parent directories) create a global.json file and specify the .NET Core version in it like this.
    {
    "sdk": {
    "version": "2.2.100"
    }
    }
     Please be sure that specifying .NET Core SDK version is installed on your machine. If you keep all of your solutions in the home folder you can put a global.json file into this folder.
  • restart Rider

Option 2. Uninstalling later versions

  • check that necessary .NET Core version is installed on your machine (as was described above).
  • remove all the versions which are higher than the necessary one. For example, if you have 2.2.100, 2.2.200, 2.2.300 installed you have to uninstall 2.2.200 and 2.2.300.
  • restart Rider

In order to check that now you are using the correct version of .NET Core SDK you can open the solution directory in the terminal and run the command:

dotnet --version

Please remember that .NET Core SDK 2.1.600+, 2.2.200+, and 3.0.100+ (including preview versions) are not compatible with mono 5.x.

If you use one of the latest versions of .NET Core SDK and you want to keep using it

In case you use one of the latest version of .NET Core SDK (2.1.600+, 2.2.200+, and 3.0.100+) and you want to keep using it, you should upgrade your mono according to the following list of compatible mono versions:

  • if you use mono 5.18.1.* you should upgrade it up to 5.18.1.24+
  • if you use mono 5.20.1.*you should upgrade it up to 5.20.1.34+
  • or you can install mono 6.0+

In order to check the currently installed mono version use the command

mono --version

If you have several mono versions installed you can select an MSBuild from the corresponding mono via the Rider settings "File | Settings | Build, Execution, Deployment | Toolset and Build | Use MSBuild version."

Screenshot_2019-07-10_at_16.29.32.png

Technical details

Below you can see the mapping from different .NET Core SDK versions to the corresponding embedded NuGet assembly versions.

  • 2.1.5xx: 4.9.0
  • 2.1.6xx: 5.0.0
  • 2.1.7xx: 5.1.0
  • 2.2.1xx: 4.9.x
  • 2.2.2xx: 5.0.0
  • 2.2.3xx: 5.1.0
  • 3.0.100-preview-009812: 5.0.0-preview.1
  • 3.0.100-preview-010184: 5.0.0-preview.1
  • 3.0.100-preview3-010431: 5.0.0-rtm
  • 3.0.100-preview4-011223: 5.1.0
  • 3.0.100-preview5-011568: 5.1.0
  • 3.0.100-preview6-012264: 5.2.0-preview2

And here you can see the mapping from different Mono versions to the corresponding embedded NuGet assembly versions which are included in MSBuild.

  • 5.12.0.226: 4.3
  • 5.12.0.260+: 4.7
  • 5.14+: 4.7
  • 5.16.0.179 - 5.16.0.221: 4.7
  • 5.16.0.235+: 4.8
  • 5.18.0.225 - 5.18.1.3: 4.8
  • 5.18.1.24+ : 5.0
  • 5.20.1.19: 4.8
  • 5.20.1.34+: 5.0
  • 6.0+: 5.0 

References

You can find more details here:

4 out of 8 found this helpful
5 comments
Avatar
Permanently deleted user

I'm using Linux with Mono 5.20 and dotnet 2.2 but I was receiving this error. What fixed my problem was to install the archived version (2.1.505) for dotnet.

 

 

 

1
Avatar
Permanently deleted user

Tried variants with DotNet version 2.2.100 and Mono version 5.20.1.34 also variant with preview Mono version 6. Neither works on my MacOs Mojave 10.14.5


0
Avatar
Permanently deleted user

Somehow it started working with DotNet version 2.1.605 and Mono version 5.20.1.34. May be it is connected with this https://docs.microsoft.com/en-us/visualstudio/mac/net-core-support?view=vsmac-2019

0

Hi there!

Thank you very much for bringing our attention to these details. Indeed, there were some patches in mono versions 5.20 and 5.18  which are compatible with 5.0 assemblies. We've double-checked it and fixed this article.

0
Avatar
Permanently deleted user

I am having the same issue on Rider 2019.2 on OSX while having Mono 6.0 (rider auto detects msbuild 15 as latest). I also get the same build error while using msbuild 16 in command line. None of the above solutions applies and can't fix the issue for me.

 

Edit: installing latest .Net Core SDK (v2.2.402) fixed the issue.

Edit: Note that If also want to build .net framework with Rider, need to use MSBuild version 15, and not 16, as it throws compile errors.

Edited by Permanently deleted user
0

Please sign in to leave a comment.

Have more questions?

Submit a request