Roslyn: Microsoft.CodeAnalysis.Project.Documents.Count returns 0 even when the actual .csproj project has .cs files

Created on 9 Mar 2018  Â·  11Comments  Â·  Source: dotnet/roslyn

Version Used:
2.7.0

Steps to Reproduce:

  1. We are trying to open a ".csproj" project using Roslyn's code below:
using (var workspace = MSBuildWorkspace.Create())
{
      Project currentProject = workspace.OpenProjectAsync(projectPath).Result;
      projectComilation = currentProject.GetCompilationAsync().Result;
      workspace.LoadMetadataForReferencedProjects = true;

      foreach(var document in currentProject.Documents)
     {
           ReadDocument(document);
     }
}
  1. The above code works well for projects created using VS 2015 (Example https://github.com/zzhi/JobEngine/tree/master/src/Quartz.Examples/Quartz.Examples.csproj). In such .csproj files, there is a ItemGroup having following entries :
<ItemGroup>
    <None Include="App.config" />
    <Compile Include="..\AssemblyInfo.cs">
      <Link>AssemblyInfo.cs</Link>
    </Compile>
    <Compile Include="example16\AsyncExample.cs" />
    <Compile Include="example16\AsyncJob.cs" />
    <Compile Include="Program.cs">
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="example10\PlugInExample.cs" />
    <Compile Include="example10\SimpleJob.cs" />
    <Compile Include="example11\LoadExample.cs" />
    <Compile Include="example11\SimpleJob.cs" />
    <Compile Include="example12\RemoteClientExample.cs" />
    <Compile Include="example12\RemoteServerExample.cs" />
    <Compile Include="example12\SimpleJob.cs" />
    <Compile Include="example13\ClusterExample.cs" />
    <Compile Include="example13\SimpleRecoveryJob.cs" />
    <Compile Include="example13\SimpleRecoveryStatefulJob.cs" />
    <Compile Include="example15\SimpleJob.cs" />
    <Compile Include="example1\HelloJob.cs">
      <SubType>Code</SubType>
    </Compile>
</ItemGroup>
  1. But when we try to run the same above code for .csproj created using VS 2017 (for example https://github.com/OmniSharp/omnisharp-roslyn/tree/master/src/OmniSharp.Abstractions/OmniSharp.Abstractions.csproj), the currentProject.Documents returns 0 documents.
    Also there is no Compile Include section in the OmniSharp.Abstractions.csproj which includes any .cs files. But if we open the OmniSharp.Abstractions.csproj, it has the .cs files associated with the file.

Expected Behavior:
Documents should be available.

Actual Behavior:
No Documents present

Area-IDE Bug Concept-API

Most helpful comment

I had similar error. In my case MSBuild diagnostics returned:

Msbuild error while processing file "..." with the 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

Installing Nuget Package Microsoft.Build.Tasks.Core fixed the issue (MSBuild errors didn't disappeared entirely, but it started to return list of documents correctly for both VS2012 and VS2017 projects).

All 11 comments

Tagging @DustinCampbell

@DustinCampbell looking forward to hear from you on this issue.

@DustinCampbell looking forward to hear from you on this issue.
Guys pls give some leads.

Roslyn team,

Its getting difficult to move ahead without getting a support from the members of Roslyn development team.
@jcouv and @DustinCampbell , it will be helpful if you guys can assign some one else to give inputs on this issue.

@abhijitparkhi1983
I would start with looking at MSBuildWorkspace.Diagnostics like so:
```C#
using (var workspace = MSBuildWorkspace.Create())
{
workspace.LoadMetadataForReferencedProjects = true;
Project currentProject = workspace.OpenProjectAsync(projectPath).Result;
ImmutableList diagnostics = workspace.Diagnostics;
foreach(var diagnostic in diagnostics)
{
Console.Writeline(diagnostic.Message);
}
}
````
That should tell you what is going wrong.

If I had to take a guess I would say that the version of MSBuild that MSBuildWorkspace is using to load the projects is from VS 2015 not VS 2017. Globbing (the feature being used in https://github.com/OmniSharp/omnisharp-roslyn/tree/master/src/OmniSharp.Abstractions/OmniSharp.Abstractions.csproj so it doesn't need to specify compile includes) is only available in the VS 2017 version of MSBuild.

I have the same problem. I have found several projects for which not any documents are returned. For example: https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json.sln. MSBuildWorkspace.Diagnostics is empty. This looks like a showstopper issue for a Roslyn based Windows Store app I was planning to publish. Is there any workaround?

I had similar error. In my case MSBuild diagnostics returned:

Msbuild error while processing file "..." with the 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

Installing Nuget Package Microsoft.Build.Tasks.Core fixed the issue (MSBuild errors didn't disappeared entirely, but it started to return list of documents correctly for both VS2012 and VS2017 projects).

MSDN-WhiteKnig - diagnostics did not show any error for me but documents count was 0. However installing Microsoft.Build.Tasks.Core solved issue and now i am getting documents. Thanks.

I am hitting the same issue and my diagnostics gives the error

Msbuild failed when processing the file 'C:\Users\akagarw\AppData\Local\Temp\1v4lhsxn.uii\1v4lhsxn.uii.csproj' with message: The SDK 'Microsoft.NET.Sdk' specified could not be found. C:\Users\akagarw\AppData\Local\Temp\1v4lhsxn.uii\1v4lhsxn.uii.csproj

This is my project file
````


Exe
netcoreapp3.0
_1v4lhsxn.uii




````

The issue that I am hitting is possibly related to : https://github.com/OmniSharp/omnisharp-vscode/issues/2937

I have solved this by instaling MsBuild packages from nuget

2019-03-26, an, 23:02 Akshita Agarwal notifications@github.com rašė:

I am hitting the same issue and my diagnostics gives the error

Msbuild failed when processing the file
'C:\Users\akagarw\AppData\Local\Temp\1v4lhsxn.uii\1v4lhsxn.uii.csproj' with
message: The SDK 'Microsoft.NET.Sdk' specified could not be found.
C:\Users\akagarw\AppData\Local\Temp\1v4lhsxn.uii\1v4lhsxn.uii.csproj

This is my project file
Exe netcoreapp3.0 _1v4lhsxn.uii

The issue that I am hitting is possibly related to :
OmniSharp/omnisharp-vscode#2937
https://github.com/OmniSharp/omnisharp-vscode/issues/2937

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/roslyn/issues/25370#issuecomment-476870454,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADvMlzWnc9SpO_8VFQ4S2MCUnjPnt9oOks5vapj-gaJpZM4SkO5r
.

--

Pagarbiai,

Vytautas Pranskunas

Microsoft.Build.Tasks.Core

Thanks!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MadsTorgersen picture MadsTorgersen  Â·  3Comments

AceHack picture AceHack  Â·  3Comments

marler8997 picture marler8997  Â·  3Comments

asvishnyakov picture asvishnyakov  Â·  3Comments

OndrejPetrzilka picture OndrejPetrzilka  Â·  3Comments