Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable building with a dotnet not on PATH #69186

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -40,7 +41,23 @@ public static void AssertCommandLine(

var message = engine.BuildMessages.OfType<TaskCommandLineEventArgs>().Single();
var commandLine = message.CommandLine.Replace(" ", " ").Trim();
Assert.Equal($@"{RuntimeHostInfo.GetDotNetPathOrDefault()} exec ""{task.PathToManagedTool}"" {line}", commandLine);

var dotnetPath = RuntimeHostInfo.GetDotNetPathOrDefault();
var expectedCommandLine = $@"{dotnetPath} exec ""{task.PathToManagedTool}"" {line}";

bool isOnlyFileName = Path.GetFileName(dotnetPath).Length == dotnetPath.Length;
if (isOnlyFileName)
{
// When ToolTask.GenerateFullPathToTool() returns only a file name (not a path to a file), MSBuild's ToolTask
// will search the %PATH% (see https://github.com/dotnet/msbuild/blob/5410bf323451e04e99e79bcffd158e6d8d378149/src/Utilities/ToolTask.cs#L494-L513)
// and log the full path to the exe. In this case, only assert that the commandLine ends with the expected
// command line, and ignore the full path at the beginning.
Assert.EndsWith(expectedCommandLine, commandLine);
}
else
{
Assert.Equal(expectedCommandLine, commandLine);
}

compilerTask.NoConfig = true;
Assert.Equal("/noconfig", compilerTask.GenerateToolArguments());
Expand Down
37 changes: 6 additions & 31 deletions src/Compilers/Shared/RuntimeHostInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,15 @@ 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
/// Get the path to the dotnet executable. In the case the .NET SDK did not provide this information
/// in the environment 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()
{
var (fileName, sep) = PlatformInformation.IsWindows
? ("dotnet.exe", ';')
: ("dotnet", ':');

var path = Environment.GetEnvironmentVariable("PATH") ?? "";
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
foreach (var item in path.Split(sep, StringSplitOptions.RemoveEmptyEntries))
{
try
{
var filePath = Path.Combine(item, fileName);
if (File.Exists(filePath))
{
return filePath;
}
}
catch
{
// If we can't read a directory for any reason just skip it
}
}

return fileName;
}

internal static string GetDotNetPathOrDefault() =>
Environment.GetEnvironmentVariable(DotNetHostPathEnvironmentName) ??
(PlatformInformation.IsWindows ? "dotnet.exe" : "dotnet");
#else

internal static bool IsCoreClrRuntime => false;
Expand Down
Loading