diff --git a/build.json b/build.json index 606b11f567..eaf5892634 100644 --- a/build.json +++ b/build.json @@ -2,6 +2,7 @@ "DotNetInstallScriptURL": "https://dot.net/v1", "DotNetChannel": "Preview", "DotNetVersions": [ + "2.1.811", "3.1.403", "5.0.100" ], @@ -44,6 +45,7 @@ "ProjectWithDisabledAnalyzers2", "ProjectWithAnalyzers", "NetCore21Project", + "NetCore31Project", "Net50Project", "ProjectWithAnalyzersAndEditorConfig", "ProjectWithParentEditorConfig" diff --git a/src/OmniSharp.Host/MSBuild/Discovery/MSBuildLocator.cs b/src/OmniSharp.Host/MSBuild/Discovery/MSBuildLocator.cs index 24c2435a2c..226a5ce871 100644 --- a/src/OmniSharp.Host/MSBuild/Discovery/MSBuildLocator.cs +++ b/src/OmniSharp.Host/MSBuild/Discovery/MSBuildLocator.cs @@ -46,13 +46,23 @@ protected override void DisposeCore(bool disposing) } public static MSBuildLocator CreateDefault(ILoggerFactory loggerFactory, IAssemblyLoader assemblyLoader, IConfiguration msbuildConfiguration) - => new MSBuildLocator(loggerFactory, assemblyLoader, + { + var useBundledOnly = msbuildConfiguration.GetValue("UseBundledOnly"); + if (useBundledOnly) + { + var logger = loggerFactory.CreateLogger(); + logger.LogInformation("Because 'UseBundledOnly' is enabled in the configuration, OmniSharp will only use the bundled MSBuild."); + return CreateStandAlone(loggerFactory, assemblyLoader); + } + + return new MSBuildLocator(loggerFactory, assemblyLoader, ImmutableArray.Create( new DevConsoleInstanceProvider(loggerFactory), new VisualStudioInstanceProvider(loggerFactory), new MonoInstanceProvider(loggerFactory), new StandAloneInstanceProvider(loggerFactory), new UserOverrideInstanceProvider(loggerFactory, msbuildConfiguration))); + } public static MSBuildLocator CreateStandAlone(ILoggerFactory loggerFactory, IAssemblyLoader assemblyLoader) => new MSBuildLocator(loggerFactory, assemblyLoader, diff --git a/src/OmniSharp.MSBuild/Options/MSBuildOptions.cs b/src/OmniSharp.MSBuild/Options/MSBuildOptions.cs index 3fb0491ae0..d0cdbe7f50 100644 --- a/src/OmniSharp.MSBuild/Options/MSBuildOptions.cs +++ b/src/OmniSharp.MSBuild/Options/MSBuildOptions.cs @@ -2,6 +2,7 @@ namespace OmniSharp.Options { public class MSBuildOptions { + public bool UseBundledOnly { get; set; } = false; public string ToolsVersion { get; set; } public string VisualStudioVersion { get; set; } public string Configuration { get; set; } diff --git a/test-assets/test-projects/NetCore21Project/global.json b/test-assets/test-projects/NetCore21Project/global.json new file mode 100644 index 0000000000..4b30b20fac --- /dev/null +++ b/test-assets/test-projects/NetCore21Project/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "2.1.811" + } +} diff --git a/test-assets/test-projects/NetCore31Project/NetCore31Project.csproj b/test-assets/test-projects/NetCore31Project/NetCore31Project.csproj new file mode 100644 index 0000000000..23df6047ff --- /dev/null +++ b/test-assets/test-projects/NetCore31Project/NetCore31Project.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/test-assets/test-projects/NetCore31Project/Program.cs b/test-assets/test-projects/NetCore31Project/Program.cs new file mode 100644 index 0000000000..dbae8113d5 --- /dev/null +++ b/test-assets/test-projects/NetCore31Project/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ProjectAndSolution +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/test-assets/test-projects/NetCore31Project/global.json b/test-assets/test-projects/NetCore31Project/global.json new file mode 100644 index 0000000000..2412b02fcf --- /dev/null +++ b/test-assets/test-projects/NetCore31Project/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "3.1.403" + } +} diff --git a/tests/OmniSharp.MSBuild.Tests/MSBuildSelectionTests.cs b/tests/OmniSharp.MSBuild.Tests/MSBuildSelectionTests.cs index dfeadd9e0a..1c13d46b30 100644 --- a/tests/OmniSharp.MSBuild.Tests/MSBuildSelectionTests.cs +++ b/tests/OmniSharp.MSBuild.Tests/MSBuildSelectionTests.cs @@ -1,5 +1,9 @@ using System; +using System.Collections.Generic; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using OmniSharp.MSBuild.Discovery; +using OmniSharp.Services; using TestUtility; using Xunit; using Xunit.Abstractions; @@ -255,6 +259,20 @@ public void StandAloneIsPreferredOverUnsupportedVS(string vsVersion) msbuildLocator.DeleteFakeInstancesFolders(); } + [Fact] + public void CreateDefault_UseBundledOnly_True_LocatesOnlyStandAloneInstance() + { + var configBuilder = new Microsoft.Extensions.Configuration.ConfigurationBuilder().AddInMemoryCollection(new Dictionary() + { + ["useBundledOnly"] = "true" + }); + var loggerFactory = new LoggerFactory(); + var locator = MSBuildLocator.CreateDefault(loggerFactory, new AssemblyLoader(loggerFactory), configBuilder.Build()); + var instances = locator.GetInstances(); + Assert.Single(instances); + Assert.Equal(DiscoveryType.StandAlone, instances[0].DiscoveryType); + } + private static MSBuildInstance GetStandAloneMSBuildInstance() { return new MSBuildInstance( diff --git a/tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs b/tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs index 18deacf2c9..2538e6dd00 100644 --- a/tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs +++ b/tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs @@ -185,11 +185,11 @@ public async Task The_correct_project_capablities_is_emitted() using var testProject = await TestAssets.Instance.GetTestProjectAsync("NetCore21Project"); using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory)); Assert.Single(emitter.ReceivedMessages); - Assert.Equal("GenerateDocumentationFile CSharp Managed ReferencesFolder LanguageService RelativePathDerivedDefaultNamespace AssemblyReferences COMReferences ProjectReferences SharedProjectReferences OutputGroups AllTargetOutputGroups VisualStudioWellKnownOutputGroups SingleFileGenerators DeclaredSourceItems UserSourceItems BuildWindowsDesktopTarget CrossPlatformExecutable Pack", string.Join(" ", emitter.ReceivedMessages[0].ProjectCapabilities)); + Assert.Equal("GenerateDocumentationFile CSharp Managed ReferencesFolder LanguageService RelativePathDerivedDefaultNamespace AssemblyReferences COMReferences ProjectReferences SharedProjectReferences OutputGroups AllTargetOutputGroups VisualStudioWellKnownOutputGroups SingleFileGenerators DeclaredSourceItems UserSourceItems BuildWindowsDesktopTarget CrossPlatformExecutable FolderPublish Pack", string.Join(" ", emitter.ReceivedMessages[0].ProjectCapabilities)); } [Fact] - public async Task The_correct_sdk_version_is_emitted() + public async Task The_correct_sdk_version_is_emitted_NETCore2_1() { // Arrange var emitter = new ProjectLoadTestEventEmitter(); @@ -197,11 +197,23 @@ public async Task The_correct_sdk_version_is_emitted() using var testProject = await TestAssets.Instance.GetTestProjectAsync("NetCore21Project"); using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory)); Assert.Single(emitter.ReceivedMessages); + Assert.Equal(GetHashedFileExtension("2.1.811"), emitter.ReceivedMessages[0].SdkVersion); + } + + [Fact] + public async Task The_correct_sdk_version_is_emitted_NETCore3_1() + { + // Arrange + var emitter = new ProjectLoadTestEventEmitter(); + + using var testProject = await TestAssets.Instance.GetTestProjectAsync("NetCore31Project"); + using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory)); + Assert.Single(emitter.ReceivedMessages); Assert.Equal(GetHashedFileExtension("3.1.403"), emitter.ReceivedMessages[0].SdkVersion); } [Fact] - public async Task The_correct_sdk_version_is_emitted_2() + public async Task The_correct_sdk_version_is_emitted_NET5() { // Arrange var emitter = new ProjectLoadTestEventEmitter();