KirillOsenkov / MSBuildStructuredLog

A logger for MSBuild that records a structured representation of executed targets, tasks, property and item values.
MIT License
1.44k stars 190 forks source link

Getting System.BadImageFormatException when trying to debug a task #539

Closed 0xced closed 2 years ago

0xced commented 2 years ago

Here's the error I'm getting when I try to debug a task:

System.BadImageFormatException: Could not load file or assembly 'Microsoft.NET.Build.Tasks, Version=6.0.5.2708, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'Microsoft.NET.Build.Tasks, Version=6.0.5.2708, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at TaskRunner.Executor.Execute(Task task) in Z:\Projects\MSBuildStructuredLog\src\TaskRunner\Executor.cs:line 25
   at TaskRunner.Program.Run(String binlog, Int32 index, String taskName) in Z:\Projects\MSBuildStructuredLog\src\TaskRunner\Program.cs:line 119
   at TaskRunner.Program.Main(String[] args) in Z:\Projects\MSBuildStructuredLog\src\TaskRunner\Program.cs:line 63

Press any key to continue...

This error happens with both the latest version (2.1.561) and a freshly compiled version from the main branch (commit ddea3ae1d287ad199cc08b2d7465ba12af57ebfd).

The project that I'm trying to debug is built for .NET 6.

I'm currently trying to figure out why this is happening…

0xced commented 2 years ago

So, the TaskRunner is a .NET Framework 4.7.2 exe with Prefer32Bit=true and it tries to load C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\tools\net6.0\Microsoft.NET.Build.Tasks.dll with Assembly.LoadFrom(assemblyFilePath) and this is where the System.BadImageFormatException occurs.

Next step is trying to multi-target the TaskRunner as net472 + net6.0 and see what happens.

0xced commented 2 years ago

That's it, the .NET 6 TaskRunner executable works fine when loading C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\tools\net6.0\Microsoft.NET.Build.Tasks.dll. 😃

Now I guess the challenge of multi-targeting the TaskRunner is how to determine which version should be launched to debug an MSBuild task!

KirillOsenkov commented 2 years ago

Yup, see related #403

Thanks for investigating this!