Skip to content

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

Closed

Description

@0xced

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 ddea3ae).

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

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

Activity

  1. 0xced commented on Nov 19, 2021

    @0xced
    ContributorAuthor

    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.

  2. 0xced commented on Nov 19, 2021

    @0xced
    ContributorAuthor

    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!

  3. KirillOsenkov commented on Nov 20, 2021

    @KirillOsenkov
    Owner

    Yup, see related #403

    Thanks for investigating this!

  4. added a commit that references this issue on Dec 9, 2021
    049a214
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions