HamedFathi
HamedFathi

Reputation: 3979

MSBuildWorkspace OpenProjectAsync have empty documents

I created a simple console application via dotnet new console

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

</Project>

Then I wrote a block of code in another application to get the compilation result by Roslyn

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CommandLineParser" Version="2.8.0" />
    <PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="3.6.0-4.final" />
    <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.6.0-4.final" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0-4.final" />
    <PackageReference Include="Microsoft.Build" Version="16.5.0" />
    <PackageReference Include="PowerArgs" Version="3.6.0" />
  </ItemGroup>

</Project>
var workspace = MSBuildWorkspace.Create();
workspace.LoadMetadataForReferencedProjects = true;
var project = workspace.OpenProjectAsync(@"C:\...\Desktop\sample\sample.csproj").Result;
var compilation = project.GetCompilationAsync().Result;

But the length of Documents is 0 so I wrote another block of code to find out what was the problem

var diagnostics = workspace.Diagnostics;
foreach (var diagnostic in diagnostics)
{
    Console.WriteLine(diagnostic.Message);
}

The error is (VS):

Msbuild failed when processing the file 'C:\...\Desktop\sample\sample.csproj' with message: The SDK 'Microsoft.NET.Sdk' specified could not be found.  C:\...\Desktop\sample\sample.csproj

and in Rider:

Msbuild failed when processing the file 'C:\...\Desktop\sample\sample.csproj' with message: MSB0001: Internal MSBuild Error: Type information for Microsoft.Build.Utilities.ToolLocationHelper was present in the whitelist cache as Microsoft.Build.Utilities.ToolLocationHelper, Microsoft.Build.Utilities.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a but the type could not be loaded. unexpectedly null

My dotnet core info:

.NET Core SDK (reflecting any global.json):
 Version:   3.1.201
 Commit:    b1768b4ae7

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.201\

Host (useful for support):
  Version: 3.1.3
  Commit:  4a9f85e9f8

.NET Core SDKs installed:
  3.1.101 [C:\Program Files\dotnet\sdk]
  3.1.102 [C:\Program Files\dotnet\sdk]
  3.1.201 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

What should I do to get the result?

Upvotes: 6

Views: 2004

Answers (1)

Dan
Dan

Reputation: 7724

I came across this error recently.

To get it working, I had to make sure I had:

Then in my code I could write

MSBuildLocator.RegisterDefaults(); // Register location of .NET SDK

var workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync(@"C:\project.csproj").ConfigureAwait(false);
var compilation = await project.GetCompilationAsync().ConfigureAwait(false);

It's worth being aware there is currently an issue (https://github.com/microsoft/MSBuildLocator/issues/179) with Microsoft.Build.Locator 1.5.3 that produces this same error message, so you should use 1.4.1 until a fix is provided

Upvotes: 3

Related Questions