Search code examples
visual-studio-code.net-core-3.0ubuntu-19.04

Why is my vscode 1.38.1 showing 'predefined type not defined or imported' when opening .NET Core 3.0 bare console app on Ubuntu 19


I installed .NET core 3.0 SDK and created an console app with dotnet new console. This is the project file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>
</Project>

The app runs successfully with dotnet run. When I open it in Visual Studio Code, all types including Program, void, string, Console are underlined indicating errors. The error is always the same. This example shows the string error

Predefined type 'System.String' is not defined or imported (CS0518) [myproject]

I've tried:

  • dotnet clean and dotnet restore/dotnet build
  • Manually removing bin, obj, .vscode and restoring
  • Restoring from vscode itself
  • Installed the latest version of the C# extension and of VS Code

Note that .netcoreapp2.2 apps open fine.

Here is some environment info:

$ code --version
1.38.1
b37e54c98e1a74ba89e03073e5a3761284e3ffb0
x64
$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.0.100
 Commit:    04339c3a26

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  19.04
 OS Platform: Linux
 RID:         ubuntu.19.04-x64
 Base Path:   /usr/share/dotnet/sdk/3.0.100/

Host (useful for support):
  Version: 3.0.0
  Commit:  95a0a61858

.NET Core SDKs installed:
  2.2.402 [/usr/share/dotnet/sdk]
  3.0.100 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.2.7 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.7 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.2.7 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download
# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 19.04
Release:        19.04
Codename:       disco

Solution

  • Update:

    Problem seems to be resolved in VS Code 1.39.0 even after removing mono-devel and its dependencies

    Original:

    Apparently, this is a C# Extension issue. As C# Extension fails only for netcoreapp3.0 GA #3289 issue comment suggest, installing Mono 6.0.0 (on linux or Mac) fixes the issue. I can confirm that.

    Install Mono 6 from here or for Ubuntu 18.04 and higher run the following:

    sudo apt install gnupg ca-certificates
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
    echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
    sudo apt update
    
    sudo apt install mono-devel