From 14c59bc70a19e1f46d3f7dd6a3eb5c6fc65e11e4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 4 Nov 2025 22:27:50 +0000 Subject: [PATCH 1/3] Initial plan From 6458fd48e47964b0a13f62c679c2fe7ba552a01b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 4 Nov 2025 22:44:36 +0000 Subject: [PATCH 2/3] Implement clear error message for preview SDK blocked by VS settings Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> --- .../MSBuildSdkResolver.cs | 25 ++++++ .../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 ++ .../NETCoreSdkResolver.cs | 16 +++- .../GivenAnMSBuildSdkResolver.cs | 87 +++++++++++++++++++ 17 files changed, 195 insertions(+), 1 deletion(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index ae38c8e2f61c..b3a59bf3adf9 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -31,6 +31,7 @@ public sealed class DotNetMSBuildSdkResolver : SdkResolver private readonly Func? _getCurrentProcessPath; private readonly Func _getMsbuildRuntime; private readonly NETCoreSdkResolver _netCoreSdkResolver; + private readonly VSSettings _vsSettings; private const string DOTNET_HOST_PATH = nameof(DOTNET_HOST_PATH); private const string DotnetHostExperimentalKey = "DOTNET_EXPERIMENTAL_HOST_PATH"; @@ -86,6 +87,7 @@ public DotNetMSBuildSdkResolver(Func getEnvironmentVariable, Fu _getCurrentProcessPath = getCurrentProcessPath; _netCoreSdkResolver = new NETCoreSdkResolver(getEnvironmentVariable, vsSettings); _getMsbuildRuntime = getMsbuildRuntime; + _vsSettings = vsSettings; if (_getEnvironmentVariable(EnvironmentVariableNames.DOTNET_MSBUILD_SDK_RESOLVER_ENABLE_LOG) is string val && (string.Equals(val, "true", StringComparison.OrdinalIgnoreCase) || @@ -167,6 +169,29 @@ private sealed class CachedState if (resolverResult.ResolvedSdkDirectory == null) { logger?.LogMessage($"Failed to resolve .NET SDK. Global.json path: {resolverResult.GlobalJsonPath}"); + +#if NETFRAMEWORK + // Check if we're in Visual Studio and preview SDKs are disallowed + // If so, retry allowing previews to see if a preview SDK is available + if (context.IsRunningInVisualStudio && _vsSettings.DisallowPrerelease()) + { + logger?.LogString("Retrying SDK resolution allowing preview SDKs"); + var retryResult = _netCoreSdkResolver.ResolveNETCoreSdkDirectory(globalJsonStartDir, context.MSBuildVersion, context.IsRunningInVisualStudio, dotnetRoot, disallowPrerelease: false); + + if (retryResult.ResolvedSdkDirectory != null) + { + string previewVersion = new DirectoryInfo(retryResult.ResolvedSdkDirectory).Name; + logger?.LogMessage($"Found preview SDK: {previewVersion}"); + return Failure( + factory, + logger, + context.Logger, + Strings.UnableToLocateNETCoreSdkButPreviewAvailable, + previewVersion); + } + } +#endif + return Failure( factory, logger, diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/Strings.resx b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/Strings.resx index 164399e6902d..9f46cb1ff63a 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/Strings.resx +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/Strings.resx @@ -138,4 +138,7 @@ Unable to locate the .NET SDK version '{0}' as specified by global.json, please check that the specified version is installed. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.cs.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.cs.xlf index aaabdc4329cb..101bb30f863f 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.cs.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.cs.xlf @@ -37,6 +37,11 @@ Nepovedlo se najít sadu .NET SDK. Zkontrolujte, jestli je nainstalovaná, jestli je vaše cesta nakonfigurovaná pro správnou architekturu a jestli verze zadaná v souboru global.json (pokud existuje) odpovídá nainstalované verzi. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.de.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.de.xlf index 0ffc3dca3b8c..4731c99a1635 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.de.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.de.xlf @@ -37,6 +37,11 @@ Das .NET SDK wurde nicht gefunden. Überprüfen Sie, ob es installiert ist, Ihr PFAD für die richtige Architektur konfiguriert ist und dass die in „global.json“ angegebene Version (falls vorhanden) mit der installierten Version übereinstimmt. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.es.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.es.xlf index 157de49257be..79610d60e286 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.es.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.es.xlf @@ -37,6 +37,11 @@ No se encuentra el SDK de .NET. Compruebe que está instalado, que su RUTA DE ACCESO está configurada para la arquitectura correcta y que la versión especificada en global.json (si la hubiera) coincide con la versión instalada. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.fr.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.fr.xlf index b72b2d1079c3..dea9547d8942 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.fr.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.fr.xlf @@ -37,6 +37,11 @@ Impossible de localiser le SDK .NET. Vérifiez qu'il est installé, que votre PATH est configuré pour la bonne architecture et que la version spécifiée dans global.json (le cas échéant) correspond à la version installée. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.it.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.it.xlf index 73fb7f107f50..857807b78166 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.it.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.it.xlf @@ -37,6 +37,11 @@ Non è possibile trovare .NET SDK. Verificare che sia installato, che PATH sia configurato per l'architettura corretta e che la versione specificata in global.json, se presente, corrisponda alla versione installata. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ja.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ja.xlf index c1f5da9ad1fb..296a692a112a 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ja.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ja.xlf @@ -37,6 +37,11 @@ .NET SDK が見つかりません。それがインストールされていること、正しいアーキテクチャについて PATH が構成されていること、そのインストール済みバージョンが global.json で指定されているバージョンと一致していること (指定されている場合) をご確認ください。 + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ko.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ko.xlf index e8270ad89db7..3a55cc9163cc 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ko.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ko.xlf @@ -37,6 +37,11 @@ .NET SDK를 찾을 수 없습니다. 설치되었는지, PATH가 올바른 아키텍처로 구성되었는지, global.json(있는 경우)에 지정된 버전이 설치된 버전과 일치하는지 확인하세요. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pl.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pl.xlf index e3f9732da252..5eb008c2c4d9 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pl.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pl.xlf @@ -37,6 +37,11 @@ Nie można zlokalizować zestawu .NET SDK. Sprawdź, czy jest zainstalowana, ścieżka PATH jest skonfigurowana pod kątem poprawnej architektury i czy wersja określona w pliku global.json (jeśli istnieje) jest zgodna z zainstalowaną wersją. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pt-BR.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pt-BR.xlf index d7475fdeaa7d..81b9e2a5835c 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pt-BR.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.pt-BR.xlf @@ -37,6 +37,11 @@ Não é possível localizar o .NET SDK. Verifique se ele está instalado, seu PATH está configurado para a arquitetura correta e se a versão especificada em global.json (se houver) corresponde à versão instalada. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ru.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ru.xlf index df54e7aa973f..bee4ca860bdf 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ru.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.ru.xlf @@ -37,6 +37,11 @@ Не удается найти пакет SDK для .NET. Убедитесь, что пакет установлен, PATH настроен для правильной архитектуры и версия, указанная в файле global.json (если он имеется), соответствует установленной. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.tr.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.tr.xlf index 67197fcada96..6de6e408f8f4 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.tr.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.tr.xlf @@ -37,6 +37,11 @@ .NET SDK bulunamadı. Yüklü olduğundan, PATH’inizin doğru mimari için yapılandırıldığından ve (varsa) global.json içinde belirtilen sürümün yüklü sürümle eşleştiğinden emin olun. + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hans.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hans.xlf index c9e6fa5f0789..28c8d49a29c8 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hans.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hans.xlf @@ -37,6 +37,11 @@ 找不到 .NET SDK。检查它是否已安装、是否将 PATH 配置为正确的体系结构,以及 global.json (如果有)中指定的版本是否与安装的版本匹配。 + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hant.xlf b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hant.xlf index 54f9be5ad1c0..69556d802741 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hant.xlf +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/xlf/Strings.zh-Hant.xlf @@ -37,6 +37,11 @@ 找不到 .NET SDK。請檢查是否已安裝該項目,已針對正確的架構設定您的 PATH,且在 global.json 中指定的版本 (如果有的話) 和安裝的版本相符。 + + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + Unable to locate the .NET SDK. A preview SDK (version {0}) is available but is being blocked by your Visual Studio settings. To use preview SDKs, go to Tools -> Options -> Environment -> Preview Features and check "Use previews of the .NET SDK", or add "allowPrerelease": true to global.json. + + \ No newline at end of file diff --git a/src/Resolvers/Microsoft.DotNet.SdkResolver/NETCoreSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.SdkResolver/NETCoreSdkResolver.cs index 59ae5f5b187b..ed4aa2f2ed80 100644 --- a/src/Resolvers/Microsoft.DotNet.SdkResolver/NETCoreSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.SdkResolver/NETCoreSdkResolver.cs @@ -149,7 +149,21 @@ public Version GetMinimumMSBuildVersion(string netcoreSdkDir) public SdkResolutionResult ResolveNETCoreSdkDirectory(string? globalJsonStartDir, Version msbuildVersion, bool isRunningInVisualStudio, string? dotnetExeDir) { - var result = NETCoreSdkResolverNativeWrapper.ResolveSdk(dotnetExeDir, globalJsonStartDir, _vsSettings.DisallowPrerelease()); + return ResolveNETCoreSdkDirectory(globalJsonStartDir, msbuildVersion, isRunningInVisualStudio, dotnetExeDir, _vsSettings.DisallowPrerelease()); + } + + /// + /// Resolves the .NET Core SDK directory with explicit control over prerelease handling. + /// + /// Starting directory for global.json search + /// Version of MSBuild + /// Whether running in Visual Studio + /// Directory containing dotnet executable + /// Whether to disallow prerelease SDKs + /// SDK resolution result + public SdkResolutionResult ResolveNETCoreSdkDirectory(string? globalJsonStartDir, Version msbuildVersion, bool isRunningInVisualStudio, string? dotnetExeDir, bool disallowPrerelease) + { + var result = NETCoreSdkResolverNativeWrapper.ResolveSdk(dotnetExeDir, globalJsonStartDir, disallowPrerelease); string? mostCompatible = result.ResolvedSdkDirectory; if (result.ResolvedSdkDirectory == null diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index cebe1a8152b8..ff91d62af8a6 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -589,6 +589,93 @@ public void ItRespectsAmbientVSSettings() result.Errors.Should().BeNullOrEmpty(); } + [Fact] + public void ItReportsWhenPreviewSdkIsAvailableButDisallowedInVisualStudio() + { + var environment = new TestEnvironment(_testAssetsManager); + var preview = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "11.0.0-preview1"); + + environment.CreateMuxerAndAddToPath(ProgramFiles.X64); + environment.DisallowPrereleaseByDefault = true; + + var resolver = environment.CreateResolver(); + var result = (MockResult)resolver.Resolve( + new SdkReference("Some.Test.Sdk", null, null), + new MockContext + { + ProjectFileDirectory = environment.TestDirectory, + IsRunningInVisualStudio = true + }, + new MockFactory()); + + result.Success.Should().BeFalse(); + result.Path.Should().BeNull(); + result.AdditionalPaths.Should().BeNull(); + result.Version.Should().BeNull(); + result.Warnings.Should().BeNullOrEmpty(); +#if NETFRAMEWORK + result.Errors.Should().Contain(e => e.Contains("11.0.0-preview1") && e.Contains("preview SDK")); +#else + // On non-NETFRAMEWORK platforms, the retry logic doesn't run, so we should get the generic error + result.Errors.Should().Contain(Strings.UnableToLocateNETCoreSdk); +#endif + } + + [Fact] + public void ItDoesNotReportPreviewSdkWhenNotInVisualStudio() + { + var environment = new TestEnvironment(_testAssetsManager); + var preview = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "11.0.0-preview1"); + + environment.CreateMuxerAndAddToPath(ProgramFiles.X64); + environment.DisallowPrereleaseByDefault = true; + + var resolver = environment.CreateResolver(); + var result = (MockResult)resolver.Resolve( + new SdkReference("Some.Test.Sdk", null, null), + new MockContext + { + ProjectFileDirectory = environment.TestDirectory, + IsRunningInVisualStudio = false + }, + new MockFactory()); + + result.Success.Should().BeFalse(); + result.Path.Should().BeNull(); + result.AdditionalPaths.Should().BeNull(); + result.Version.Should().BeNull(); + result.Warnings.Should().BeNullOrEmpty(); + result.Errors.Should().Contain(Strings.UnableToLocateNETCoreSdk); + result.Errors.Should().NotContain(e => e.Contains("preview SDK")); + } + + [Fact] + public void ItDoesNotReportPreviewSdkWhenPreviewsAreAllowed() + { + var environment = new TestEnvironment(_testAssetsManager); + var preview = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "11.0.0-preview1"); + + environment.CreateMuxerAndAddToPath(ProgramFiles.X64); + environment.DisallowPrereleaseByDefault = false; + + var resolver = environment.CreateResolver(); + var result = (MockResult)resolver.Resolve( + new SdkReference("Some.Test.Sdk", null, null), + new MockContext + { + ProjectFileDirectory = environment.TestDirectory, + IsRunningInVisualStudio = true + }, + new MockFactory()); + + result.Success.Should().BeTrue($"No error expected. Error encountered: {string.Join(Environment.NewLine, result.Errors ?? new string[] { })}. Mocked Process Path: {environment.ProcessPath}. Mocked Path: {environment.PathEnvironmentVariable}"); + result.Path.Should().Be(preview.FullName); + result.AdditionalPaths.Should().BeNull(); + result.Version.Should().Be("11.0.0-preview1"); + result.Warnings.Should().BeNullOrEmpty(); + result.Errors.Should().BeNullOrEmpty(); + } + [Fact] public void GivenTemplateLocatorItCanResolveSdkVersion() { From f3b311f9ef7f1dbe3d4db480071677d59963e438 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 4 Nov 2025 22:47:55 +0000 Subject: [PATCH 3/3] Address code review: extract variable for clarity Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> --- .../MSBuildSdkResolver.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index b3a59bf3adf9..50520d8cb1d3 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -178,9 +178,10 @@ private sealed class CachedState logger?.LogString("Retrying SDK resolution allowing preview SDKs"); var retryResult = _netCoreSdkResolver.ResolveNETCoreSdkDirectory(globalJsonStartDir, context.MSBuildVersion, context.IsRunningInVisualStudio, dotnetRoot, disallowPrerelease: false); - if (retryResult.ResolvedSdkDirectory != null) + string? previewSdkDirectory = retryResult.ResolvedSdkDirectory; + if (previewSdkDirectory != null) { - string previewVersion = new DirectoryInfo(retryResult.ResolvedSdkDirectory).Name; + string previewVersion = new DirectoryInfo(previewSdkDirectory).Name; logger?.LogMessage($"Found preview SDK: {previewVersion}"); return Failure( factory,