提交 0576aa01 编写于 作者: M Matt Warren

Merge pull request #4791 from mattwar/Bug3931

Fix check for missing/unsupported language
......@@ -8,3 +8,4 @@ Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.MSBuildProjectLoader(Microso
Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.Properties.get -> System.Collections.Immutable.ImmutableDictionary<string, string>
Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.SkipUnrecognizedProjects.get -> bool
Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.SkipUnrecognizedProjects.set -> void
static Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create(Microsoft.CodeAnalysis.Host.HostServices hostServices) -> Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace
\ No newline at end of file
......@@ -716,6 +716,21 @@ private bool TryGetLoaderFromProjectPath(string projectFilePath, ReportMode mode
}
}
// since we have both C# and VB loaders in this same library, it no longer indicates whether we have full language support available.
if (loader != null)
{
language = loader.Language;
// check for command line parser existing... if not then error.
var commandLineParser = _workspace.Services.GetLanguageServices(language).GetService<ICommandLineParserService>();
if (commandLineParser == null)
{
loader = null;
this.ReportFailure(mode, string.Format(WorkspacesResources.CannotOpenProjectUnsupportedLanguage, projectFilePath, language));
return false;
}
}
return loader != null;
}
}
......
......@@ -60,6 +60,15 @@ public static MSBuildWorkspace Create(IDictionary<string, string> properties)
return Create(properties, DesktopMefHostServices.DefaultServices);
}
/// <summary>
/// Create a new instance of a workspace that can be populated by opening solution and project files.
/// </summary>
/// <param name="hostServices">The <see cref="HostServices"/> used to configure this workspace.</param>
public static MSBuildWorkspace Create(HostServices hostServices)
{
return Create(ImmutableDictionary<string, string>.Empty, hostServices);
}
/// <summary>
/// Create a new instance of a workspace that can be populated by opening solution and project files.
/// </summary>
......
......@@ -11,6 +11,8 @@
using System.Xml.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
......@@ -1031,6 +1033,67 @@ public void TestOpenSolution_WithUnrecognizedProjectTypeGuidAndUnrecognizedExten
});
}
private HostServices hostServicesWithoutCSharp = MefHostServices.Create(MefHostServices.DefaultAssemblies.Where(a => !a.FullName.Contains("CSharp")));
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
[WorkItem(3931, "https://github.com/dotnet/roslyn/issues/3931")]
public void TestOpenSolution_WithMissingLanguageLibraries_WithSkipFalse_Throws()
{
// proves that if the language libaries are missing then the appropriate error occurs
CreateFiles(GetSimpleCSharpSolutionFiles());
AssertThrows<InvalidOperationException>(() =>
{
var ws = MSBuildWorkspace.Create(hostServicesWithoutCSharp);
ws.SkipUnrecognizedProjects = false;
var solution = ws.OpenSolutionAsync(GetSolutionFileName(@"TestSolution.sln")).Result;
},
e =>
{
Assert.Equal(true, e.Message.Contains("extension"));
});
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
[WorkItem(3931, "https://github.com/dotnet/roslyn/issues/3931")]
public void TestOpenSolution_WithMissingLanguageLibraries_WithSkipTrue_SucceedsWithDiagnostic()
{
// proves that if the language libaries are missing then the appropriate error occurs
CreateFiles(GetSimpleCSharpSolutionFiles());
var ws = MSBuildWorkspace.Create(hostServicesWithoutCSharp);
ws.SkipUnrecognizedProjects = true;
var dx = new List<WorkspaceDiagnostic>();
ws.WorkspaceFailed += delegate (object sender, WorkspaceDiagnosticEventArgs e)
{
dx.Add(e.Diagnostic);
};
var solution = ws.OpenSolutionAsync(GetSolutionFileName(@"TestSolution.sln")).Result;
Assert.Equal(1, dx.Count);
Assert.True(dx[0].Message.Contains("extension"));
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
[WorkItem(3931, "https://github.com/dotnet/roslyn/issues/3931")]
public void TestOpenProject_WithMissingLanguageLibraries_Throws()
{
// proves that if the language libaries are missing then the appropriate error occurs
CreateFiles(GetSimpleCSharpSolutionFiles());
AssertThrows<InvalidOperationException>(() =>
{
var ws = MSBuildWorkspace.Create(hostServicesWithoutCSharp);
var project = ws.OpenProjectAsync(GetSolutionFileName(@"CSharpProject\CSharpProject.csproj")).Result;
},
e =>
{
Assert.Equal(true, e.Message.Contains("extension"));
});
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public void TestOpenProject_WithInvalidFilePath_Fails()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册