From 434ed304a720c260ee8ebe86d78a265e33de3c25 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 6 Dec 2024 18:53:51 +0100 Subject: [PATCH 01/22] add DOTNET_HOST_PATH to MSBuildSdkResolver --- .../MSBuildSdkResolver.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index a4a7b555b3bf..e8b67629be03 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -189,6 +189,13 @@ private sealed class CachedState minimumVSDefinedSDKVersion); } + string dotnetExe = Path.Combine(dotnetRoot, "dotnet.exe"); + if (File.Exists(dotnetExe)) + { + propertiesToAdd ??= new Dictionary(); + propertiesToAdd.Add("DOTNET_HOST_PATH", dotnetExe); + } + if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson) { logger?.LogMessage($"Could not resolve SDK specified in '{resolverResult.GlobalJsonPath}'. Ignoring global.json for this resolution."); @@ -207,10 +214,7 @@ private sealed class CachedState warnings.Add(Strings.GlobalJsonResolutionFailed); } - if (propertiesToAdd == null) - { - propertiesToAdd = new Dictionary(); - } + propertiesToAdd ??= new Dictionary(); propertiesToAdd.Add("SdkResolverHonoredGlobalJson", "false"); propertiesToAdd.Add("SdkResolverGlobalJsonPath", resolverResult.GlobalJsonPath); From 8e460fcc8408bcbaf14571e5e807aabafadf50c2 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 12 Dec 2024 13:32:08 +0100 Subject: [PATCH 02/22] fix tests --- .../GivenAnMSBuildSdkResolver.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index 7c29127040f7..de383a627c50 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -200,7 +200,8 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo 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((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); - result.PropertiesToAdd.Should().BeNull(); + result.PropertiesToAdd.Should().NotBeNull(); + result.PropertiesToAdd.Should().ContainKey("DOTNET_HOST_PATH"); result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); result.Warnings.Should().BeNullOrEmpty(); result.Errors.Should().BeNullOrEmpty(); @@ -274,7 +275,8 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionIn 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((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); - result.PropertiesToAdd.Count.Should().Be(2); + result.PropertiesToAdd.Count.Should().Be(3); + result.PropertiesToAdd.Should().ContainKey("DOTNET_HOST_PATH"); result.PropertiesToAdd.ContainsKey("SdkResolverHonoredGlobalJson"); result.PropertiesToAdd.ContainsKey("SdkResolverGlobalJsonPath"); result.PropertiesToAdd["SdkResolverHonoredGlobalJson"].Should().Be("false"); From 0918901aecd354b3a0f46bbe907158222f5d59a6 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 12 Dec 2024 13:41:49 +0100 Subject: [PATCH 03/22] assert count in test --- .../GivenAnMSBuildSdkResolver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index de383a627c50..a44a961eb143 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -200,7 +200,7 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo 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((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); - result.PropertiesToAdd.Should().NotBeNull(); + result.PropertiesToAdd.Count.Should().Be(1); result.PropertiesToAdd.Should().ContainKey("DOTNET_HOST_PATH"); result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); result.Warnings.Should().BeNullOrEmpty(); From 286b9456168fb6356ed737b0f3eab9db0a748568 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 12 Dec 2024 18:41:28 +0100 Subject: [PATCH 04/22] use consts to get "dotnet.exe" --- .../Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index e8b67629be03..ffc72dcaade3 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -189,7 +189,7 @@ private sealed class CachedState minimumVSDefinedSDKVersion); } - string dotnetExe = Path.Combine(dotnetRoot, "dotnet.exe"); + string dotnetExe = Path.Combine(dotnetRoot, Constants.DotNet + Constants.ExeSuffix); if (File.Exists(dotnetExe)) { propertiesToAdd ??= new Dictionary(); From eb03ff760ade0caca8fb46d899dd6dee7821ad97 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 12 Dec 2024 19:33:07 +0100 Subject: [PATCH 05/22] fix tests --- .../MSBuildSdkResolver.cs | 37 +++++++++++++++++++ .../GivenAnMSBuildSdkResolver.cs | 12 ++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index ffc72dcaade3..01cbf45d4336 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -1,7 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Generic; using System.Reflection; +using System.Text.Json; +using System.Text.Json.Nodes; using Microsoft.Build.Framework; using Microsoft.DotNet.Cli; using Microsoft.DotNet.Configurer; @@ -196,6 +199,15 @@ private sealed class CachedState propertiesToAdd.Add("DOTNET_HOST_PATH", dotnetExe); } + string? runtimeVersion = dotnetRoot != null ? + GetMSbuildRuntimeVersion(resolverResult.ResolvedSdkDirectory, dotnetRoot) : + null; + if (runtimeVersion != null) + { + propertiesToAdd ??= new Dictionary(); + propertiesToAdd.Add("MSBUILD_NET_TASKHOST_RUNTIME_VERSION", runtimeVersion); + } + if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson) { logger?.LogMessage($"Could not resolve SDK specified in '{resolverResult.GlobalJsonPath}'. Ignoring global.json for this resolution."); @@ -258,6 +270,31 @@ private sealed class CachedState return factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion, propertiesToAdd, itemsToAdd, warnings); } + private static string? GetMSbuildRuntimeVersion(string sdkDirectory, string dotnetRoot) + { + // 1. Get the runtime version from the MSBuild.runtimeconfig.json file + string runtimeConfigPath = Path.Combine(sdkDirectory, "MSBuild.runtimeconfig.json"); + if (File.Exists(runtimeConfigPath)) + { + var runtimeConfigJson = JsonNode.Parse(File.ReadAllText(runtimeConfigPath)) as JsonObject; + var runtimeOptionsFramework = runtimeConfigJson?["runtimeOptions"]?["framework"]; + string? runtimeName = runtimeOptionsFramework?["name"]?.ToString(); + string? runtimeVersion = runtimeOptionsFramework?["version"]?.ToString(); + + // 2. Check that the runtime version is installed (in shared folder) + if (runtimeName != null && runtimeVersion != null) + { + string runtimePath = Path.Combine(dotnetRoot, "shared", runtimeName, runtimeVersion); + if (Directory.Exists(runtimePath)) + { + return runtimeVersion; + } + } + } + + return null; + } + private static SdkResult Failure(SdkResultFactory factory, ResolverLogger? logger, SdkLogger sdkLogger, string format, params object?[] args) { string error = string.Format(format, args); diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index a44a961eb143..c5e629791640 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -13,6 +13,8 @@ namespace Microsoft.DotNet.Cli.Utils.Tests { public class GivenAnMSBuildSdkResolver : SdkTest { + private const string _dotnetHost = "DOTNET_HOST_PATH"; + private const string _msbuildNetTaskHostRuntimeVersion = "MSBUILD_NET_TASKHOST_RUNTIME_VERSION"; public GivenAnMSBuildSdkResolver(ITestOutputHelper logger) : base(logger) { @@ -200,8 +202,9 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo 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((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); - result.PropertiesToAdd.Count.Should().Be(1); - result.PropertiesToAdd.Should().ContainKey("DOTNET_HOST_PATH"); + result.PropertiesToAdd.Count.Should().Be(2); + result.PropertiesToAdd.Should().ContainKey(_dotnetHost); + result.PropertiesToAdd.Should().ContainKey(_msbuildNetTaskHostRuntimeVersion); result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); result.Warnings.Should().BeNullOrEmpty(); result.Errors.Should().BeNullOrEmpty(); @@ -275,8 +278,9 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionIn 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((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); - result.PropertiesToAdd.Count.Should().Be(3); - result.PropertiesToAdd.Should().ContainKey("DOTNET_HOST_PATH"); + result.PropertiesToAdd.Count.Should().Be(4); + result.PropertiesToAdd.Should().ContainKey(_dotnetHost); + result.PropertiesToAdd.Should().ContainKey(_msbuildNetTaskHostRuntimeVersion); result.PropertiesToAdd.ContainsKey("SdkResolverHonoredGlobalJson"); result.PropertiesToAdd.ContainsKey("SdkResolverGlobalJsonPath"); result.PropertiesToAdd["SdkResolverHonoredGlobalJson"].Should().Be("false"); From 1eaf3396c60501a67e25d3074f830b8f0058be1b Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 12 Dec 2024 19:54:48 +0100 Subject: [PATCH 06/22] remove unnecessary usings --- .../Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 01cbf45d4336..aff7559d7d94 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -1,9 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Collections.Generic; using System.Reflection; -using System.Text.Json; using System.Text.Json.Nodes; using Microsoft.Build.Framework; using Microsoft.DotNet.Cli; From 2f812fcfda18991089411c66207556bd3074db95 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 12 Dec 2024 22:58:41 +0100 Subject: [PATCH 07/22] fix the test --- .../GivenAnMSBuildSdkResolver.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index c5e629791640..b5a8ac0050a4 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -281,8 +281,8 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionIn result.PropertiesToAdd.Count.Should().Be(4); result.PropertiesToAdd.Should().ContainKey(_dotnetHost); result.PropertiesToAdd.Should().ContainKey(_msbuildNetTaskHostRuntimeVersion); - result.PropertiesToAdd.ContainsKey("SdkResolverHonoredGlobalJson"); - result.PropertiesToAdd.ContainsKey("SdkResolverGlobalJsonPath"); + result.PropertiesToAdd.Should().ContainKey("SdkResolverHonoredGlobalJson"); + result.PropertiesToAdd.Should().ContainKey("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 version '1.2.3' as specified by global.json, please check that the specified version is installed." }); From 7853ee3d9f896a6243c09d8c367fa5db16dea83b Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 12 Dec 2024 23:50:30 +0100 Subject: [PATCH 08/22] mock getting runtime version for tests --- .../MSBuildSdkResolver.cs | 9 ++++++--- .../GivenAnMSBuildSdkResolver.cs | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index aff7559d7d94..f1e9c028e008 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -28,6 +28,7 @@ public sealed class DotNetMSBuildSdkResolver : SdkResolver private readonly Func _getEnvironmentVariable; private readonly Func? _getCurrentProcessPath; + private readonly Func _getMsbuildRuntime; private readonly NETCoreSdkResolver _netCoreSdkResolver; private static CachingWorkloadResolver _staticWorkloadResolver = new(); @@ -35,12 +36,12 @@ public sealed class DotNetMSBuildSdkResolver : SdkResolver private bool _shouldLog = false; public DotNetMSBuildSdkResolver() - : this(Environment.GetEnvironmentVariable, null, VSSettings.Ambient) + : this(Environment.GetEnvironmentVariable, null, null, VSSettings.Ambient) { } // Test constructor - public DotNetMSBuildSdkResolver(Func getEnvironmentVariable, Func? getCurrentProcessPath, VSSettings vsSettings) + public DotNetMSBuildSdkResolver(Func getEnvironmentVariable, Func? getCurrentProcessPath, Func? getMsbuildRuntime, VSSettings vsSettings) { _getEnvironmentVariable = getEnvironmentVariable; _getCurrentProcessPath = getCurrentProcessPath; @@ -52,6 +53,8 @@ public DotNetMSBuildSdkResolver(Func getEnvironmentVariable, Fu { _shouldLog = true; } + + _getMsbuildRuntime = getMsbuildRuntime ?? GetMSbuildRuntimeVersion; } private sealed class CachedState @@ -198,7 +201,7 @@ private sealed class CachedState } string? runtimeVersion = dotnetRoot != null ? - GetMSbuildRuntimeVersion(resolverResult.ResolvedSdkDirectory, dotnetRoot) : + _getMsbuildRuntime(resolverResult.ResolvedSdkDirectory, dotnetRoot) : null; if (runtimeVersion != null) { diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index b5a8ac0050a4..82aa18378afc 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -590,6 +590,7 @@ public SdkResolver CreateResolver(bool useAmbientSettings = false) GetEnvironmentVariable, // force current executable location to be the mocked dotnet executable location () => ProcessPath, + (x, y) => "mockRuntimeVersion", useAmbientSettings ? VSSettings.Ambient : new VSSettings(VSSettingsFile?.FullName, DisallowPrereleaseByDefault)); From dcead1fccd96cdeb651dff5a48b91d3ae75ca671 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 12 Dec 2024 23:57:32 +0100 Subject: [PATCH 09/22] rename prop to MSBuildNetTaskHostRuntimeVersion --- .../Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs | 2 +- .../GivenAnMSBuildSdkResolver.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index f1e9c028e008..4cfc8f32451e 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -206,7 +206,7 @@ private sealed class CachedState if (runtimeVersion != null) { propertiesToAdd ??= new Dictionary(); - propertiesToAdd.Add("MSBUILD_NET_TASKHOST_RUNTIME_VERSION", runtimeVersion); + propertiesToAdd.Add("MSBuildNetTaskHostRuntimeVersion", runtimeVersion); } if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson) diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index 82aa18378afc..c8380d41bba4 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -14,7 +14,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests public class GivenAnMSBuildSdkResolver : SdkTest { private const string _dotnetHost = "DOTNET_HOST_PATH"; - private const string _msbuildNetTaskHostRuntimeVersion = "MSBUILD_NET_TASKHOST_RUNTIME_VERSION"; + private const string _msbuildNetTaskHostRuntimeVersion = "MSBuildNetTaskHostRuntimeVersion"; public GivenAnMSBuildSdkResolver(ITestOutputHelper logger) : base(logger) { From 877ff7cd7a32a6da8dfa62ea28c4ccc7fd0e5bdf Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 11:31:25 +0100 Subject: [PATCH 10/22] rename to SdkResolverMSBuildTaskHostRuntimeVersion --- .../Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs | 2 +- .../GivenAnMSBuildSdkResolver.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 4cfc8f32451e..1f481243a31f 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -206,7 +206,7 @@ private sealed class CachedState if (runtimeVersion != null) { propertiesToAdd ??= new Dictionary(); - propertiesToAdd.Add("MSBuildNetTaskHostRuntimeVersion", runtimeVersion); + propertiesToAdd.Add("SdkResolverMSBuildTaskHostRuntimeVersion", runtimeVersion); } if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson) diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index c8380d41bba4..ab388a8e1eb4 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -14,7 +14,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests public class GivenAnMSBuildSdkResolver : SdkTest { private const string _dotnetHost = "DOTNET_HOST_PATH"; - private const string _msbuildNetTaskHostRuntimeVersion = "MSBuildNetTaskHostRuntimeVersion"; + private const string _msbuildNetTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; public GivenAnMSBuildSdkResolver(ITestOutputHelper logger) : base(logger) { From d2780f329db83cdab11bacbea8eda0bd089ec89e Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 11:40:55 +0100 Subject: [PATCH 11/22] small refactoring --- .../MSBuildSdkResolver.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 1f481243a31f..230accf8ce7a 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -36,16 +36,17 @@ public sealed class DotNetMSBuildSdkResolver : SdkResolver private bool _shouldLog = false; public DotNetMSBuildSdkResolver() - : this(Environment.GetEnvironmentVariable, null, null, VSSettings.Ambient) + : this(Environment.GetEnvironmentVariable, null, GetMSbuildRuntimeVersion, VSSettings.Ambient) { } // Test constructor - public DotNetMSBuildSdkResolver(Func getEnvironmentVariable, Func? getCurrentProcessPath, Func? getMsbuildRuntime, VSSettings vsSettings) + public DotNetMSBuildSdkResolver(Func getEnvironmentVariable, Func? getCurrentProcessPath, Func getMsbuildRuntime, VSSettings vsSettings) { _getEnvironmentVariable = getEnvironmentVariable; _getCurrentProcessPath = getCurrentProcessPath; _netCoreSdkResolver = new NETCoreSdkResolver(getEnvironmentVariable, vsSettings); + _getMsbuildRuntime = getMsbuildRuntime; if (_getEnvironmentVariable(EnvironmentVariableNames.DOTNET_MSBUILD_SDK_RESOLVER_ENABLE_LOG) is string val && (string.Equals(val, "true", StringComparison.OrdinalIgnoreCase) || @@ -53,8 +54,6 @@ public DotNetMSBuildSdkResolver(Func getEnvironmentVariable, Fu { _shouldLog = true; } - - _getMsbuildRuntime = getMsbuildRuntime ?? GetMSbuildRuntimeVersion; } private sealed class CachedState From f873b882ddfbc31cc65ca7a9b93e379360b019b6 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 17:03:09 +0100 Subject: [PATCH 12/22] rename variable --- .../GivenAnMSBuildSdkResolver.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index ab388a8e1eb4..3688e35d72a8 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -14,7 +14,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests public class GivenAnMSBuildSdkResolver : SdkTest { private const string _dotnetHost = "DOTNET_HOST_PATH"; - private const string _msbuildNetTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; + private const string _msbuildTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; public GivenAnMSBuildSdkResolver(ITestOutputHelper logger) : base(logger) { @@ -204,7 +204,7 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo result.AdditionalPaths.Should().BeNull(); result.PropertiesToAdd.Count.Should().Be(2); result.PropertiesToAdd.Should().ContainKey(_dotnetHost); - result.PropertiesToAdd.Should().ContainKey(_msbuildNetTaskHostRuntimeVersion); + result.PropertiesToAdd.Should().ContainKey(_msbuildTaskHostRuntimeVersion); result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); result.Warnings.Should().BeNullOrEmpty(); result.Errors.Should().BeNullOrEmpty(); @@ -280,7 +280,7 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionIn result.AdditionalPaths.Should().BeNull(); result.PropertiesToAdd.Count.Should().Be(4); result.PropertiesToAdd.Should().ContainKey(_dotnetHost); - result.PropertiesToAdd.Should().ContainKey(_msbuildNetTaskHostRuntimeVersion); + result.PropertiesToAdd.Should().ContainKey(_msbuildTaskHostRuntimeVersion); result.PropertiesToAdd.Should().ContainKey("SdkResolverHonoredGlobalJson"); result.PropertiesToAdd.Should().ContainKey("SdkResolverGlobalJsonPath"); result.PropertiesToAdd["SdkResolverHonoredGlobalJson"].Should().Be("false"); From 3fd448adf81762129b4c6cc7b28b482bb8fc113f Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 17:19:05 +0100 Subject: [PATCH 13/22] check string is null or empty; check only "dotnet.exe"; log if couldn't set DOTNET_HOST_PATH and SdkResolverMSBuildTaskHostRuntimeVersion --- .../MSBuildSdkResolver.cs | 21 ++++++++++++++----- .../Constants.cs | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 230accf8ce7a..8540f3e6319b 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -31,6 +31,9 @@ public sealed class DotNetMSBuildSdkResolver : SdkResolver private readonly Func _getMsbuildRuntime; private readonly NETCoreSdkResolver _netCoreSdkResolver; + private const string _dotnetHost = "DOTNET_HOST_PATH"; + private const string _msbuildTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; + private static CachingWorkloadResolver _staticWorkloadResolver = new(); private bool _shouldLog = false; @@ -192,20 +195,28 @@ private sealed class CachedState minimumVSDefinedSDKVersion); } - string dotnetExe = Path.Combine(dotnetRoot, Constants.DotNet + Constants.ExeSuffix); + string dotnetExe = Path.Combine(dotnetRoot, Constants.DotNetExe); if (File.Exists(dotnetExe)) { propertiesToAdd ??= new Dictionary(); - propertiesToAdd.Add("DOTNET_HOST_PATH", dotnetExe); + propertiesToAdd.Add(_dotnetHost, dotnetExe); + } + else + { + logger?.LogMessage($"Could set '{_dotnetHost}' because dotnet executable does not exists at '{dotnetExe}'."); } string? runtimeVersion = dotnetRoot != null ? _getMsbuildRuntime(resolverResult.ResolvedSdkDirectory, dotnetRoot) : null; - if (runtimeVersion != null) + if (!string.IsNullOrEmpty(runtimeVersion)) { propertiesToAdd ??= new Dictionary(); - propertiesToAdd.Add("SdkResolverMSBuildTaskHostRuntimeVersion", runtimeVersion); + propertiesToAdd.Add(_msbuildTaskHostRuntimeVersion, runtimeVersion); + } + else + { + logger?.LogMessage($"Could not set '{_msbuildTaskHostRuntimeVersion}' because runtime version could not be determined."); } if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson) @@ -282,7 +293,7 @@ private sealed class CachedState string? runtimeVersion = runtimeOptionsFramework?["version"]?.ToString(); // 2. Check that the runtime version is installed (in shared folder) - if (runtimeName != null && runtimeVersion != null) + if (!string.IsNullOrEmpty(runtimeName) && !string.IsNullOrEmpty(runtimeVersion)) { string runtimePath = Path.Combine(dotnetRoot, "shared", runtimeName, runtimeVersion); if (Directory.Exists(runtimePath)) diff --git a/src/Resolvers/Microsoft.DotNet.NativeWrapper/Constants.cs b/src/Resolvers/Microsoft.DotNet.NativeWrapper/Constants.cs index 29d0dbcd3daa..998663b58204 100644 --- a/src/Resolvers/Microsoft.DotNet.NativeWrapper/Constants.cs +++ b/src/Resolvers/Microsoft.DotNet.NativeWrapper/Constants.cs @@ -7,6 +7,7 @@ internal static class Constants { public const string HostFxr = "hostfxr"; public const string DotNet = "dotnet"; + public const string DotNetExe = "dotnet.exe"; public const string PATH = "PATH"; public const string DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR = "DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR"; From 6f8431626ca2ef0eee606451c08471d74ac3a050 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 17:35:16 +0100 Subject: [PATCH 14/22] read json fields in a stream --- .../MSBuildSdkResolver.cs | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 8540f3e6319b..6792ef70bffc 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Reflection; -using System.Text.Json.Nodes; +using System.Text.Json; using Microsoft.Build.Framework; using Microsoft.DotNet.Cli; using Microsoft.DotNet.Configurer; @@ -285,25 +285,21 @@ private sealed class CachedState { // 1. Get the runtime version from the MSBuild.runtimeconfig.json file string runtimeConfigPath = Path.Combine(sdkDirectory, "MSBuild.runtimeconfig.json"); - if (File.Exists(runtimeConfigPath)) - { - var runtimeConfigJson = JsonNode.Parse(File.ReadAllText(runtimeConfigPath)) as JsonObject; - var runtimeOptionsFramework = runtimeConfigJson?["runtimeOptions"]?["framework"]; - string? runtimeName = runtimeOptionsFramework?["name"]?.ToString(); - string? runtimeVersion = runtimeOptionsFramework?["version"]?.ToString(); + if (!File.Exists(runtimeConfigPath)) return null; - // 2. Check that the runtime version is installed (in shared folder) - if (!string.IsNullOrEmpty(runtimeName) && !string.IsNullOrEmpty(runtimeVersion)) - { - string runtimePath = Path.Combine(dotnetRoot, "shared", runtimeName, runtimeVersion); - if (Directory.Exists(runtimePath)) - { - return runtimeVersion; - } - } - } + using var stream = File.OpenRead(runtimeConfigPath); + using var jsonDoc = JsonDocument.Parse(stream); + + JsonElement root = jsonDoc.RootElement; + if (!root.TryGetProperty("runtimeOptions", out JsonElement runtimeOptions) || + !runtimeOptions.TryGetProperty("framework", out JsonElement framework)) return null; + + string? runtimeName = framework.GetProperty("name").GetString(); + string? runtimeVersion = framework.GetProperty("version").GetString(); - return null; + return (!string.IsNullOrEmpty(runtimeName) && !string.IsNullOrEmpty(runtimeVersion) && + Directory.Exists(Path.Combine(dotnetRoot, "shared", runtimeName, runtimeVersion))) + ? runtimeVersion : null; } private static SdkResult Failure(SdkResultFactory factory, ResolverLogger? logger, SdkLogger sdkLogger, string format, params object?[] args) From 6b86aa70f8af2338702dacf044f336e16985eed8 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 17:37:08 +0100 Subject: [PATCH 15/22] add comment --- .../Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 6792ef70bffc..837d8b9edcf9 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -297,6 +297,7 @@ private sealed class CachedState string? runtimeName = framework.GetProperty("name").GetString(); string? runtimeVersion = framework.GetProperty("version").GetString(); + // 2. Check that the runtime version is installed (in shared folder) return (!string.IsNullOrEmpty(runtimeName) && !string.IsNullOrEmpty(runtimeVersion) && Directory.Exists(Path.Combine(dotnetRoot, "shared", runtimeName, runtimeVersion))) ? runtimeVersion : null; From f6020e05cd8e31b4d55aee028acb93cc97eead37 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 17:50:55 +0100 Subject: [PATCH 16/22] fix message --- .../Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 837d8b9edcf9..a428d9667171 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -203,7 +203,7 @@ private sealed class CachedState } else { - logger?.LogMessage($"Could set '{_dotnetHost}' because dotnet executable does not exists at '{dotnetExe}'."); + logger?.LogMessage($"Could not set '{_dotnetHost}' because dotnet executable '{dotnetExe}' does not exist."); } string? runtimeVersion = dotnetRoot != null ? From 6aab2956d66eac2f661842bab7c2f400970fb347 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 18:05:28 +0100 Subject: [PATCH 17/22] check the value of SdkResolverMSBuildTaskHostRuntimeVersion in tests; rename const variables --- .../MSBuildSdkResolver.cs | 12 ++++++------ .../GivenAnMSBuildSdkResolver.cs | 14 ++++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index a428d9667171..51c8800e875e 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -31,8 +31,8 @@ public sealed class DotNetMSBuildSdkResolver : SdkResolver private readonly Func _getMsbuildRuntime; private readonly NETCoreSdkResolver _netCoreSdkResolver; - private const string _dotnetHost = "DOTNET_HOST_PATH"; - private const string _msbuildTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; + private const string DotnetHost = "DOTNET_HOST_PATH"; + private const string MsbuildTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; private static CachingWorkloadResolver _staticWorkloadResolver = new(); @@ -199,11 +199,11 @@ private sealed class CachedState if (File.Exists(dotnetExe)) { propertiesToAdd ??= new Dictionary(); - propertiesToAdd.Add(_dotnetHost, dotnetExe); + propertiesToAdd.Add(DotnetHost, dotnetExe); } else { - logger?.LogMessage($"Could not set '{_dotnetHost}' because dotnet executable '{dotnetExe}' does not exist."); + logger?.LogMessage($"Could not set '{DotnetHost}' because dotnet executable '{dotnetExe}' does not exist."); } string? runtimeVersion = dotnetRoot != null ? @@ -212,11 +212,11 @@ private sealed class CachedState if (!string.IsNullOrEmpty(runtimeVersion)) { propertiesToAdd ??= new Dictionary(); - propertiesToAdd.Add(_msbuildTaskHostRuntimeVersion, runtimeVersion); + propertiesToAdd.Add(MsbuildTaskHostRuntimeVersion, runtimeVersion); } else { - logger?.LogMessage($"Could not set '{_msbuildTaskHostRuntimeVersion}' because runtime version could not be determined."); + logger?.LogMessage($"Could not set '{MsbuildTaskHostRuntimeVersion}' because runtime version could not be determined."); } if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson) diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index 3688e35d72a8..ce2ceceb0515 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -13,8 +13,8 @@ namespace Microsoft.DotNet.Cli.Utils.Tests { public class GivenAnMSBuildSdkResolver : SdkTest { - private const string _dotnetHost = "DOTNET_HOST_PATH"; - private const string _msbuildTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; + private const string DotnetHost = "DOTNET_HOST_PATH"; + private const string MSBuildTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; public GivenAnMSBuildSdkResolver(ITestOutputHelper logger) : base(logger) { @@ -203,8 +203,9 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo result.Path.Should().Be((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); result.PropertiesToAdd.Count.Should().Be(2); - result.PropertiesToAdd.Should().ContainKey(_dotnetHost); - result.PropertiesToAdd.Should().ContainKey(_msbuildTaskHostRuntimeVersion); + result.PropertiesToAdd.Should().ContainKey(DotnetHost); + result.PropertiesToAdd.Should().ContainKey(MSBuildTaskHostRuntimeVersion); + result.PropertiesToAdd[MSBuildTaskHostRuntimeVersion].Should().Be("mockRuntimeVersion"); result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); result.Warnings.Should().BeNullOrEmpty(); result.Errors.Should().BeNullOrEmpty(); @@ -279,8 +280,9 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionIn result.Path.Should().Be((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); result.PropertiesToAdd.Count.Should().Be(4); - result.PropertiesToAdd.Should().ContainKey(_dotnetHost); - result.PropertiesToAdd.Should().ContainKey(_msbuildTaskHostRuntimeVersion); + result.PropertiesToAdd.Should().ContainKey(DotnetHost); + result.PropertiesToAdd.Should().ContainKey(MSBuildTaskHostRuntimeVersion); + result.PropertiesToAdd[MSBuildTaskHostRuntimeVersion].Should().Be("mockRuntimeVersion"); result.PropertiesToAdd.Should().ContainKey("SdkResolverHonoredGlobalJson"); result.PropertiesToAdd.Should().ContainKey("SdkResolverGlobalJsonPath"); result.PropertiesToAdd["SdkResolverHonoredGlobalJson"].Should().Be("false"); From ab767e26bf10a3cd380a9461735fca6cc74d3578 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 13 Dec 2024 18:08:21 +0100 Subject: [PATCH 18/22] rename const --- .../MSBuildSdkResolver.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 51c8800e875e..d05e5f369ce7 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -32,7 +32,7 @@ public sealed class DotNetMSBuildSdkResolver : SdkResolver private readonly NETCoreSdkResolver _netCoreSdkResolver; private const string DotnetHost = "DOTNET_HOST_PATH"; - private const string MsbuildTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; + private const string MSBuildTaskHostRuntimeVersion = "SdkResolverMSBuildTaskHostRuntimeVersion"; private static CachingWorkloadResolver _staticWorkloadResolver = new(); @@ -212,11 +212,11 @@ private sealed class CachedState if (!string.IsNullOrEmpty(runtimeVersion)) { propertiesToAdd ??= new Dictionary(); - propertiesToAdd.Add(MsbuildTaskHostRuntimeVersion, runtimeVersion); + propertiesToAdd.Add(MSBuildTaskHostRuntimeVersion, runtimeVersion); } else { - logger?.LogMessage($"Could not set '{MsbuildTaskHostRuntimeVersion}' because runtime version could not be determined."); + logger?.LogMessage($"Could not set '{MSBuildTaskHostRuntimeVersion}' because runtime version could not be determined."); } if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson) From e9ad6d2fc2915ebb90d00602f9a329acb41f47b9 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Sat, 14 Dec 2024 14:02:57 +0100 Subject: [PATCH 19/22] check that dotnetRoot is not null --- .../MSBuildSdkResolver.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index d05e5f369ce7..f1507b8b9f92 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -195,8 +195,10 @@ private sealed class CachedState minimumVSDefinedSDKVersion); } - string dotnetExe = Path.Combine(dotnetRoot, Constants.DotNetExe); - if (File.Exists(dotnetExe)) + string? dotnetExe = !string.IsNullOrEmpty(dotnetRoot) ? + Path.Combine(dotnetRoot, Constants.DotNetExe) : + null; + if (!string.IsNullOrEmpty(dotnetExe) && File.Exists(dotnetExe)) { propertiesToAdd ??= new Dictionary(); propertiesToAdd.Add(DotnetHost, dotnetExe); From bb2c730a786cf0ee1a6e1ff67f674aa33c3ea514 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Sat, 14 Dec 2024 14:59:18 +0100 Subject: [PATCH 20/22] small fix --- .../Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index f1507b8b9f92..4d1198753afa 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -195,7 +195,7 @@ private sealed class CachedState minimumVSDefinedSDKVersion); } - string? dotnetExe = !string.IsNullOrEmpty(dotnetRoot) ? + string? dotnetExe = dotnetRoot != null ? Path.Combine(dotnetRoot, Constants.DotNetExe) : null; if (!string.IsNullOrEmpty(dotnetExe) && File.Exists(dotnetExe)) From ce5d0b69e18b5d2af956aadc3e4e46d84882eb84 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Sat, 14 Dec 2024 15:08:07 +0100 Subject: [PATCH 21/22] small fix --- .../Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs index 4d1198753afa..1f9535fc7fce 100644 --- a/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs +++ b/src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs @@ -198,7 +198,7 @@ private sealed class CachedState string? dotnetExe = dotnetRoot != null ? Path.Combine(dotnetRoot, Constants.DotNetExe) : null; - if (!string.IsNullOrEmpty(dotnetExe) && File.Exists(dotnetExe)) + if (File.Exists(dotnetExe)) { propertiesToAdd ??= new Dictionary(); propertiesToAdd.Add(DotnetHost, dotnetExe); From 817ed91653f6d4c8d3ddb6e735bfb2c7e2bd0b25 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Sun, 15 Dec 2024 18:13:53 +0100 Subject: [PATCH 22/22] fix test for unix --- .../GivenAnMSBuildSdkResolver.cs | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs index ce2ceceb0515..ba6bee057b4d 100644 --- a/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs +++ b/test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs @@ -202,8 +202,16 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuild(bool disallo 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((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); - result.PropertiesToAdd.Count.Should().Be(2); - result.PropertiesToAdd.Should().ContainKey(DotnetHost); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // DotnetHost is the path to dotnet.exe. Can be only on Windows. + result.PropertiesToAdd.Count.Should().Be(2); + result.PropertiesToAdd.Should().ContainKey(DotnetHost); + } + else + { + result.PropertiesToAdd.Count.Should().Be(1); + } result.PropertiesToAdd.Should().ContainKey(MSBuildTaskHostRuntimeVersion); result.PropertiesToAdd[MSBuildTaskHostRuntimeVersion].Should().Be("mockRuntimeVersion"); result.Version.Should().Be(disallowPreviews ? "98.98.98" : "99.99.99-preview"); @@ -279,8 +287,16 @@ public void ItReturnsHighestSdkAvailableThatIsCompatibleWithMSBuildWhenVersionIn 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((disallowPreviews ? compatibleRtm : compatiblePreview).FullName); result.AdditionalPaths.Should().BeNull(); - result.PropertiesToAdd.Count.Should().Be(4); - result.PropertiesToAdd.Should().ContainKey(DotnetHost); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // DotnetHost is the path to dotnet.exe. Can be only on Windows. + result.PropertiesToAdd.Count.Should().Be(4); + result.PropertiesToAdd.Should().ContainKey(DotnetHost); + } + else + { + result.PropertiesToAdd.Count.Should().Be(3); + } result.PropertiesToAdd.Should().ContainKey(MSBuildTaskHostRuntimeVersion); result.PropertiesToAdd[MSBuildTaskHostRuntimeVersion].Should().Be("mockRuntimeVersion"); result.PropertiesToAdd.Should().ContainKey("SdkResolverHonoredGlobalJson");