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
19.06k stars 4.04k forks source link

Project's list of documents should not be empty when project contains one or more .cs files #24767

Open MortInfinite opened 6 years ago

MortInfinite commented 6 years ago

Version Used: Microsoft.CodeAnalysis NuGet package version 2.6.1

Steps to Reproduce:

  1. Create empty console project with .NET framework 4.6, call this project "RoslynTest".
  2. Add Microsoft.CodeAnalysis NuGet package version 2.6.1 to project.
  3. Add new console project with .NET framework 4.6, to solution, call this project "TestProject".
  4. Add the following code to "RoslynTest" project's Program.cs file:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;

namespace RoslynTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            Task task = Task.Run(() => program.Main());
            task.Wait();
        }

        async Task Main()
        {
            MSBuildWorkspace    build   = MSBuildWorkspace.Create();
            Solution        solution = await build.OpenSolutionAsync("..\\..\\..\\RoslynTest.sln");
            Project         project = solution.Projects.Where(currentProject => currentProject.Name == "TestProject").First();

            foreach(Document document in project.Documents)
                Trace.WriteLine(document.Name);
        }
    }
}
  1. Run RoslynTest project.

Expected Behavior: Expected to see "Program.cs" document in the list of documents contained in "TestProject" project.

Actual Behavior: The project.Documents collection is empty.

Pilchie commented 6 years ago

Tagging @DustinCampbell. Try hooking something up to the WorkspaceFailed event, and also taking a look at the Compilation's Diagnostics. I suspect something went wrong with loading the project.

MortInfinite commented 6 years ago

The solution.Workspace.WorkspaceFailed event isn't raised.

The project's compilation's diagnostics contains one entry: error CS5001: Program does not contain a static 'Main' method suitable for an entry point

CrispyDrone commented 4 years ago

I fixed this issue by adding public access modifiers to the Program class and Main method.

However, now I'm getting the following issue when trying to load a project that targets .net 4.7.2:

[Failure] Msbuild failed when processing the file 
'C:\Users\CrispyDrone\Documents\Projects\aop-logging-poc\SampleTarget.net47\SampleTarget.net47.csproj' with message: 
The imported project "C:\Users\CrispyDrone\Documents\Projects\aop-logging-poc\Aop.Logging\bin\Debug\netcoreapp2.1\Microsoft.CSharp.targets" was not found. 
Confirm that the expression in the Import declaration "C:\Users\CrispyDrone\Documents\Projects\aop-logging-poc\Aop.Logging\bin\Debug\netcoreapp2.1\Microsoft.CSharp.targets" is correct, 
and that the file exists on disk.  C:\Users\CrispyDrone\Documents\Projects\aop-logging-poc\SampleTarget.net47\SampleTarget.net47.csproj

When I try to open a project that targets dotnet core 2.1, I receive the following error message:

[Failure] Msbuild failed when processing the file 'C:\Users\CrispyDrone\Documents\Projects\aop-logging-poc\SampleTarget\SampleTarget.csproj' 
with message: The SDK 'Microsoft.NET.Sdk' specified could not be found.
C:\Users\CrispyDrone\Documents\Projects\aop-logging-poc\SampleTarget\SampleTarget.csproj

output of dotnet --list-sdks:

$ dotnet --list-sdks
2.1.508 [C:\Program Files\dotnet\sdk]
2.1.510 [C:\Program Files\dotnet\sdk]
3.0.101 [C:\Program Files\dotnet\sdk]
3.1.100 [C:\Program Files\dotnet\sdk]
5.0.100-alpha1-015759 [C:\Program Files\dotnet\sdk]