Recently after upgrading to .Net 7 and Visual Studio Code 1.73.1 the IDE visual-studio-code shows error message like
cmd.exe
(as admin or as user <-- i picked admin)cmd.exe
go to a folder for instance cd C:\dev\
dotnet new console --use-program-main -o IdeTrouble
c:\dev\IdeTrouble
dotnet run
code .
this open VSCode and the project IdeTroubledotnet build
from a terminalC:\dev\IdeTrouble>dotnet build
MSBuild version 17.4.0+18d5aef85 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
IdeTrouble -> C:\dev\stackoverflow\IdeTrouble
\bin\Debug\net7.0
\IdeTrouble.dll
Build succeeded.
In the screen you can see that rider shows no errors but visual-studio-code 1.73.1 does show lots of errors.
I would like that visual studio code shows no errors and problems and finds all the types and namespaces it needs.
This is the code but running dotnet new console --use-program-main
is all you need to reproduce this annoyance on my machine.
namespace IdeTrouble;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
This is one of the errors
[{
"resource": "/c:/dev/IdeTrouble
/obj/Debug/net7.0
/.NETCoreApp,Version=v7.0.AssemblyAttributes.cs",
"owner": "csharp",
"code": "CS0400",
"severity": 8,
"message": "The type or namespace name 'System' could not be found
in the global namespace
(are you missing an assembly reference?)
[IdeTrouble]",
"source": "csharp",
"startLineNumber": 4,
"startColumn": 20,
"endLineNumber": 4,
"endColumn": 26
}]
After rebooting an opening the output windows (CTRL + SHIFT + U) i saw this
[fail]: OmniSharp.MSBuild.ProjectLoader
Version 7.0.100 of the .NET SDK requires at least version 17.3.0
of MSBuild. The current available version of MSBuild is 17.1.0.7505.
Change the .NET SDK specified in global.json to an older version
that requires the MSBuild version currently available.
[fail]: OmniSharp.MSBuild.ProjectLoader
The SDK 'Microsoft.NET.Sdk' specified could not be found.
[fail]: OmniSharp.MSBuild.ProjectManager
Failed to load project file 'c:\dev\...\ObjectNotDefined.csproj'.
To my understanding of MSBuild the needed msbuild version should be part of the sdk. Running dotnet --list-sdks
returns this:
C:\Windows\System32>dotnet --list-sdks
3.1.120 [C:\Program Files\dotnet\sdk]
3.1.426 [C:\Program Files\dotnet\sdk]
5.0.408 [C:\Program Files\dotnet\sdk]
6.0.113 [C:\Program Files\dotnet\sdk]
7.0.100 [C:\Program Files\dotnet\sdk]
I updated C# extension for Visual Studio Code and now got an error "dotnet.exe" could not be found so i added the path to the sdk
"omnisharp.sdkPath": "C:\\Program Files\\dotnet\\sdk"
to the omnisharp config settings
and also
The Troubleshooting: 'The .NET Core SDK cannot be located.' errors advised to check where dotnet is located
C:\>where.exe dotnet
and possibly change the path
In 64-bit environments the .NET SDK will fail to be discovered if the 32-bit dotnet path comes before the 64-bit dotnet path in the Environment PATH variable. Try removing the 32-bit path entirely from your PATH variable and relaunch VS Code to see if your issue is resolved.
The command where.exe dotnet
returned this
C:\Program Files\dotnet\dotnet.exe
C:\Program Files (x86)\dotnet\dotnet.exe
To check the path you can enter these commands
C:\>SystemPropertiesAdvanced.exe
C:\>echo %PATH%
I deleted the x86-path
and set both paths sdk
and dotnetPath
in the omnisharp settings.
"omnisharp.sdkPath": "C:\\Program Files\\dotnet\\sdk"
"omnisharp.dotnetPath": "C:\\Program Files\\dotnet\\"
This resolved all the reported problems and also Visual Studio Code go-to-definition
does not work