Fixing issues with building C# solutions in .NET Core using Visual Studio Code

If you want to build C# solutions in .NET Core using Visual Studio Code, you might come across these two issues.

Symptoms

Recently, I wanted to see if I could work a .NET Core solution we've been building at Rencore on my Mac. I had previously installed Visual Studio Code, the C# extension for Visual Studio Code, .net core as well as other prerequisites like Mono and OmniSharp.

My project would build just fine using dotnet build, but in VSCode I'd miss intellisense. While trying to solve the issue I came across many proposed solutions. Here are the two that worked for me.

Bug 1: "The SDK 'Microsoft.NET.Sdk' specified could not be found."

After opening a .NET Core project in VSCode, you might see a bug similar to the following:

The SDK 'Microsoft.NET.Sdk' specified could not be found.

To fix it, in your VSCode settings, set the OmniSharp Path to latest:

{
  "omnisharp.path": "latest"
}

You're more likely to see this issue if you installed OmniSharp a while ago and haven't updated it since. Apparently, older versions of OmniSharp can't properly deal with newer versions of .NET Core. With this setting, on each start, VSCode will check if it's using the latest version of OmniSharp, which should properly support the latest features.

After applying the aforementioned change, reload the VSCode window. Often, you will start seeing another issue.

Bug 2: "Predefined type 'System.*' is not defined or imported"

Once OmniSharp will be able to load your project, it will start complaining that it can't find specific types that are a part of .NET Core. Your code window will be full of red squigglies.

To solve it, ensure you have the ~/.omnisharp/omnisharp.json file (you might need to create it), and verify that it has the following contents:

{
  "MSBuild": {
    "UseLegacySdkResolver": true
  }
}

Apparently, in some cases, OmniSharp has trouble finding the right version of the .NET framework to use with the project. On Windows, it's often solved by a parallel installation of Visual Studio. On macOS though, the above change, instructs it to use the legacy way of resolving .NET framework version.

That's it! With both changes applied I could build my project and had proper intellisense just like expected.