dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.95k stars 4.02k forks source link

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

Open abhijitparkhi1983 opened 6 years ago

abhijitparkhi1983 commented 6 years ago

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);
     }
    }
  2. 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

jcouv commented 6 years ago

Tagging @DustinCampbell

abhijitparkhi1983 commented 6 years ago

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

abhijitparkhi1983 commented 6 years ago

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

abhijitparkhi1983 commented 6 years ago

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.

jmarolf commented 6 years ago

@abhijitparkhi1983 I would start with looking at MSBuildWorkspace.Diagnostics like so:

using (var workspace = MSBuildWorkspace.Create())
{
    workspace.LoadMetadataForReferencedProjects = true;
    Project currentProject = workspace.OpenProjectAsync(projectPath).Result;
    ImmutableList<WorkspaceDiagnostic> 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.

kinex commented 6 years ago

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?

MSDN-WhiteKnight commented 5 years ago

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).

vytautas-pranskunas- commented 5 years ago

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.

akshita31 commented 5 years ago

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


  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <RootNamespace>_1v4lhsxn.uii</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NewtonSoft.json" Version="1.0.0" />
  </ItemGroup>

</Project>

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

vytautas-pranskunas- commented 5 years ago

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

EXKulo commented 5 years ago

Microsoft.Build.Tasks.Core

Thanks!!