Skip to content

Commit

Permalink
Enable building with a dotnet not on PATH (#69186)
Browse files Browse the repository at this point in the history
* 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 #21237 and dotnet/cli#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.
  • Loading branch information
eerhardt authored Jul 24, 2023
1 parent b3c2e7b commit 7db1e56
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Compilers/Shared/RuntimeHostInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ internal static (string processFilePath, string commandLineArguments, string too

internal static bool IsCoreClrRuntime => true;

private const string DotNetHostPathEnvironmentName = "DOTNET_HOST_PATH";

/// <summary>
/// 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".
/// </summary>
/// <remarks>
/// See the following issue for rationale why only %PATH% is considered
/// https://github.com/dotnet/runtime/issues/88754
/// </remarks>
internal static string GetDotNetPathOrDefault()
{
if (Environment.GetEnvironmentVariable(DotNetHostPathEnvironmentName) is string pathToDotNet)
{
return pathToDotNet;
}

var (fileName, sep) = PlatformInformation.IsWindows
? ("dotnet.exe", ';')
: ("dotnet", ':');
Expand Down

0 comments on commit 7db1e56

Please sign in to comment.