dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.84k stars 666 forks source link

Omnisharp does not respect / discover global.json and dotnet version, causing a lot of fake errors #5595

Open Meligy opened 1 year ago

Meligy commented 1 year ago

Issue Description

In "omnisharp.useModernNet": true mode, when you have a global.json file which restricts the major version of dotnet, for example "version": "6.0.202", "rollForward": "latestMinor", it ignores it, tries to use a higher dotnet version, which of course fails, causing a lot of fake errors like:

image

Even though dotnet compilation fails, and setting the dotnet version manually in workspace settings works

"omnisharp.sdkVersion": "6.0.202"

Which is not a good worksaround, because it's a duplicate of the global.json file, and more importantly, because it does not allow a version range. I cannot say 6.0.* for example, which is what to be expected when the global.json file has "rollForward": "latestMinor".

Notes:

Steps to Reproduce

Expected Behavior

Omnisharp to pick up the global.json file, and use the highest compatible .NET SDK version

Actual Behavior

Omnisharp Output says it's using the highest SDK (7 in the steps) and logs below show it doesn't see the global.json file.

This happens even if there's another similar file in a higher folder or user level.

Also worth noting that in the logs it says it didn't see a global.json file, although it exists and it's applicable as confirmed by dotnet CLI commands.

Logs

OmniSharp log

Starting OmniSharp server at 24/02/2023, 12:31:41
    Target: /Users/meligy/REDACTED/REDACTED.sln

OmniSharp server started with .NET 7.0.200
.
    Path: /Users/meligy/.vscode-insiders/extensions/ms-dotnettools.csharp-1.25.4-darwin-arm64/.omnisharp/1.39.4-net6.0/OmniSharp.dll
    PID: 26875

Received response for /v2/getcodeactions but could not find request.

C# log

(That was empty)

Environment information

VSCode version: 1.76.0-insider C# Extension: 1.25.4

Mono Information OmniSharp using mono: 6.12.0
Dotnet Information .NET SDK: Version: 7.0.200 Commit: 534117727b Runtime Environment: OS Name: Mac OS X OS Version: 13.2 OS Platform: Darwin RID: osx.13-arm64 Base Path: /usr/local/share/dotnet/sdk/7.0.200/ Host: Version: 7.0.3 Architecture: arm64 Commit: 0a2bda10e8 .NET SDKs installed: 6.0.202 [/usr/local/share/dotnet/sdk] 7.0.200 [/usr/local/share/dotnet/sdk] .NET runtimes installed: Microsoft.AspNetCore.App 6.0.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Other architectures found: None Environment variables: DOTNET_ROOT [/usr/local/share/dotnet] global.json file: Not found Learn more: https://aka.ms/dotnet/info Download .NET: https://aka.ms/dotnet/download
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |classynaming|reflectiondm|1.2.0| |Code-Like-A-Rainbow|Invillia|0.3.0| |copilot-labs|GitHub|0.10.726| |copilot-nightly|GitHub|1.74.8965| |csharp|ms-dotnettools|1.25.4| |dotnet-test-explorer|formulahendry|0.7.8| |jupyter|ms-toolsai|2023.2.1000552237| |jupyter-keymap|ms-toolsai|1.0.0| |jupyter-renderers|ms-toolsai|1.0.15| |markdown-pdf|yzane|1.4.4| |msbuild-project-tools|tintoy|0.4.9| |prettier-vscode|esbenp|9.10.4| |roslynator|josefpihrt-vscode|4.2.0| |semantic-darcula|JuanMiret|0.0.2| |semantic-rainbow|thertzlor|1.1.5| |shell-syntax|bmalehorn|1.0.5| |Theme-rainbow|gerane|0.0.5| |vscode-drawio|hediet|1.6.6| |vscode-eslint|dbaeumer|2.4.0| |vscode-javascript-booster|sburg|14.0.1| |vscode-jupyter-cell-tags|ms-toolsai|0.1.6| |vscode-jupyter-slideshow|ms-toolsai|0.1.5| |vscode-styled-components|styled-components|1.7.6| |vscode-versionlens|pflannery|1.5.0|;
Meligy commented 1 year ago

After some more investigation, I think I found the root issue, which is way less likely to happen than I originally thought...

Essentially, the answer lies in the Dotnet Information:

.NET SDKs installed:
6.0.202 [/usr/local/share/dotnet/sdk]
7.0.200 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

I was about confused because the Omnisharp log always says it's running as .NET 7, so I thought this was the problem.

But the actual problem seems to be that, given a .NET SDK 6.0.202 SDK installed and set in global.json, and the ability to go to the latest installed .NET 6 SDK allowed in the file, I also somehow had a .NET 6.0.4 runtime, but no .NET 6.0.4 SDK.

This is probably what confused Omnisharp. I think it looked for the missing .NET 6.0.4 SDK when it found the .NET 6.0.4 runtime. Because installing the .NET 6.0.4 SDK worked around the issue.

The correct behaviour would be to search only SDKs regardless of runtime. Or at least give a clear error about not finding the .NET 6.0.4 SDK specifically, or log the version in standard logging even.

But again, knowing that, it's a way less priority issue than originally thought.

sei-jmattson commented 1 year ago

Because installing the .NET 6.0.4 SDK worked around the issue.

I still don't understand the problem or solution. SDK 6.0.202 provides the runtime 6.0.4, which you already have. Are you saying omnisharp found runtime 6.0.13 and couldn't find some assets from the corresponding SDK 6.0.405?

I just installed the latest SDK 6.0.412 with runtime 6.0.20, but still get spurious omnisharp errors. I had to specify omnisharp.sdkVersion: 6.0.412 to get it working correctly.