maca88 / AsyncGenerator

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

Does not work on .NET 6 #157

Closed hazzik closed 2 years ago

hazzik commented 2 years ago
     [exec] Starting 'dotnet (async-generator)' in 'C:\Projects\Private\nhibernate-core\src'
     [exec] AsyncGenerator
     [exec] 
     [exec] Generating async code started
     [exec] System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
     [exec] Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec]    at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
     [exec]    at System.Reflection.RuntimeAssembly.get_DefinedTypes()
     [exec]    at System.Composition.Hosting.ContainerConfiguration.<>c.<WithAssemblies>b__16_0(Assembly a)
     [exec]    at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
     [exec]    at System.Composition.TypedParts.TypedPartExportDescriptorProvider..ctor(IEnumerable`1 types, AttributedModelProvider attributeContext)
     [exec]    at System.Composition.Hosting.ContainerConfiguration.CreateContainer()
     [exec]    at Microsoft.CodeAnalysis.Host.Mef.MefHostServices.Create(IEnumerable`1 assemblies)
     [exec]    at Microsoft.CodeAnalysis.Host.Mef.MSBuildMefHostServices.get_DefaultServices()
     [exec]    at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create(IDictionary`2 properties)
     [exec]    at AsyncGenerator.AsyncCodeGenerator.CreateWorkspace(String targetFramework) in C:\Workspace\Git\AsyncGenerator\Source\AsyncGenerator\AsyncCodeGenerator.cs:line 108
     [exec]    at AsyncGenerator.AsyncCodeGenerator.GenerateAsync(AsyncCodeConfiguration configuration, CancellationToken cancellationToken) in C:\Workspace\Git\AsyncGenerator\Source\AsyncGenerator\AsyncCodeGenerator.cs:line 76
     [exec]    at AsyncGenerator.CommandLine.Program.Main(String[] args) in C:\Workspace\Git\AsyncGenerator\Source\AsyncGenerator.CommandLine\Program.cs:line 59
     [exec] System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
     [exec] System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
     [exec] System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
     [exec] System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
     [exec] System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
     [exec] System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
     [exec] File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
maca88 commented 2 years ago

Published version 0.20.0 which works also with .NET 6.

hazzik commented 2 years ago

Having some issues converting NHibernate.Tests:

 [exec] info: AsyncCodeGenerator[0]
 [exec]       Opening project 'C:\Projects\Private\nhibernate-core\src\NHibernate.Test\NHibernate.Test.csproj' started
 [exec] warn: AsyncCodeGenerator[0]
 [exec]       One or more warnings occurred while opening the project:
 [exec]       Found project reference without a matching metadata reference: C:\Projects\Private\nhibernate-core\src\NHibernate\NHibernate.csproj
 [exec]       Found project reference without a matching metadata reference: C:\Projects\Private\nhibernate-core\src\NHibernate.DomainModel\NHibernate.DomainModel.csproj
 [exec]
 [exec] info: AsyncCodeGenerator[0]
 [exec]       Opening project 'C:\Projects\Private\nhibernate-core\src\NHibernate.Test\NHibernate.Test.csproj' completed
 [exec] info: AsyncCodeGenerator[0]
 [exec]       Configuring project prior analyzation started
 [exec] info: AsyncCodeGenerator[0]
 [exec]       Configuring project prior analyzation completed
 [exec] info: AsyncCodeGenerator[0]
 [exec]       Compiling project 'NHibernate.Test' started
 [exec] info: AsyncCodeGenerator[0]
 [exec]       Compiling project 'NHibernate.Test' completed
 [exec] info: AsyncCodeGenerator[0]
 [exec]       Initializing registered plugins for project 'NHibernate.Test' started
 [exec] crit: AsyncGenerator.CommandLine[0]
 [exec]       Unable to find NUnit.Framework.Assert type
 [exec]       System.InvalidOperationException: Unable to find NUnit.Framework.Assert type
 [exec]          at AsyncGenerator.Core.Plugins.NUnitPlugin.Initialize(Project project, IProjectConfiguration configuration, Compilation compilation) in C:\Workspace\Git\AsyncGenerator\Source\AsyncGenerator.Core\Plugins\NUnitPlugin.cs:line 61
 [exec]          at AsyncGenerator.AsyncCodeGenerator.GenerateProject(ProjectData projectData, ILoggerFactory loggerFactory, ILogger logger, CancellationToken cancellationToken) in C:\Workspace\Git\AsyncGenerator\Source\AsyncGenerator\AsyncCodeGenerator.cs:line 0
 [exec]          at AsyncGenerator.AsyncCodeGenerator.GenerateAsync(AsyncCodeConfiguration configuration, CancellationToken cancellationToken) in C:\Workspace\Git\AsyncGenerator\Source\AsyncGenerator\AsyncCodeGenerator.cs:line 84
 [exec]          at AsyncGenerator.CommandLine.Program.Main(String[] args) in C:\Workspace\Git\AsyncGenerator\Source\AsyncGenerator.CommandLine\Program.cs:line 51
maca88 commented 2 years ago

I am able to get this error only on nhibernate/nhibernate-core/pull/2951 pull request. By removing netcoreapp2.0 from the NHibernate test project, the targetFramework inside AsyncGenerator.yml for the test project needs to be changed to either net6.0 or net461. By changing it to net6.0, I noticed that there is a bug with Assert.That that I will fix tomorrow, for ignoring .NET 6 only methods, the following configuration can be added to the test project configuration:

    ignoreSearchForAsyncCounterparts:
    - name: Wait
      containingTypeName: Task
    - name: Prepare
      containingTypeName: DbCommand
    - name: Close
      containingTypeName: DbConnection
    - name: Close
      containingTypeName: DbDataReader
hazzik commented 2 years ago

Thanks

maca88 commented 2 years ago

With the newly released version 0.20.1 the mentioned bug for Assert.That method is fixed.

bahusoid commented 2 years ago

Hm.. Installing .NET 6 breaks async generation. I see the same exception as hazzik for master branch. So for master it seems 0.20.1 will solve the problem. What about 5.3 branch? Any workarounds? Trick with global.json no longer works for me with .NET 6 installed

maca88 commented 2 years ago

Yea you are right, for some reason global.json does not work for lower .NET Core versions anymore, even when using "rollForward": "disable". I was not able to find any workaround yet. For now, the easiest way to fix the issue would be to port nhibernate/nhibernate-core/pull/2605 in 5.3.x branch. I tried to run 0.20.1 version on 5.3.x branch and it does not produce any code changes.

bahusoid commented 2 years ago

Alternatively you can publish 0.18.3 version with MsBuildLocator restricted to .NET Core 2.1. So instead of:

https://github.com/maca88/AsyncGenerator/blob/110c0a1aa5d045f1a88d084ff2e49dfc500617ef/Source/AsyncGenerator/Internal/EnvironmentHelper.cs#L84-L87

Use something like:

            var instance = MSBuildLocator.QueryVisualStudioInstances()
#if NETCOREAPP2_1
                .Where(o => o.Version.Major == 2)
#endif
                .OrderByDescending(o => o.Version)
                .FirstOrDefault();
maca88 commented 2 years ago

In Windows QueryVisualStudioInstances() will return the installed Visual Studios, in my case I get two instances, one for VS2019 and one for VS2022. If VS2019 is selected, then the global.json works and so the code generation. So basically this means that if a developer has only VS2022 installed, it won't work. Based on that, I still think that porting the mentioned PR is a better alternative, but if you disagree I can publish a new version with the proposed logic that would work also on Windows (if VS2019 is installed).

bahusoid commented 2 years ago

In Windows QueryVisualStudioInstances() will return the installed Visual Studios

Did you run it under .NET Core or NetFx? It seems true for NetFx only.

Here is example I run under .NET 6 on Windows with latest Microsoft.Build.Locator 1.4.1:

using Microsoft.Build.Locator;

var instance = MSBuildLocator.QueryVisualStudioInstances()
    //.Where(o => o.Version.Major == 2)
    .OrderByDescending(o => o.Version)
    .ToList();

foreach (var i in instance)
{
    Console.WriteLine(i.Version);
    Console.WriteLine(i.MSBuildPath);
}    

Output I see lists all .NET Core SDKs:

6.0.100
C:\Program Files\dotnet\sdk\6.0.100\
5.0.201
C:\Program Files\dotnet\sdk\5.0.201\
2.1.602
C:\Program Files\dotnet\sdk\2.1.602\
2.1.601
C:\Program Files\dotnet\sdk\2.1.601\
2.1.504
C:\Program Files\dotnet\sdk\2.1.504\
2.1.503
C:\Program Files\dotnet\sdk\2.1.503\
2.1.502
C:\Program Files\dotnet\sdk\2.1.502\
2.1.500
C:\Program Files\dotnet\sdk\2.1.500\
2.1.202
C:\Program Files\dotnet\sdk\2.1.202\
2.1.201
C:\Program Files\dotnet\sdk\2.1.201\

P.S. I have only VS 2019 installed.

maca88 commented 2 years ago

Did you run it under .NET Core or NetFx?

My bad, I didn't noticed that I had net472 for the target framework.

I published version 0.18.3 which avoids selecting not supported MSBuild versions.

bahusoid commented 2 years ago

Thanks!