Puresharp icon indicating copy to clipboard operation
Puresharp copied to clipboard

AssemblyResolutionException on attempt to build project using Puresharp

Open kjaworski opened this issue 4 years ago • 1 comments

The following error appears during building a project in VS:

7>Unhandled Exception: Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
7>   at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
7>   at Mono.Cecil.DefaultAssemblyResolver.Resolve(AssemblyNameReference name)
7>   at IPuresharp.Authority.Modules(AssemblyDefinition assembly)
7>   at IPuresharp.Authority.Modules(AssemblyDefinition assembly)
7>   at IPuresharp.Authority.Include(AssemblyDefinition assembly, Assembly[] dependencies)
7>   at IPuresharp.Authority.Manage(TypeDefinition type, MethodDefinition method)
7>   at IPuresharp.Authority..ctor(TypeDefinition type)
7>   at IPuresharp.Program.Manage(TypeDefinition type)
7>   at IPuresharp.Program.Manage(String assembly)
7>   at IPuresharp.Program.Main(String[] arguments)

And VS logs this error: The command ""%USERPROFILE%\.nuget\packages\IPuresharp\5.0.5\build\net452\IPuresharp.exe" "C:\Sources\path_to_My.Internal.Company.Assembly.dll"" exited with code -532462766. My.Internal.Company.Assembly C:\Users\myuser\.nuget\packages\ipuresharp\5.0.5\build\netcoreapp2.1\IPuresharp.targets 11

kjaworski avatar May 17 '21 11:05 kjaworski

If you're building a classic .NET application, this may be due to IPureSharp's Program.Manage method not adding all possible packages folders below the solution root to the DefaultAssemblyResolver.

For example, if you are building a .NET classic solution in a folder named Foo containing a MyFoo project folder and a packages folder, the Foo\packages folder will not be added to the DefaultAssemblyResolver, and therefore not scanned for packages, leading to a compile-time exception.

You can see the source code for Program.Manage here. The DefaultAssemblyResolver only contains the folders %PROGRAM_FILES%\dotnet\shared and %USER_PROFILE%\.nuget\packages, in that order.

The folders in the DefaultAssemblyResolver are evaluated in the order in which they are added to it. %PROGRAM_FILES%\dotnet\shared was scanned and no matches were found. The scan proceeded to %USER_PROFILE%\.nuget\packages and no matches were found. Finally, as no more directories were in the resolver, the AssemblyResolutionException was thrown using the last directory scanned.

Ideally, any packages folders between the assembly that is the target of the weaving operation and the solution root should be added before %PROGRAM_FILES%\dotnet\shared and %USER_PROFILE%\.nuget\packages.

DreadLordMikey avatar Jan 04 '23 02:01 DreadLordMikey