dotnet / orleans

Cloud Native application framework for .NET
https://docs.microsoft.com/dotnet/orleans
MIT License
10.07k stars 2.03k forks source link

code generation fails with dotnet 2.1 rc1 #4585

Closed gamemachine closed 6 years ago

gamemachine commented 6 years ago

This is a console app.

Orleans-CodeGen - Generating file C:\Games\aigame\Game\Server\obj\Debug\netcoreapp2.1\Server.orleans.g.cs 1>-- Code Generation FAILED -- 1> 1>Exc level 0: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. 1> at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr typeInstArgs, Int32 typeInstCount, IntPtr methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) 1> at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) 1> at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) 1> at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) 1> at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) 1> at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) 1> at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit) 1> at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](Assembly element) 1> at Orleans.ApplicationParts.AssemblyAttributeFeatureProvider1.PopulateFeature(IEnumerable1 parts, TFeature feature) in D:\build\agent_work\18\s\src\Orleans.Core\ApplicationParts\AssemblyAttributeFeatureProvider.cs:line 41 1> at Orleans.ApplicationParts.ApplicationPartManager.PopulateFeature[TFeature](TFeature feature) in D:\build\agent_work\18\s\src\Orleans.Core\ApplicationParts\ApplicationPartManager.cs:line 49 1> at Orleans.ApplicationPartManagerExtensions.CreateAndPopulateFeature[TFeature](IApplicationPartManager applicationPartManager) in D:\build\agent_work\18\s\src\Orleans.Core\ApplicationParts\ApplicationPartManagerExtensions.cs:line 60 1> at Orleans.CodeGenerator.RoslynCodeGenerator..ctor(IApplicationPartManager partManager, ILoggerFactory loggerFactory) in D:\build\agent_work\18\s\src\Orleans.CodeGeneration\RoslynCodeGenerator.cs:line 43 1> at Orleans.CodeGeneration.CodeGenerator.GenerateSourceForAssembly(Assembly grainAssembly, LogLevel logLevel) 1> at Orleans.CodeGeneration.CodeGenerator.GenerateCodeInternal(CodeGenOptions options) 1> at Orleans.CodeGeneration.CodeGenerator.GenerateCode(CodeGenOptions options) 1> at Orleans.CodeGeneration.Program.Main(String[] args) 1>C:\Users\chris.nuget\packages\microsoft.orleans.orleanscodegenerator.build\2.0.3\build\Microsoft.Orleans.OrleansCodeGenerator.Build.targets(76,5): error MSB3073: The command ""dotnet" "C:\Users\chris.nuget\packages\microsoft.orleans.orleanscodegenerator.build\2.0.3\build..\tasks\netcoreapp2.0\Orleans.CodeGeneration.Build.dll" "@obj\Debug\netcoreapp2.1\codegen\Server.orleans.g.args.txt"" exited with code 3. 1>Done building project "Server.csproj" -- FAILED.

georgend commented 6 years ago

@gamemachine have you made any progress on this issue? Any new findings?

ghost commented 6 years ago

I was curious and willing to try and help a little so I took a quick look.

My understanding is that the CodeGeneration.Build heavily relies on reflection and has to load many assemblies to do its work. Thing is it's trying to load System.Runtime.dll of .net core 2.1 which leads to the exception because there's already a System.Runtime.dll loaded, but with the version of the .net core 2.0.7 (the one that was used to build the CodeGeneration.Build.

So far the NuGet package has two plaforms exposed:

My bet is we would need a netcoreapp21 folder with a version of the application compiled with .net core 2.1, that would certainly resolve the version mismatch issue that triggers the exception.

I took a look at the Microsoft.Orleans.OrleansCodeGenerator.Build.targets build target, you can see there are two paths depending from the $(MSBuildIsCore) variable to create the two existing platform.

Creating a third platform is way beyond my MSBuid skills and I don't even know if it's possible to build side-by-side dll of .net core 2.0 and 2.1 in the same Visual Studio solution... Even building Orleans.CodeGeneration.Build in .net core 2.1 won't be straightforward, looks like the whole Orleans solution is capped for 2.0 max.

Quick, stupid workaround Don't use build time code generation, the runtime one should be fine has it would be loaded and executed with version 2.1 of the system assemblies...

gamemachine commented 6 years ago

Unfortunately I can't remember the specifics of where it failed. But removing build time code gen I did try and it didn't work. Although invariably that problem whatever it was is likely easier to fix.

gamemachine commented 6 years ago

So I just upgraded to 2.1 release and tried again without code generation. Same error as before now I remember what it was. Calling GetGrain with Orleans client throws an error saying 'Cannot find an implementation class for grain interface'.

gamemachine commented 6 years ago

Ok so that was my bad I just didn't have it configured correctly for runtime code gen.

gamemachine commented 6 years ago

Although runtime generation doesn't appear to use WithReferences, so it looks like you need to call AddApplicationPart for every grain and type that needs to be serialized?

vmendi commented 6 years ago

We hit the same problem. We are going to go back to .net core 2.0 while we wait for a solution here.

benjaminpetit commented 6 years ago

It seems that building OrleansCodegeneration.Build targeting the 2.1 runtime seems to fix the issue at build time. Still testing to see if there is no issue when the silo is up and running.

ghost commented 6 years ago

@benjaminpetit yes, it should be as the right version of the DLL are then used for the CodeGen... Is there any drawback to rely on runtime generation instead of build-time? instead of the time overhead at each run?

sergeybykov commented 6 years ago

Resolved via #4673.