NuGet restore of a multi-targeted project yields error MSB4018 "WriteRestoreGraphTask"

WP (Smokeball) 146 Reputation points
2021-11-03T05:37:28.66+00:00

We are going through the process of multi-targeting our projects, for the most part it works, things compile.

But for one project we've run into the following stack trace when running nuget restore on the project

MSBuild auto-detection: using msbuild version '17.0.0.45303' from 'C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\bin'.
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018: The "WriteRestoreGraphTask" task failed unexpectedly. [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018: System.NullReferenceException: Object reference not set to an instance of an object. [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at NuGet.Frameworks.AssetTargetFallbackFramework..ctor(NuGet.Frameworks1190085.NuGetFramework framework, IReadOnlyList`1 fallbackFrameworks) [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at NuGet.Commands.AssetTargetFallbackUtility.GetFallbackFramework(NuGet.Frameworks1190085.NuGetFramework projectFramework, IEnumerable`1 packageTargetFallback, IEnumerable`1 assetTargetFallback) [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at NuGet.Commands.AssetTargetFallbackUtility.ApplyFramework(TargetFrameworkInformation targetFrameworkInfo, IEnumerable`1 packageTargetFallback, IEnumerable`1 assetTargetFallback) [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at NuGet.Commands.MSBuildRestoreUtility.AddPackageTargetFallbacks(PackageSpec spec, IEnumerable`1 items) [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at NuGet.Commands.MSBuildRestoreUtility.GetPackageSpec(IEnumerable`1 items) [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at NuGet.Commands.MSBuildRestoreUtility.GetDependencySpec(IEnumerable`1 items) [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at NuGet.Build.Tasks.WriteRestoreGraphTask.Execute() [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]
C:\Users\wpang\AppData\Local\Temp\NuGetScratch\4wcxk1dx.nnr.nugetrestore.targets(160,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() [C:\Users\wpang\AppData\Local\Temp\NuGetScratch\npu0my22.xrd.nugetinputs.targets]

The project is upgraded to the new .NET SDK project style and then the TargetFrameworks are hand edited to include the following

<TargetFrameworks>net40;net5.0-windows</TargetFrameworks>

Not exactly sure why this error is occurring.

Does anyone have any ideas or pointers I can look at to try.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,357 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,929 questions
{count} vote

1 answer

Sort by: Most helpful
  1. David Broadfoot 6 Reputation points
    2022-05-24T23:12:23.44+00:00

    This error was on nuget 5.X. Upgrading to nuget 6.X seems to fix the issue

    1 person found this answer helpful.
    0 comments No comments