From 7db1e56d141782245f293cbcb2038e176f97f3fe Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Mon, 24 Jul 2023 15:34:42 -0600 Subject: [PATCH] Enable building with a dotnet not on PATH (#69186) * Enable building with a dotnet not on PATH In #68918 we removed inspecting DOTNET_HOST_PATH environment variable. This broke the scenario where a specific dotnet "hive" was installed to a location not on the PATH. When the .NET SDK commands invoke a sub-process (for example MSBuild), it sets the DOTNET_HOST_PATH environment variable to tell the sub-process "this is where the dotnet.exe that invoked this command is located". See https://github.com/dotnet/roslyn/pull/21237 and https://github.com/dotnet/cli/pull/7311 for more info. This change reverts the behavior back to respect DOTNET_HOST_PATH, and if it isn't set it will just use "dotnet" and let the OS take care of finding the executable on the PATH. Fix #69150 * Fix tests to workaround MSBuild searching the PATH itself. * Respond to PR feedback. Make the change smaller until @jaredpar gets back. Only make the minimal change required, which is to check DOTNET_HOST_PATH. --- src/Compilers/Shared/RuntimeHostInfo.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Compilers/Shared/RuntimeHostInfo.cs b/src/Compilers/Shared/RuntimeHostInfo.cs index 70be40e5f43d..0f08c684f7ef 100644 --- a/src/Compilers/Shared/RuntimeHostInfo.cs +++ b/src/Compilers/Shared/RuntimeHostInfo.cs @@ -49,16 +49,20 @@ internal static (string processFilePath, string commandLineArguments, string too internal static bool IsCoreClrRuntime => true; + private const string DotNetHostPathEnvironmentName = "DOTNET_HOST_PATH"; + /// - /// Get the path to the dotnet executable. In the case the host did not provide this information - /// in the environment this will return simply "dotnet". + /// Get the path to the dotnet executable. In the case the .NET SDK did not provide this information + /// in the environment this tries to find "dotnet" on the PATH. In the case it is not found, + /// this will return simply "dotnet". /// - /// - /// See the following issue for rationale why only %PATH% is considered - /// https://github.com/dotnet/runtime/issues/88754 - /// internal static string GetDotNetPathOrDefault() { + if (Environment.GetEnvironmentVariable(DotNetHostPathEnvironmentName) is string pathToDotNet) + { + return pathToDotNet; + } + var (fileName, sep) = PlatformInformation.IsWindows ? ("dotnet.exe", ';') : ("dotnet", ':');