-2

I have developed following code to generate dll files using Microsoft.CodeAnalysis.Emit library. The code successfully generates dll files for C# projects. However it doesn't successfully build Visual Basic projects. It throws lot of compiler errors for VB projects which build successfully using the VS IDE. Please see the errors thrown for a basic Windows application project. Is there any specific compiler options for VB projects? Please advice how to resolve this.

Microsoft.CodeAnalysis package version: 1.3.1

class Program
{
    static void Main(string[] args)
    {
        const string UnitTestArtifactFolder = @"c:\VSUnitTest";
        string SolutionPath = @"C:\B\VBWinApp\VBWinApp\VBWinApp.vbproj";
        CompileProject(SolutionPath, UnitTestArtifactFolder);
    }

    private static void CompileProject(string projectFilePath, string outputFolderPath)
    {
        using (var workspace = MSBuildWorkspace.Create())
        {
            var project = workspace.OpenProjectAsync(projectFilePath).Result;
            Emit(project, outputFolderPath).Wait();
        }
    }

    private static async Task Emit(Project project, string outputFolderPath)
    {
        Directory.CreateDirectory(outputFolderPath);
        var options = GetCompilationOptions(project);
        var compilation = await project.GetCompilationAsync();
        compilation = compilation.WithOptions(options);
        var outputFilePath = Path.Combine(outputFolderPath, Path.GetFileName(project.OutputFilePath));
        var pdbFilePath = Path.ChangeExtension(outputFilePath, "pdb");
        //LogInfo("Compiling the project...");
        var compilationStatus = compilation.Emit(outputFilePath, pdbPath: pdbFilePath);

        if (!compilationStatus.Success)
        {
            //LogError(compilationStatus.Diagnostics.First(k => k.WarningLevel == 0));
        }
        else
        {
           // LogInfo("Compilation successful.");
        }
    }

    private static CompilationOptions GetCompilationOptions(Project project)
    {
        if (project.Language == "C#")
        {
            //LogInfo("Using C# Compilation Options");
            return new CSharpCompilationOptions
            (OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Debug);
        }
        else if (project.Language == "Visual Basic")
        {
            //LogInfo("Using Visual Basic Compilation Options");
            return new VisualBasicCompilationOptions
                (OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Debug);
        }
        else
        {
            //Throw if the language is other than C# or VB
            throw new InvalidOperationException("Unsupported Language.");
        }
    }
}

Compiler errors on basic Windows app.

[0] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(34) : error BC30284: sub 'OnCreateMainForm' cannot be declared 'Overrides' because it does not override a sub in a base class. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [1] C:\B\VBWinApp\VBWinApp\My Project\Settings.Designer.vb(67) : error BC30002: Type 'Global.VBWinApp.My.MySettings' is not defined. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [2] C:\B\VBWinApp\VBWinApp\My Project\Settings.Designer.vb(69) : error BC30456: 'VBWinApp' is not a member of 'Global'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [3] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(26) : error BC30057: Too many arguments to 'Public Overloads Sub New()'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [4] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(27) : error BC30456: 'IsSingleInstance' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [5] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(28) : error BC30456: 'EnableVisualStyles' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [6] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(29) : error BC30456: 'SaveMySettingsOnExit' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [7] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(30) : error BC30456: 'ShutDownStyle' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [8] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(35) : error BC30456: 'MainForm' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [9] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(35) : error BC30456: 'VBWinApp' is not a member of 'Global'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [10]C:\B\VBWinApp\VBWinApp\My Project\Settings.Designer.vb(33) : error BC30456: 'Application' is not a member of 'My'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [11]C:\B\VBWinApp\VBWinApp\My Project\Settings.Designer.vb(47) : error BC30456: 'Application' is not a member of 'My'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [12]C:\B\VBWinApp\VBWinApp\My Project\AssemblyInfo.vb(1) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [13]C:\Users\xxxxx\AppData\Local\Temp.NETFramework,Version=v4.5.2.AssemblyAttributes.vb(4) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [14]C:\Users\xxxxx\AppData\Local\Temp.NETFramework,Version=v4.5.2.AssemblyAttributes.vb(5) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic}

svick
  • 236,525
  • 50
  • 385
  • 514
Bandara
  • 780
  • 8
  • 29
  • It's almost impossible to help you without seeing the VB code, but it appears that you may be missing references to some assemblies. You've used the `Overrides` keyword with the `OnCreateMainForm` method but the base class apparently does not have that method. Since you haven't shown any of the code that's just a guess. – Chris Dunaway Apr 05 '17 at 13:42
  • The VB project is a windows forms application without any customization. – Bandara Apr 05 '17 at 14:00
  • The VB.NET IDE auto-generates a bunch of code to support the My namespace and Application Framework features. That is the code it is complaining about. Otherwise very hard to see, it is well hidden. Not exactly a DLL project. Do consider the questionable wisdom of implementing your own IDE, you already have one. – Hans Passant Apr 05 '17 at 14:37
  • @HansPassant: I'm working on a VS extension where need to generate build artifacts and upload to a location for analysis purpose. – Bandara Apr 05 '17 at 15:07

1 Answers1

4

The issue is that by setting compilation options, you're throwing away all options that come from the project. If you just comment out the line compilation = compilation.WithOptions(options);, compilation should succeed (at least it does for me for a newly created VB.NET WinForms project).

svick
  • 236,525
  • 50
  • 385
  • 514
  • Thanks svick, It seems I have overridden the project default compiler options. I have commented the code as you suggested and it worked. – Bandara Apr 06 '17 at 05:50