MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.29k stars 327 forks source link

Mapster.Tool new FileNotFoundException after latest update #572

Open CountingSheep02 opened 1 year ago

CountingSheep02 commented 1 year ago

Hello @andrerav,

We're having an issue after upgrading from Mapster.Tool 8.4.0-pre05 to 8.4.0-pre06 which incorporated these changes from merge #544. I'm running on macOS and using .NET 7

Our project is structured into a Models library which is then referenced by a Mappings library that contains the IRegister classes and generated code that is output by the Mapster.Tool package.

When running the new prerelease package, we get an error that it can't load all of the assemblies anymore.

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Google.Protobuf, Version=3.22.1.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604'. The system cannot find the file specified.

  File name: 'Google.Protobuf, Version=3.22.1.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604'
     at SampleApplication.Mappings.SampleRegister.Register(TypeAdapterConfig config)
     at Mapster.TypeAdapterConfig.Apply(IEnumerable`1 registers) in C:\Projects\Mapster\src\Mapster\TypeAdapterConfig.cs:line 662
     at Mapster.TypeAdapterConfig.Scan(Assembly[] assemblies) in C:\Projects\Mapster\src\Mapster\TypeAdapterConfig.cs:line 651
     at Mapster.Tool.Program.GenerateExtensions(ExtensionOptions opt) in C:\Projects\Mapster\src\Mapster.Tool\Program.cs:line 386
     at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)
     at Mapster.Tool.Program.Main(String[] args) in C:\Projects\Mapster\src\Mapster.Tool\Program.cs:line 18

I noticed the previously referenced merge(#544 ) removed the AssembyResolver.cs and switched over to using:

Assembly.LoadFrom(Path.GetFullPath(opt.Assembly));

previously:

using var dynamicContext = new AssemblyResolver(Path.GetFullPath(opt.Assembly));
var assembly = dynamicContext.Assembly;

I pulled down the source code and reverted these changes to what they were previously, and everything worked again. It seems like the assembly resolver maybe shouldn't have been removed and the original issue might need to be addressed in another way?

andrerav commented 1 year ago

@CountingSheep02 Hm, I still think the new solution is preferable (i.e using Assembly.LoadFrom() rather than using the older custom assembly resolving class). However, I don't think it's appropriate to simply catch the FileNotFoundException in the GetLoadableTypes() method either. I will do some testing and get back to you.

MobinHiq commented 1 year ago

Hi @andrerav and @CountingSheep02 , the problem comes from dotnet version. I noticed this using both net6.0 and net7.0 with the same project settings which everything works fine on net6.0 but on net7.0 same error mentioned above pops in my solution's output. I hope this problem would be fixed soon.

Arrivegrsmith commented 1 year ago

I'm also having similar issues with Mapster.Tool on dotnet 7 on macOS. ` Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

 at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
 at System.Reflection.Assembly.GetTypes()
 at Mapster.Tool.Extensions.Scan(CodeGenerationConfig config, Assembly assembly) in C:\Projects\Mapster\src\Mapster.Tool\Extensions.cs:line 177
 at Mapster.Tool.Program.GenerateModels(ModelOptions opt) in C:\Projects\Mapster\src\Mapster.Tool\Program.cs:line 146
 at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)
 at Mapster.Tool.Program.Main(String[] args) in C:\Projects\Mapster\src\Mapster.Tool\Program.cs:line 17`

To me, it seems like code generation should be using https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview anyway instead of a CLI tool and csproj integration.