Search code examples
c#visual-studio-code.net-7.0omnisharp

Visual Studio Code 1.73.1 trouble - object not defined


Initial

Recently after upgrading to .Net 7 and Visual Studio Code 1.73.1 the IDE shows error message like

  • CS0518 Predefined type 'type' is not defined or imported
  • CS0400 The type or namespace name 'identifier' could not be found in the global namespace (are you missing an assembly reference?)
  • CS0246 The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)

Steps to reproduce

  1. install .Net 7.0
  2. install Visual Studio Code 1.73.1
  3. open cmd.exe (as admin or as user <-- i picked admin)
  4. inside cmd.exe go to a folder for instance cd C:\dev\
  5. run dotnet new console --use-program-main -o IdeTrouble
  6. cd into folder c:\dev\IdeTrouble
  7. run dotnet run
  8. run code . this open VSCode and the project IdeTrouble
  9. run dotnet build from a terminal
C:\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.

Result

  • Expected: the IDE reports no errors
  • Actual: lots of errors (see screen below)

In the screen you can see that shows no errors but 1.73.1 does show lots of errors.

Question: How do i fix this?

I would like that visual studio code shows no errors and problems and finds all the types and namespaces it needs.

enter image description here

Code

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
}]

OmniSharp Error - Update

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]

Solution

  • 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

    Visual Studio Code - settings omnisharp 1

    and also

    Visual Studio Code - settings omnisharp 2

    Update

    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