From a3e24a3ad5655c9337bcabdd154d2ca97681e3da Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Thu, 23 Jul 2020 14:45:28 -0700 Subject: [PATCH 1/4] Fall back to best matching SDK ignoring global.json when SDK resolution fails --- .../MSBuildSdkResolver.cs | 49 +++++++++++++---- .../NETCoreSdkResolver.cs | 6 ++ .../Strings.resx | 3 + .../xlf/Strings.cs.xlf | 5 ++ .../xlf/Strings.de.xlf | 5 ++ .../xlf/Strings.es.xlf | 5 ++ .../xlf/Strings.fr.xlf | 5 ++ .../xlf/Strings.it.xlf | 5 ++ .../xlf/Strings.ja.xlf | 5 ++ .../xlf/Strings.ko.xlf | 5 ++ .../xlf/Strings.pl.xlf | 5 ++ .../xlf/Strings.pt-BR.xlf | 5 ++ .../xlf/Strings.ru.xlf | 5 ++ .../xlf/Strings.tr.xlf | 5 ++ .../xlf/Strings.zh-Hans.xlf | 5 ++ .../xlf/Strings.zh-Hant.xlf | 5 ++ .../GivenAnMSBuildSdkResolver.cs | 55 ++++++++++++++++++- 17 files changed, 163 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index d3839e5d8ee9..46ce71481f41 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -4,6 +4,7 @@ using Microsoft.Build.Framework; using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; @@ -56,6 +57,9 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext { string msbuildSdksDir = null; string netcoreSdkVersion = null; + IDictionary propertiesToAdd = new Dictionary(); + IDictionary itemsToAdd = new Dictionary(); + List warnings = new List(); if (context.State is CachedResult priorResult) { @@ -118,6 +122,13 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext netcoreSdkVersion, minimumVSDefinedSDKVersion); } + + if (!resolverResult.GlobalJsonHonored) + { + warnings.Add(Strings.GlobalJsonResolutionFailed); + propertiesToAdd.Add("SdkResolverHonoredGlobalJson", "false"); + propertiesToAdd.Add("SdkResolverGlobalJsonPath", resolverResult.GlobalJsonPath); + } } context.State = new CachedResult @@ -135,7 +146,7 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext msbuildSdkDir); } - return factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion); + return factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion, propertiesToAdd, itemsToAdd, warnings); } private static SdkResult Failure(SdkResultFactory factory, string format, params object[] args) @@ -186,9 +197,9 @@ public CompatibleSdkValue(string mostRecentCompatible, string mostRecentCompatib } } - private string GetMostCompatibleSdk(string dotnetExeDirectory, Version msbuildVersion) + private string GetMostCompatibleSdk(string dotnetExeDirectory, Version msbuildVersion, int minimumSdkMajorVersion = 0) { - CompatibleSdkValue sdks = GetMostCompatibleSdks(dotnetExeDirectory, msbuildVersion); + CompatibleSdkValue sdks = GetMostCompatibleSdks(dotnetExeDirectory, msbuildVersion, minimumSdkMajorVersion); if (_vsSettings.DisallowPrerelease()) { return sdks.MostRecentCompatibleNonPreview; @@ -197,7 +208,7 @@ private string GetMostCompatibleSdk(string dotnetExeDirectory, Version msbuildVe return sdks.MostRecentCompatible; } - private CompatibleSdkValue GetMostCompatibleSdks(string dotnetExeDirectory, Version msbuildVersion) + private CompatibleSdkValue GetMostCompatibleSdks(string dotnetExeDirectory, Version msbuildVersion, int minimumSdkMajorVersion) { return s_compatibleSdks.GetOrAdd( new CompatibleSdkKey(dotnetExeDirectory, msbuildVersion), @@ -218,6 +229,11 @@ private CompatibleSdkValue GetMostCompatibleSdks(string dotnetExeDirectory, Vers continue; } + if (Int32.TryParse(netcoreSdkVersion.Split('.')[0], out int sdkMajorVersion) && sdkMajorVersion < minimumSdkMajorVersion) + { + continue; + } + if (mostRecent == null) { mostRecent = netcoreSdkDir; @@ -292,17 +308,26 @@ private NETCoreSdkResolver.Result ResolveNETCoreSdkDirectory(SdkResolverContext { string globalJsonStartDir = Path.GetDirectoryName(context.SolutionFilePath ?? context.ProjectFilePath); var result = NETCoreSdkResolver.ResolveSdk(dotnetExeDir, globalJsonStartDir, _vsSettings.DisallowPrerelease()); + result.GlobalJsonHonored = true; - if (result.ResolvedSdkDirectory != null - && result.GlobalJsonPath == null - && context.MSBuildVersion < GetMinimumMSBuildVersion(result.ResolvedSdkDirectory)) + string mostCompatible = result.ResolvedSdkDirectory; + if (result.ResolvedSdkDirectory == null + && result.GlobalJsonPath != null + && context.IsRunningInVisualStudio) // TODO this works when opening VS but once building in VS this is false and we get the old SDK resolution error. { - string mostCompatible = GetMostCompatibleSdk(dotnetExeDir, context.MSBuildVersion); + result.GlobalJsonHonored = false; + mostCompatible = GetMostCompatibleSdk(dotnetExeDir, context.MSBuildVersion, 5); + } + else if (result.ResolvedSdkDirectory != null + && result.GlobalJsonPath == null + && context.MSBuildVersion < GetMinimumMSBuildVersion(result.ResolvedSdkDirectory)) + { + mostCompatible = GetMostCompatibleSdk(dotnetExeDir, context.MSBuildVersion); + } - if (mostCompatible != null) - { - result.ResolvedSdkDirectory = mostCompatible; - } + if (mostCompatible != null) + { + result.ResolvedSdkDirectory = mostCompatible; } return result; diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs b/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs index e00c618d93d7..8b83f1704cb3 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs @@ -20,6 +20,12 @@ public sealed class Result /// public string GlobalJsonPath; + /// + /// True if either (A) there was no applicable global.json or (B) existing global.json was used in resolution. + /// False if a global.json was found but there was no compatible SDK, so it was ignored. + /// + public bool GlobalJsonHonored; + public void Initialize(Interop.hostfxr_resolve_sdk2_result_key_t key, string value) { switch (key) diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/Strings.resx b/src/Microsoft.DotNet.MSBuildSdkResolver/Strings.resx index 548e00f32fe4..0b839942726e 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/Strings.resx +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/Strings.resx @@ -132,4 +132,7 @@ Unable to locate the .NET SDK. Check that it is installed and that the version specified in global.json (if any) matches the installed version. + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.cs.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.cs.xlf index 5842cba7e801..5876e58289b0 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.cs.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.cs.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} nejde najít. Zajistěte, aby byla nainstalovaná dostatečně vysoká verze sady .NET Core SDK, a/nebo zvyšte verzi zadanou v souboru global.json. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.de.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.de.xlf index 37f896733aff..e29a5661ce57 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.de.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.de.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} wurde nicht gefunden. Stellen Sie sicher, dass eine aktuelle Version des .NET Core SDK installiert ist, und/oder erhöhen Sie die in "global.json" angegebene Version. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.es.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.es.xlf index 6837dba032da..86544e3942ff 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.es.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.es.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} no se encuentra. Compruebe que hay un SDK de .NET Core suficientemente reciente instalado y aumente la versión especificada en global.json. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.fr.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.fr.xlf index 9904f66cf67a..0cfd8a53cc98 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.fr.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.fr.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} introuvable. Vérifiez qu'un SDK .NET Core suffisamment récent est installé et/ou augmentez la version spécifiée dans global.json. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.it.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.it.xlf index cbd23fe4fdc5..dc0aa94c66ca 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.it.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.it.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} non è stato trovato. Verificare che sia installata una versione abbastanza recente di .NET Core SDK e/o aumentare la versione specificata in global.json. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ja.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ja.xlf index edae0d5ce511..ded3baf852fc 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ja.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ja.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} が見つかりません。十分新しい .NET Core SDK がインストールされていることを確認するか、global.json で指定するバージョンを上げてください (どちらも実行する必要がある場合もあります)。 diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ko.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ko.xlf index 7a3641d8e1c1..b2c9cdc28922 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ko.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ko.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0}을(를) 찾을 수 없습니다. 최신 .NET Core SDK가 설치되어 있는지 확인하거나 global.json에 지정된 버전을 높이세요. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pl.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pl.xlf index d6a417a96853..b4ec17d6d84e 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pl.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pl.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. Nie można odnaleźć elementu {0}. Sprawdź, czy zainstalowano wystarczająco aktualną wersję zestawu .NET Core SDK i/lub zwiększ wersję określoną w pliku global.json. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pt-BR.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pt-BR.xlf index 8857826ccc8c..2520a52b19b9 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pt-BR.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pt-BR.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} não encontrado. Verifique se um SDK do .NET Core suficientemente recente está instalado e/ou aumente a versão especificada no global.json. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ru.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ru.xlf index eba0fd0cb460..eecabe0067e3 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ru.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ru.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} не найден. Убедитесь, что установлена достаточно свежая версия пакета SDK для .NET Core, и/или увеличьте версию, указанную в файле global.json. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.tr.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.tr.xlf index e15cdc3eb74d..fd0051aff00c 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.tr.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.tr.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0} bulunamadı. Yeterince yeni bir .NET Core SDK'sının yüklü olduğundan emin olun ve/veya global.json içinde belirtilen sürümü artırın. diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hans.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hans.xlf index 178e865d544a..19bcc81d5acc 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hans.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hans.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. {0}未找到。检查是否安装了最新的. net Core SDK,或增加global.json中指定的版本。 diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hant.xlf b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hant.xlf index 1e56c9476536..7b789f83c55f 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hant.xlf +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hant.xlf @@ -2,6 +2,11 @@ + + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed. + + {0} not found. Check that a recent enough .NET SDK is installed and/or increase the version specified in global.json. 找不到 {0}。請檢查安裝版本的 .NET Core SDK 是否夠新,並 (或) 提高 global.json 中指定的版本。 diff --git a/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index 79b017c7a4f2..a435a2a5c362 100644 --- a/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -156,6 +156,7 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo result.Success.Should().BeTrue(); result.Path.Should().Be((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); + result.PropertiesToAdd.Should().BeEmpty(); result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); result.Warnings.Should().BeNullOrEmpty(); result.Errors.Should().BeNullOrEmpty(); @@ -164,7 +165,7 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo [Theory] [InlineData(true)] [InlineData(false)] - public void ItDoesNotReturnHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionInGlobalJsonCannotBeFound(bool disallowPreviews) + public void ItDoesNotReturnHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionInGlobalJsonCannotBeFoundOutsideOfVisualStudio(bool disallowPreviews) { var environment = new TestEnvironment(_testAssetsManager, callingMethod: "ItDoesNotReturnHighest___", identifier: disallowPreviews.ToString()) { @@ -185,17 +186,59 @@ public void ItDoesNotReturnHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVer { MSBuildVersion = new Version(20, 0, 0, 0), ProjectFileDirectory = environment.TestDirectory, + IsRunningInVisualStudio = false }, new MockFactory()); result.Success.Should().BeFalse(); result.Path.Should().BeNull(); result.AdditionalPaths.Should().BeNull(); - result.Version.Should().BeNull();; + result.PropertiesToAdd.Should().BeNull(); + result.Version.Should().BeNull(); result.Warnings.Should().BeNullOrEmpty(); result.Errors.Should().NotBeEmpty(); } + [Theory] + [InlineData(true)] + [InlineData(false)] + public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionInGlobalJsonCannotBeFoundAndRunningInVisualStudio(bool disallowPreviews) + { + var environment = new TestEnvironment(_testAssetsManager, callingMethod: "ItReturnsHighest___", identifier: disallowPreviews.ToString()) + { + DisallowPrereleaseByDefault = disallowPreviews + }; + + var compatibleRtm = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "98.98.98", new Version(19, 0, 0, 0)); + var compatiblePreview = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.99-preview", new Version(20, 0, 0, 0)); + var incompatible = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "100.100.100", new Version(21, 0, 0, 0)); + + environment.CreateMuxerAndAddToPath(ProgramFiles.X64); + environment.CreateGlobalJson(environment.TestDirectory, "1.2.3"); + + var resolver = environment.CreateResolver(); + var result = (MockResult)resolver.Resolve( + new SdkReference("Some.Test.Sdk", null, null), + new MockContext + { + MSBuildVersion = new Version(20, 0, 0, 0), + ProjectFileDirectory = environment.TestDirectory, + IsRunningInVisualStudio = true + }, + new MockFactory()); + + result.Success.Should().BeTrue(); + result.Path.Should().Be((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); + result.AdditionalPaths.Should().BeNull(); + result.PropertiesToAdd.Count.Should().Be(2); + result.PropertiesToAdd.ContainsKey("SdkResolverHonoredGlobalJson"); + result.PropertiesToAdd.ContainsKey("SdkResolverGlobalJsonPath"); + result.PropertiesToAdd["SdkResolverHonoredGlobalJson"].Should().Be("false"); + result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); + result.Warnings.Should().BeEquivalentTo(new[] { "Unable to locate the .NET SDK as specified by global.json, please check that the specified version is installed." }); + result.Errors.Should().BeNullOrEmpty(); + } + [Fact] public void ItReturnsNullWhenTheDefaultVSRequiredSDKVersionIsHigherThanTheSDKVersionAvailable() { @@ -609,6 +652,7 @@ private sealed class MockContext : SdkResolverContext public new string ProjectFilePath { get => base.ProjectFilePath; set => base.ProjectFilePath = value; } public new string SolutionFilePath { get => base.SolutionFilePath; set => base.SolutionFilePath = value; } public new Version MSBuildVersion { get => base.MSBuildVersion; set => base.MSBuildVersion = value; } + public new bool IsRunningInVisualStudio { get => base.IsRunningInVisualStudio; set => base.IsRunningInVisualStudio = value; } public DirectoryInfo ProjectFileDirectory { @@ -630,6 +674,9 @@ public override SdkResult IndicateFailure(IEnumerable errors, IEnumerabl public override SdkResult IndicateSuccess(string path, string version, IEnumerable warnings = null) => new MockResult(success: true, path: path, version: version, warnings: warnings); + public override SdkResult IndicateSuccess(string path, string version, IDictionary propertiesToAdd, IDictionary itemsToAdd, IEnumerable warnings = null) + => new MockResult(success: true, path: path, version: version, warnings: warnings, propertiesToAdd: propertiesToAdd, itemsToAdd: itemsToAdd); + public override SdkResult IndicateSuccess(IEnumerable paths, string version, IDictionary propertiesToAdd = null, IDictionary itemsToAdd = null, IEnumerable warnings = null) => new MockResult(success: true, paths: paths, version: version, propertiesToAdd, itemsToAdd, warnings); @@ -638,13 +685,15 @@ public override SdkResult IndicateSuccess(IEnumerable paths, string vers private sealed class MockResult : SdkResult { public MockResult(bool success, string path, string version, IEnumerable warnings = null, - IEnumerable errors = null) + IEnumerable errors = null, IDictionary propertiesToAdd = null, IDictionary itemsToAdd = null) { Success = success; Path = path; Version = version; Warnings = warnings; Errors = errors; + PropertiesToAdd = propertiesToAdd; + ItemsToAdd = itemsToAdd; } public MockResult(bool success, IEnumerable paths, string version, From e2f1cbf3aac3118b2b2661725f0851bb2e17eed3 Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Thu, 23 Jul 2020 14:45:45 -0700 Subject: [PATCH 2/4] Error build on failed SDK resolution --- src/Tasks/Common/Resources/Strings.resx | 4 ++ src/Tasks/Common/Resources/xlf/Strings.cs.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.de.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.es.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.fr.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.it.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.ja.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.ko.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.pl.xlf | 5 +++ .../Common/Resources/xlf/Strings.pt-BR.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.ru.xlf | 5 +++ src/Tasks/Common/Resources/xlf/Strings.tr.xlf | 5 +++ .../Common/Resources/xlf/Strings.zh-Hans.xlf | 5 +++ .../Common/Resources/xlf/Strings.zh-Hant.xlf | 5 +++ .../Microsoft.NET.Sdk.DefaultItems.targets | 7 +++ .../GivenThatWeWantToBuildWithGlobalJson.cs | 44 +++++++++++++++++++ 16 files changed, 120 insertions(+) create mode 100644 src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithGlobalJson.cs diff --git a/src/Tasks/Common/Resources/Strings.resx b/src/Tasks/Common/Resources/Strings.resx index e6c8dc85e9a4..562d62c6b1d1 100644 --- a/src/Tasks/Common/Resources/Strings.resx +++ b/src/Tasks/Common/Resources/Strings.resx @@ -653,4 +653,8 @@ The following are names of parameters or literal values and should not be transl NETSDK1137: It is no longer necessary to use the Microsoft.NET.Sdk.WindowsDesktop SDK. Microsoft.NET.Sdk can be used instead. {StrBegin="NETSDK1137: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + diff --git a/src/Tasks/Common/Resources/xlf/Strings.cs.xlf b/src/Tasks/Common/Resources/xlf/Strings.cs.xlf index 803ffeca50aa..92ac59b87f97 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.cs.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.cs.xlf @@ -349,6 +349,11 @@ NETSDK1049: Přeložený soubor má nesprávnou image, nemá žádná metadata nebo je jiným způsobem nedostupný. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: Pro vybranou konfiguraci publikování se optimalizace velikosti sestavení nepodporuje. Ujistěte se, že publikujete samostatnou aplikaci. diff --git a/src/Tasks/Common/Resources/xlf/Strings.de.xlf b/src/Tasks/Common/Resources/xlf/Strings.de.xlf index f4477e505c1c..21a1a73eaca6 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.de.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.de.xlf @@ -349,6 +349,11 @@ NETSDK1049: Die aufgelöste Datei enthält ein fehlerhaftes Image oder keine Metadaten, oder der Zugriff ist aus anderen Gründen nicht möglich. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: Die Größenoptimierung von Assemblys wird für die ausgewählte Veröffentlichungskonfiguration nicht unterstützt. Stellen Sie sicher, dass Sie eine eigenständige App veröffentlichen. diff --git a/src/Tasks/Common/Resources/xlf/Strings.es.xlf b/src/Tasks/Common/Resources/xlf/Strings.es.xlf index e608cbae9852..06ebeec86b63 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.es.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.es.xlf @@ -349,6 +349,11 @@ NETSDK1049: El archivo resuelto tiene una imagen incorrecta, no tiene metadatos o no es posible su acceso. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: No se admite la optimización de tamaño de los ensamblados para la configuración de publicación seleccionada. Asegúrese de que está publicando una aplicación autónoma. diff --git a/src/Tasks/Common/Resources/xlf/Strings.fr.xlf b/src/Tasks/Common/Resources/xlf/Strings.fr.xlf index 64ae2d07a01a..205a5f35289e 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.fr.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.fr.xlf @@ -349,6 +349,11 @@ NETSDK1049: Le fichier résolu a une image incorrecte, ne comporte pas de métadonnées ou n'est pas accessible. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: l'optimisation de la taille des assemblys n'est pas prise en charge pour la configuration de publication sélectionnée. Vérifiez que vous publiez une application autonome. diff --git a/src/Tasks/Common/Resources/xlf/Strings.it.xlf b/src/Tasks/Common/Resources/xlf/Strings.it.xlf index e1718c7e5309..91c8b8c1a326 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.it.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.it.xlf @@ -349,6 +349,11 @@ NETSDK1049: il file risolto ha un'immagine danneggiata, non contiene metadati o è inaccessibile per altri motivi. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: l'ottimizzazione degli assembly per le dimensioni non è supportata per la configurazione di pubblicazione selezionata. Assicurarsi di pubblicare un'app indipendente. diff --git a/src/Tasks/Common/Resources/xlf/Strings.ja.xlf b/src/Tasks/Common/Resources/xlf/Strings.ja.xlf index 04c61f5eb2f4..30b96e95db59 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.ja.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.ja.xlf @@ -349,6 +349,11 @@ NETSDK1049: 解決されたファイルは、無効なイメージが含まれているか、メタデータが存在しないか、またはアクセスできません。{0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: アセンブリのサイズの最適化は、選択された公開構成に対してはサポートされていません。自己完結型のアプリを公開していることをご確認ください。 diff --git a/src/Tasks/Common/Resources/xlf/Strings.ko.xlf b/src/Tasks/Common/Resources/xlf/Strings.ko.xlf index 1f390807feaf..ee17f6681699 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.ko.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.ko.xlf @@ -349,6 +349,11 @@ NETSDK1049: 확인된 파일의 이미지가 잘못되었거나, 메타데이터가 없거나, 파일 자체에 액세스할 수 없습니다. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: 선택한 게시 구성에서는 크기에 대한 어셈블리 최적화가 지원되지 않습니다. 자체 포함 앱을 게시하고 있는지 확인하세요. diff --git a/src/Tasks/Common/Resources/xlf/Strings.pl.xlf b/src/Tasks/Common/Resources/xlf/Strings.pl.xlf index 1ab444365d29..ebfce960a3b1 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.pl.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.pl.xlf @@ -349,6 +349,11 @@ NETSDK1049: Rozpoznany plik ma nieprawidłowy obraz, nie ma metadanych lub jest w inny sposób niedostępny. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: Optymalizacja zestawów pod kątem rozmiaru nie jest obsługiwana w przypadku wybranej konfiguracji publikowania. Upewnij się, że publikujesz niezależną aplikację. diff --git a/src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf b/src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf index 5d1f8fc295a3..6d3cf8272691 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf @@ -349,6 +349,11 @@ NETSDK1049: O arquivo resolvido tem uma imagem inválida, não tem metadados ou está inacessível. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: não há suporte para a otimização de assemblies para tamanho na configuração de publicação selecionada. Verifique se você está publicando um aplicativo independente. diff --git a/src/Tasks/Common/Resources/xlf/Strings.ru.xlf b/src/Tasks/Common/Resources/xlf/Strings.ru.xlf index be3856391b6c..ce50db149ccc 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.ru.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.ru.xlf @@ -349,6 +349,11 @@ NETSDK1049: разрешенный файл содержит недопустимый образ, не содержит метаданных или недоступен по другим причинам. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: оптимизация сборок по размеру не поддерживается для выбранной конфигурации публикации. Убедитесь, что вы публикуете автономное приложение. diff --git a/src/Tasks/Common/Resources/xlf/Strings.tr.xlf b/src/Tasks/Common/Resources/xlf/Strings.tr.xlf index c12ae3fdc98f..be429a59f961 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.tr.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.tr.xlf @@ -349,6 +349,11 @@ NETSDK1049: Çözümlenen dosyada hatalı görüntü var, meta veri yok veya dosya erişilemez durumda. {0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: Derlemeleri boyut için iyileştirme, seçilen yayımlama yapılandırması için desteklenmiyor. Lütfen kendi içinde bulunan bir uygulama yayımladığınızdan emin olun. diff --git a/src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf b/src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf index 3b6407cd29b9..ba18ab098ff2 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf @@ -349,6 +349,11 @@ NETSDK1049: 解析的文件包含错误图像、无元数据或不可访问。{0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: 所选发布配置不支持优化程序集的大小。请确保你发布的是独立应用。 diff --git a/src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf b/src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf index 2b561ccacf47..65e1c6c10cc6 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf @@ -349,6 +349,11 @@ NETSDK1049: 解析的檔案含有毀損的映像、沒有中繼資料,或有其他無法存取的情況。{0} {1} {StrBegin="NETSDK1049: "} + + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1138: "} + NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. NETSDK1102: 選取的發佈設定不支援最佳化組件的大小。請確定您發佈的是獨立式應用程式。 diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets index 2f35e84f780c..075f576ac10d 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets @@ -343,6 +343,13 @@ Copyright (c) .NET Foundation. All rights reserved. + + + + diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithGlobalJson.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithGlobalJson.cs new file mode 100644 index 000000000000..26e9818cebd6 --- /dev/null +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithGlobalJson.cs @@ -0,0 +1,44 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using FluentAssertions; +using Microsoft.NET.TestFramework; +using Microsoft.NET.TestFramework.Commands; +using Xunit; +using Xunit.Abstractions; +using Microsoft.NET.TestFramework.Assertions; +using Microsoft.NET.TestFramework.ProjectConstruction; + +namespace Microsoft.NET.Build.Tests +{ + public class GivenThatWeWantToBuildWithGlobalJson : SdkTest + { + public GivenThatWeWantToBuildWithGlobalJson(ITestOutputHelper log) : base(log) + {} + + [Fact] + public void It_fails_build_on_failed_sdk_resolution() + { + var fakePath = "fakePath"; + TestProject testProject = new TestProject() + { + Name = "FailedResolution", + IsSdkProject = true, + TargetFrameworks = "net5.0" + }; + testProject.AdditionalProperties["SdkResolverHonoredGlobalJson"] = "false"; + testProject.AdditionalProperties["SdkResolverGlobalJsonPath"] = fakePath; + + var testAsset = _testAssetsManager.CreateTestProject(testProject); + + var buildCommand = new BuildCommand(testAsset); + buildCommand.Execute() + .Should() + .Fail() + .And + .HaveStdOutContaining("NETSDK1138") + .And + .HaveStdOutContaining(fakePath); + } + } +} From 09d017ecbe4921c228fa16b3699cb89f71e6e399 Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Mon, 3 Aug 2020 11:10:33 -0700 Subject: [PATCH 3/4] PR feedback --- .../MSBuildSdkResolver.cs | 7 +++---- .../NETCoreSdkResolver.cs | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 46ce71481f41..575d68010f97 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -123,7 +123,7 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext minimumVSDefinedSDKVersion); } - if (!resolverResult.GlobalJsonHonored) + if (resolverResult.FailedToResolveSpecifiedInGlobalJson) { warnings.Add(Strings.GlobalJsonResolutionFailed); propertiesToAdd.Add("SdkResolverHonoredGlobalJson", "false"); @@ -308,14 +308,13 @@ private NETCoreSdkResolver.Result ResolveNETCoreSdkDirectory(SdkResolverContext { string globalJsonStartDir = Path.GetDirectoryName(context.SolutionFilePath ?? context.ProjectFilePath); var result = NETCoreSdkResolver.ResolveSdk(dotnetExeDir, globalJsonStartDir, _vsSettings.DisallowPrerelease()); - result.GlobalJsonHonored = true; string mostCompatible = result.ResolvedSdkDirectory; if (result.ResolvedSdkDirectory == null && result.GlobalJsonPath != null - && context.IsRunningInVisualStudio) // TODO this works when opening VS but once building in VS this is false and we get the old SDK resolution error. + && context.IsRunningInVisualStudio) { - result.GlobalJsonHonored = false; + result.FailedToResolveSpecifiedInGlobalJson = true; mostCompatible = GetMostCompatibleSdk(dotnetExeDir, context.MSBuildVersion, 5); } else if (result.ResolvedSdkDirectory != null diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs b/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs index 8b83f1704cb3..ddafb9c36a9a 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs @@ -21,10 +21,9 @@ public sealed class Result public string GlobalJsonPath; /// - /// True if either (A) there was no applicable global.json or (B) existing global.json was used in resolution. - /// False if a global.json was found but there was no compatible SDK, so it was ignored. + /// True if a global.json was found but there was no compatible SDK, so it was ignored. /// - public bool GlobalJsonHonored; + public bool FailedToResolveSpecifiedInGlobalJson; public void Initialize(Interop.hostfxr_resolve_sdk2_result_key_t key, string value) { From ad4ae2e0b871f9913ab8b8e0eb65f9eba6b4dd98 Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Wed, 5 Aug 2020 14:49:28 -0700 Subject: [PATCH 4/4] PR feedback --- .../MSBuildSdkResolver.cs | 21 +++++++++++++------ .../NETCoreSdkResolver.cs | 2 +- src/Tasks/Common/Resources/xlf/Strings.cs.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.de.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.es.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.fr.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.it.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.ja.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.ko.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.pl.xlf | 6 +++--- .../Common/Resources/xlf/Strings.pt-BR.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.ru.xlf | 6 +++--- src/Tasks/Common/Resources/xlf/Strings.tr.xlf | 6 +++--- .../Common/Resources/xlf/Strings.zh-Hans.xlf | 6 +++--- .../Common/Resources/xlf/Strings.zh-Hant.xlf | 6 +++--- .../GivenAnMSBuildSdkResolver.cs | 2 +- .../GivenThatWeWantToBuildWithGlobalJson.cs | 2 +- 17 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 575d68010f97..1bbc4b3bc461 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -57,9 +57,9 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext { string msbuildSdksDir = null; string netcoreSdkVersion = null; - IDictionary propertiesToAdd = new Dictionary(); - IDictionary itemsToAdd = new Dictionary(); - List warnings = new List(); + IDictionary propertiesToAdd = null; + IDictionary itemsToAdd = null; + List warnings = null; if (context.State is CachedResult priorResult) { @@ -123,9 +123,17 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext minimumVSDefinedSDKVersion); } - if (resolverResult.FailedToResolveSpecifiedInGlobalJson) + if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson) { + if (warnings == null) + { + warnings = new List(); + } warnings.Add(Strings.GlobalJsonResolutionFailed); + if (propertiesToAdd == null) + { + propertiesToAdd = new Dictionary(); + } propertiesToAdd.Add("SdkResolverHonoredGlobalJson", "false"); propertiesToAdd.Add("SdkResolverGlobalJsonPath", resolverResult.GlobalJsonPath); } @@ -229,7 +237,7 @@ private CompatibleSdkValue GetMostCompatibleSdks(string dotnetExeDirectory, Vers continue; } - if (Int32.TryParse(netcoreSdkVersion.Split('.')[0], out int sdkMajorVersion) && sdkMajorVersion < minimumSdkMajorVersion) + if (minimumSdkMajorVersion != 0 && Int32.TryParse(netcoreSdkVersion.Split('.')[0], out int sdkMajorVersion) && sdkMajorVersion < minimumSdkMajorVersion) { continue; } @@ -314,7 +322,8 @@ private NETCoreSdkResolver.Result ResolveNETCoreSdkDirectory(SdkResolverContext && result.GlobalJsonPath != null && context.IsRunningInVisualStudio) { - result.FailedToResolveSpecifiedInGlobalJson = true; + result.FailedToResolveSDKSpecifiedInGlobalJson = true; + // We need the SDK to be version 5 or higher to ensure that we generate a build error when we fail to resolve the SDK specified by global.json mostCompatible = GetMostCompatibleSdk(dotnetExeDir, context.MSBuildVersion, 5); } else if (result.ResolvedSdkDirectory != null diff --git a/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs b/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs index ddafb9c36a9a..d5f29e65d8ef 100644 --- a/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs +++ b/src/Microsoft.DotNet.MSBuildSdkResolver/NETCoreSdkResolver.cs @@ -23,7 +23,7 @@ public sealed class Result /// /// True if a global.json was found but there was no compatible SDK, so it was ignored. /// - public bool FailedToResolveSpecifiedInGlobalJson; + public bool FailedToResolveSDKSpecifiedInGlobalJson; public void Initialize(Interop.hostfxr_resolve_sdk2_result_key_t key, string value) { diff --git a/src/Tasks/Common/Resources/xlf/Strings.cs.xlf b/src/Tasks/Common/Resources/xlf/Strings.cs.xlf index ef1805475e8d..bf74b6d58c5f 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.cs.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.cs.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.de.xlf b/src/Tasks/Common/Resources/xlf/Strings.de.xlf index d2b80e32a934..02fe467874de 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.de.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.de.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.es.xlf b/src/Tasks/Common/Resources/xlf/Strings.es.xlf index c112fd3549f0..86d54e01bf1e 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.es.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.es.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.fr.xlf b/src/Tasks/Common/Resources/xlf/Strings.fr.xlf index 8a80afd5c44f..ecb9a0d4e5d2 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.fr.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.fr.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.it.xlf b/src/Tasks/Common/Resources/xlf/Strings.it.xlf index d40d45dfd3fe..b60f243b334d 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.it.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.it.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.ja.xlf b/src/Tasks/Common/Resources/xlf/Strings.ja.xlf index 57c1066c33a3..c07ad4b34909 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.ja.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.ja.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.ko.xlf b/src/Tasks/Common/Resources/xlf/Strings.ko.xlf index f0e69a82ed0f..5649ff24aa6b 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.ko.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.ko.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.pl.xlf b/src/Tasks/Common/Resources/xlf/Strings.pl.xlf index 5cf536d74c3f..4ccece6c37c7 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.pl.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.pl.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf b/src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf index c9d49aa6b4dc..bf428ef93faa 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.ru.xlf b/src/Tasks/Common/Resources/xlf/Strings.ru.xlf index b842085773bf..3fa36637a2cc 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.ru.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.ru.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.tr.xlf b/src/Tasks/Common/Resources/xlf/Strings.tr.xlf index 6c03c47c458c..0c8edccbbc7d 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.tr.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.tr.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf b/src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf index 758d12ba3bf1..b2e0ee1449c3 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf b/src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf index 9f32c10d81ab..0e663f6b602d 100644 --- a/src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf +++ b/src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf @@ -350,9 +350,9 @@ {StrBegin="NETSDK1049: "} - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - NETSDK1138: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. - {StrBegin="NETSDK1138: "} + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + NETSDK1141: Unable to resolve the .NET SDK version as specified in the global.json located at {0}. + {StrBegin="NETSDK1141: "} NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. diff --git a/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index a435a2a5c362..2561cbfc56d5 100644 --- a/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/src/Tests/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -156,7 +156,7 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo result.Success.Should().BeTrue(); result.Path.Should().Be((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); - result.PropertiesToAdd.Should().BeEmpty(); + result.PropertiesToAdd.Should().BeNull(); result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); result.Warnings.Should().BeNullOrEmpty(); result.Errors.Should().BeNullOrEmpty(); diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithGlobalJson.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithGlobalJson.cs index 26e9818cebd6..b1113b248b58 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithGlobalJson.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithGlobalJson.cs @@ -36,7 +36,7 @@ public void It_fails_build_on_failed_sdk_resolution() .Should() .Fail() .And - .HaveStdOutContaining("NETSDK1138") + .HaveStdOutContaining("NETSDK1141") .And .HaveStdOutContaining(fakePath); }