maca88 / AsyncGenerator

Generating async c# code using Roslyn
MIT License
47 stars 16 forks source link

Fails opening project with "Could not load file or assembly NuGet.ProjectModel" #150

Closed fredericDelaporte closed 4 years ago

fredericDelaporte commented 4 years ago

I have removed VS 2017 and installed VS 2019 today, and now the async generation of NHIbernate fails with this error log (some parts, put between {}, have been translated to English by me):

     [exec] Generating async code started
     [exec] Opening project 'E:\Projets\nhibernate\nhibernate-core\src\NHibernate\NHibernate.csproj' started
     [exec] System.InvalidOperationException: One or more errors occurred while opening the project:
     [exec] MSBuild {has failed processing the file} 'E:\Projets\nhibernate\nhibernate-core\src\NHibernate\NHibernate.csproj' {with message} : C:\Program Files\dotnet\sdk\3.1.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets: (234, 5): The "ResolvePackageAssets" task
failed unexpectedly.
     [exec] System.IO.FileNotFoundException: Could not load file or assembly 'NuGet.ProjectModel, Version=5.6.0.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. {The specified file is not found.}
     [exec] File name: 'NuGet.ProjectModel, Version=5.6.0.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35' ---> System.IO.FileNotFoundException: Could not load the specified file.
     [exec] File name: 'NuGet.ProjectModel'
     [exec]    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(AssemblyName assemblyName)
     [exec]    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
     [exec]    at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader.CreateReaderFromMemory(ResolvePackageAssets task, Byte[] settingsHash)
     [exec]    at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader..ctor(ResolvePackageAssets task)
     [exec]    at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ReadItemGroups()
     [exec]    at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ExecuteCore()
     [exec]    at Microsoft.NET.Build.Tasks.TaskBase.Execute()
     [exec]    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
     [exec]    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)
     [exec]
     [exec]
     [exec] MSBuild {has failed processing the file} 'E:\Projets\nhibernate\nhibernate-core\src\NHibernate\NHibernate.csproj' {with message} : C:\Program Files\dotnet\sdk\3.1.302\Roslyn\Microsoft.CSharp.Core.targets: (59, 5): The "Microsoft.CodeAnalysis.BuildTasks.Csc" task could not be loaded from
 the assembly C:\Program Files\dotnet\sdk\3.1.302\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll. Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. {The specified file is not found.} Confirm that the <UsingTask> declaration is correct, that
the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
     [exec] Hint: For suppressing irrelevant errors use SuppressDiagnosticFailures option.
     [exec]    at AsyncGenerator.AsyncCodeGenerator.CheckForErrors(MSBuildWorkspace workspace, String itemType, ImmutableArray`1 supressFailuresPredicates, ILogger logger)
     [exec]    at AsyncGenerator.AsyncCodeGenerator.OpenProject(MSBuildWorkspace workspace, String filePath, ImmutableArray`1 supressFailuresPredicates, ILogger logger, CancellationToken cancellationToken)
     [exec]    at AsyncGenerator.AsyncCodeGenerator.GenerateAsync(AsyncCodeConfiguration configuration, CancellationToken cancellationToken)
     [exec]    at AsyncGenerator.CommandLine.Program.Main(String[] args)

I have seen #146 about adding support for .Net Core 3.1. But I still have .Net Core 2.1 installed too. And attempting to remove 3.1 from the Visual Studio Installer asks for removing almost all of VS2019.

Is #146 required for being able to use the async generator when .Net Core 3.1 is installed? Or is there anything else to do to fix this trouble, like a way to instruct the generator to use the 2.1 SDK instead of 3.1?

maca88 commented 4 years ago

Or is there anything else to do to fix this trouble, like a way to instruct the generator to use the 2.1 SDK instead of 3.1?

you can instruct to use a lower SDK version by adding a global.json file in the root NHibernate folder. Mine contains:

{
  "sdk": {
    "version": "3.1.100"
  }
}
hazzik commented 4 years ago

I do the same. But I set 2.1.700.

fredericDelaporte commented 4 years ago

Ok, thanks. I have put 2.2.109, as I have 2.1.516 at most in 2.1 series, and it fixes the trouble. I have put this above the NHibernate root, to avoid having the file detected by git.

So I guess this issue will actually be fixed by #146. Closing.

We may commit the workaround into NHibernate, with something like:

{
  "sdk": {
    "version": "2.0.0",
    "rollForward": "latestMinor"
  }
}

But as the rollForward property is a 3.0 SDK thing, it will not work for those not having any 3.x SDK, unless they have precisely a 2.0.0 SDK. So for some people it may cause troubles instead of helping.