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

CS7068: "Reference to type X claims it is defined in this assembly, but it is not defined" when compiling corefx #9893

Open svick opened 8 years ago

svick commented 8 years ago

I'm trying to compile corefx using MSBuildWorkspace and I'm failing at System.Diagnostics.Debug.

The errors I'm getting are all of the form:

error CS7068: Reference to type 'X' claims it is defined in this assembly, but it is not defined in source or any added modules

Where X are various basic types like Object, Enum, Attribute or IEnumerable<> (full error list).

Since running msbuild from the command line works fine (it only warns that "Packages containing MSBuild targets and props files cannot be fully installed in projects targeting multiple frameworks.", which I think is not relevant), I'm assuming this is an issue in Roslyn and not in the corefx project.

Running msbuild previously also should have restored all necessary packages and project.MetadataReferences looks okay to me (it contains 23 items, including Microsoft.TargetingPack.Private.CoreCLR\1.0.0-rc3-23910\lib\netstandard1.0\mscorlib.dll).

I'm using packages version 1.3.0-beta1-20160304-02 from Roslyn nightly MyGet. My code:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.MSBuild;

using static Microsoft.CodeAnalysis.DiagnosticSeverity;

namespace BooleanOrFinder
{
    static class Program
    {
        private const string SolutionPath = @"E:\Users\Svick\git\corefx\src\System.Diagnostics.Debug\System.Diagnostics.Debug.sln";

        static void Main()
        {
            var workspace = MSBuildWorkspace.Create(new Dictionary<string, string>
            {
                // http://stackoverflow.com/a/29550838/41071
                {"CheckForSystemRuntimeDependency", "true"}
            });

            var solution = workspace.OpenSolutionAsync(SolutionPath).Result;

            foreach (var project in solution.Projects)
            {
                var compilation = project.GetCompilationAsync().Result;

                if (compilation.GetDiagnostics().Any(d => d.Severity == Error))
                {
                    foreach (var diagnostic in compilation.GetDiagnostics())
                    {
                        Console.WriteLine(diagnostic);
                    }
                }
            }
        }
    }
}
gafter commented 8 years ago

Assigning to IDE as MSBuildWorkspace is in the workspaces layer.

jasonmalinowski commented 5 years ago

@svick Is this something you're still working around by chance?

svick commented 5 years ago

I don't need this code anymore, I don't even remember why I did when I opened this issue.

But I have now updated the code to:

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using static Microsoft.CodeAnalysis.DiagnosticSeverity;

static class Program
{
    private const string SolutionPath = @"C:\code\corefx\src\System.Diagnostics.Debug\System.Diagnostics.Debug.sln";

    static async Task Main()
    {
        MSBuildLocator.RegisterDefaults();

        var workspace = MSBuildWorkspace.Create();

        var solution = await workspace.OpenSolutionAsync(SolutionPath);

        foreach (var project in solution.Projects)
        {
            var compilation = await project.GetCompilationAsync();

            //compilation = compilation.WithOptions(compilation.Options.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));

            if (compilation.GetDiagnostics().Any(d => d.Severity == Error))
            {
                foreach (var diagnostic in compilation.GetDiagnostics())
                {
                    Console.WriteLine($"{project.Name}: {diagnostic}");
                }
            }
        }
    }
}

I then ran it with current versions of Roslyn and corefx and the output was:

System.Diagnostics.Debug.Tests: error CS5001: Program does not contain a static 'Main' method suitable for an entry point
System.Diagnostics.Debug: error CS5001: Program does not contain a static 'Main' method suitable for an entry point
System.Diagnostics.Debug: error CS5001: Program does not contain a static 'Main' method suitable for an entry point

You can fairly easily work around those errors (see commented-out line in the above code), but I think it does imply something is still wrong.

If you think that's not worth investigating, feel free to close this issue.