1

I am trying to develop a solution which would analyze both C# and VisualBasic, of course with appropriate branches in the code for the two languages.

Initially I got the "language not supported" for both languages. Eventually I found an answer for C# which solved the problem for this particular language. The answer can be found at

Roslyn throws The language 'C#' is not supported

The resolution was very simple, just adding

var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions);

to my code. Apparently this makes MSBuild aware that the Microsoft.CodeAnalysis.CSharp.Workspaces is needed and as a result it loads it.

I have tried to find something similar for VisualBasic, but I was not able to find anything. I have tried, for example

var _v2 = typeof(Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation);

... but that did not help.

Is there a solution for VisualBasic similar to that of C#?

Mike
  • 87
  • 5
  • It's strange that the error message says `VisualBasic` instead of `Visual Basic`. Just to rule out a typo: Did you use `LanguageNames.VisualBasic`, which should be "Visual Basic", not "VisualBasic"? – Heinzi Nov 12 '19 at 14:13
  • 1
    Have you got a reference to [`Microsoft.CodeAnalysis.VisualBasic.Workspaces`](https://www.nuget.org/packages/Microsoft.CodeAnalysis.VisualBasic.Workspaces)? – DavidG Nov 12 '19 at 14:14
  • https://github.com/dotnet/roslyn-sdk/tree/master/samples/VisualBasic/Analyzers – Hans Passant Nov 12 '19 at 14:14
  • The exact message was Cannot open project 'C:\AppsDemo\vb-project-master\vbproject\vbproject.vbproj' because the language 'Visual Basic' is not supported. Which indeed had 'Visual Basic' not "VisualBasic'. I had the reference to Microsoft.CodeAnalysis.VisualBasic.Workspaces. – Mike Nov 12 '19 at 14:20
  • The corresponding error for "C#" may have some hints: https://stackoverflow.com/questions/38204509/roslyn-throws-the-language-c-is-not-supported – JoshVarty Nov 12 '19 at 17:34
  • Unfortunately the samples mentioned by @HansPassant are all in VB, while my code is in C#. Regardless, I could not find anything resembling my code, in particular an invocation of workspace.OpenSolutionAsync(), which in my case creates a Solution object without any projects inside. – Mike Nov 12 '19 at 23:34
  • I have already referred to the article mentioned by @JoshVarty . It makes a good suggestion for C#, which unfortunately does not work for Visual Basic. – Mike Nov 12 '19 at 23:36
  • Hmm, `Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation` exists in `Microsoft.CodeAnalysis.VisualBasic.dll` not `Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll`. I can't seem to find any public types in `Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll`... – JoshVarty Nov 13 '19 at 00:21
  • I had the same experience. Mine it was a wild and desperate try. I am aware that I should reference some class in Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll, but I could find no public class. There were such public classes in the C# equivalent, so it worked fine there. – Mike Nov 13 '19 at 02:12
  • How are you attempting to load the projects? With the MSBuildWorkspace or the AdHocWorkspace? without knowing what you are actually doing to try and load this project I do not know how we can provide a definitive answer – Jonathon Marolf Nov 13 '19 at 17:43
  • I am using MSBuildWorkspace. `using (MSBuildWorkspace buildWorkspace = MSBuildWorkspace.Create()) { ... Solution solutionToAnalyze = buildWorkspace.OpenSolutionAsync(pathSolution).Result;..}` – Mike Nov 13 '19 at 22:05

2 Answers2

1

I have discovered that for some unknown reason VS 2019 did not copy the Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll into the bin\X86\Debug folder. I copied it there manually, and then the problem was solved. The resulting Solution object contained the information about the projects in the VB solution.

The question still remains why the build does not copy this dll, but at least I can get around it manually.

Mike
  • 87
  • 5
0

@DavidG's comment on the question got me on the right track.

I already had the first two PackageReferences below and was getting the following InvalidOperationException exception.

Cannot open project '<Project Path>' because the language 'Visual Basic' is not supported.

The exception was no longer thrown when I added the third PackageReference.

<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MsBuild" Version="3.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="3.6.0" />

Note that I originally went with the latest version for the new PackageReference but had NuGet errors related to conflicting versions of Microsoft.CodeAnalysis.Common. I used the same version as the existing packages to resolve that.

NU1107 Version conflict detected for Microsoft.CodeAnalysis.Common. Install/reference Microsoft.CodeAnalysis.Common 4.2.0 directly to project to resolve this issue. -> Microsoft.CodeAnalysis.VisualBasic.Workspaces 4.2.0 -> Microsoft.CodeAnalysis.Common (= 4.2.0) TypescriptRuntimeGenerator -> Microsoft.CodeAnalysis.Workspaces.MSBuild 3.6.0 -> Microsoft.CodeAnalysis.Common (= 3.6.0).

Scott Munro
  • 13,369
  • 3
  • 74
  • 80