BeniaminoBaggins
BeniaminoBaggins

Reputation: 12433

Omnisharp vscode - Unhandled exception. System.BadImageFormatException: Could not load file or assembly

I've been having a hard time running dotnet commands on an existing dotnet 6 application, on my m1 mac. I seem to have made it worse by downloading the dotnet SDK 6.0.401 in arm64 version, which has caused this error as soon as I open the project in vs code:

Starting OmniSharp server at 9/25/2022, 4:57:13 PM Target: /Users/benjamin/dev/vepo_back_end/Vepo.sln

OmniSharp server started with .NET 6.0.401 . Path: /Users/benjamin/.vscode/extensions/ms-dotnettools.csharp-1.25.0-darwin-arm64/.omnisharp/1.39.0-net6.0/OmniSharp.dll PID: 59074

Unhandled exception. System.BadImageFormatException: Could not load file or assembly '/Users/benjamin/.vscode/extensions/ms-dotnettools.csharp-1.25.0-darwin-arm64/.omnisharp/1.39.0-net6.0/OmniSharp.dll'. An attempt was made to load a program with an incorrect format.

File name: '/Users/benjamin/.vscode/extensions/ms-dotnettools.csharp-1.25.0-darwin-arm64/.omnisharp/1.39.0-net6.0/OmniSharp.dll'

I have redownloaded the x86 version and reinstalled C# in vscode extensions but I still get the same error. What should I do?

UPDATE: Ive now successfully run dotnet commands and even ran the app. I still have the same error message when I open vs code though, so I still have the problem but it is not preventing me doing anything.

Upvotes: 1

Views: 3779

Answers (6)

Carl Walsh
Carl Walsh

Reputation: 6959

On Windows I had the same problem with dotnet x86 and x64 incompatibility. I have 32-bit vscode installed, which installs ms-dotnettools.csharp-1.25.2-win32-ia32, which downloads that x86 version of Omnisharp. But I had C:\Program Files\dotnet before C:\Program Files (x86)\dotnet on my PATH, so when the vscode extension ran dotnet the x64 version was found, which failed.

The fix was to move C:\Program Files (x86)\dotnet to be before the x64 dotnet on my PATH, either temporarily on the command line, or in the system environment variables.

Upvotes: 0

Tanmesh Shah
Tanmesh Shah

Reputation: 77

I faced a similar issue. I hope this solution works.

My solution was

Extension -> Settings (wheel symbol) -> Install Another Version... -> 1.24.4

Basically, I downgraded the version of the C# extension.

Upvotes: 1

BeniaminoBaggins
BeniaminoBaggins

Reputation: 12433

In my case just reverting the C# vscode extension to version 1.24.4 fixed it.

Actually, the issue with the version only exists for people with x64 .net sdk installed on an m1 mac. . Reveerting the C# vscode extension, or downloading the arm64 .net sdk should work (and I reverted the C# vscode extension).

Upvotes: 3

Meligy
Meligy

Reputation: 36594

Would you by accident have the setting omnisharp.useModernNet set to false? Or have any omnisharp. setting with mono or path or sdk in it?

If so, try removing all of them. And maybe setting omnisharp.useModernNet explicitly to true (it should be the default).

If this fails, try:

  • Removing and reinstalling the extension
  • Setting "omnisharp.path": "latest"

Upvotes: 2

Khoa Nguyen
Khoa Nguyen

Reputation: 1086

I got the same problem, I installed .NET 6.0.401 from Microsoft packages but the issue still happened.

It turned out VSCode was using x64 version which was installed previously, not arm64 one. It seems the installer didn't relocate my dotnet command. I re-create the dotnet link to correct arm64 one and the error was gone.

Upvotes: 1

covrebo
covrebo

Reputation: 48

I had the same errors using dotnet 6.0.x SDK and VSCode on my M1 Mac. VSCode would not run the C# debugger with the same error. I checked in the File > About VSCode menu item and was running the x64 version of VSCode. I uninstalled VSCode and then re-installed with the universal installer and it now shows the Universal installation. This resolved the error for me.

Upvotes: 0

Related Questions