none
Compilation errors when compiling project

    問題

  • I am trying to write a code that will test if any method has ExecuteReader without corresponding Close reader - basic test to play with, just checks if ExecuteReader and Close are in the same block.

    This basic test was easy to implement and perform with Roslyn on hard-coded string with class code. However, when I try to open a solution programmaticaly and compile a project or document I get a lot of errors, here is the excerpt:


    error CS0246: The type or namespace name 'Roslyn' could not be found (are you missing a using directive or an assembly reference?)
    error CS0234: The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?)
    error CS0246: The type or namespace name 'InvocationExpressionSyntax' could not be found (are you missing a using directive or an assembly reference?)

    Here is the code:

     

                IWorkspace workspace = Workspace.LoadSolution(@"d:\test\test.sln");
                ISolution solution = workspace.CurrentSolution;
     
                foreach (IProject project in solution.Projects)
                {
                        var compilation = project.GetCompilation(); // tried with .AddReferences(project.MetadataReferences); also
     
                        foreach (var diagnostic in compilation.GetDiagnostics())
                        {
                            diagnostic.ToString();
                            Console.WriteLine(diagnostic.Info);
                        }

     

    I don't know if it matters, my test application is Roslyn Console Application.


    2011年10月31日 下午 11:10

解答

  • Filip,

    Thanks for reporting this issue! The bug turns out to be that Workspace.LoadSolution() attempts to process the each project in the solution using Configuration = Debug and Platform = AnyCPU by default. However, if the solution only contains an x86 project, that results in references not being found. You can workaround this by specifying the Platform when calling Workspace.LoadSolution().

    IWorkspace workspace = Workspace.LoadSolution(@"d:\test\test.sln", platform: "x86");
    

    We have fixed the problem for the next public release.

     


    Dustin Campbell | Senior Program Manager | Roslyn Visual Basic and C# Language Services
    2011年11月9日 下午 04:22
    擁有者
  • Hi Filip - This looks like a bug. As far as I can tell, the issue seems to be that the IProjects (and Compilations) for the projects loaded through Workspace.LoadSolution() only contain references for "mscorlib" and "System.Core" (all other references are missing). Running the following code only prints "mscorlib" and "System.Core" for each project in the solution...

    var workspace = Roslyn.Services.Workspace.LoadSolution(@"d:\test\test.sln");
    foreach (var p in workspace.CurrentSolution.Projects)
    {
        Console.WriteLine("Project File: " + p.Id.Name);
        var compilation = p.GetCompilation();
        foreach (var r in compilation.ReferencedAssemblyNames)
        {
            Console.WriteLine("    " + r.Name);
        }
    }

    Also, looks like the issue repros for any project (i.e. not just for Roslyn Console Applications).

    A possible workaround would be to parse each project file (pointed to by IProject.Id.Name) using System.Xml / Microsoft.Build and add the missing references to the Compilation manually (by calling .AddReferences() on the Compilation object)).

    Update: Please see Dustin's response below for an easier and more correct workaround.

    I can open an internal bug on your behalf for this issue. Alternately, if you wish to track the issue, you can open a bug directly on Connect. Let me know what works best for you. Thanks for reporting this!!


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    2011年11月1日 上午 12:58
    擁有者

所有回覆

  • Hi Filip - This looks like a bug. As far as I can tell, the issue seems to be that the IProjects (and Compilations) for the projects loaded through Workspace.LoadSolution() only contain references for "mscorlib" and "System.Core" (all other references are missing). Running the following code only prints "mscorlib" and "System.Core" for each project in the solution...

    var workspace = Roslyn.Services.Workspace.LoadSolution(@"d:\test\test.sln");
    foreach (var p in workspace.CurrentSolution.Projects)
    {
        Console.WriteLine("Project File: " + p.Id.Name);
        var compilation = p.GetCompilation();
        foreach (var r in compilation.ReferencedAssemblyNames)
        {
            Console.WriteLine("    " + r.Name);
        }
    }

    Also, looks like the issue repros for any project (i.e. not just for Roslyn Console Applications).

    A possible workaround would be to parse each project file (pointed to by IProject.Id.Name) using System.Xml / Microsoft.Build and add the missing references to the Compilation manually (by calling .AddReferences() on the Compilation object)).

    Update: Please see Dustin's response below for an easier and more correct workaround.

    I can open an internal bug on your behalf for this issue. Alternately, if you wish to track the issue, you can open a bug directly on Connect. Let me know what works best for you. Thanks for reporting this!!


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    2011年11月1日 上午 12:58
    擁有者
  • Hi Shyam,

    thank You for both workaround (voted up) and effort to reproduce it as a bug. I just opened bug on Connect to be able to track the issue.

    Kind Regards,

    Filip

    2011年11月1日 上午 09:17
  • Thank you Filip!
    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    2011年11月1日 下午 09:20
    擁有者
  • Filip,

    Thanks for reporting this issue! The bug turns out to be that Workspace.LoadSolution() attempts to process the each project in the solution using Configuration = Debug and Platform = AnyCPU by default. However, if the solution only contains an x86 project, that results in references not being found. You can workaround this by specifying the Platform when calling Workspace.LoadSolution().

    IWorkspace workspace = Workspace.LoadSolution(@"d:\test\test.sln", platform: "x86");
    

    We have fixed the problem for the next public release.

     


    Dustin Campbell | Senior Program Manager | Roslyn Visual Basic and C# Language Services
    2011年11月9日 下午 04:22
    擁有者
  • Dustin,

    Thanks for the help on this I was experiencing the same issue.  When you say this is fixed does that mean that no matter what the "platform" is set to in the project you will ensure that the ReferencedAssemblies will be loaded correctly?  I am trying to implement a solution that will scan source code.  I will not be able to control what value they have set for their platform.  So, when I load the solution/project I will it to load without specifying the platform.  If I specify a platform different from what they have their references do not load.  eg. I force it to 'x86' and they are set to 'AnyCPU', references do not load.

    As a workaround, Is there a way I can load the project without specifying, grab the platform they set and then reload it again with that platform in the mean time?

    Thanks,

    -J

    2012年5月24日 下午 06:12
  • Hi,

    I meant that this is fixed in the sense that you shouldn't have to specify the platform. Specifically passing the platform was definitely a workaround. However, you should find this fixed in the recent refresh of the Roslyn CTP.


    Dustin Campbell | Senior Program Manager | Roslyn Visual Basic and C# Language Services

    2012年6月7日 下午 02:30
    擁有者