Skip to content

Commit

Permalink
Respond to PR feedback.
Browse files Browse the repository at this point in the history
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 committed Jul 24, 2023
1 parent 214cb7b commit e17345b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,7 @@ public static void AssertCommandLine(

var message = engine.BuildMessages.OfType<TaskCommandLineEventArgs>().Single();
var commandLine = message.CommandLine.Replace(" ", " ").Trim();

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);
}
Assert.Equal($@"{RuntimeHostInfo.GetDotNetPathOrDefault()} exec ""{task.PathToManagedTool}"" {line}", commandLine);

compilerTask.NoConfig = true;
Assert.Equal("/noconfig", compilerTask.GenerateToolArguments());
Expand Down
36 changes: 32 additions & 4 deletions src/Compilers/Shared/RuntimeHostInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,39 @@ internal static (string processFilePath, string commandLineArguments, string too

/// <summary>
/// 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".
/// in the environment this tries to find "dotnet" on the PATH. In the case it is not found,
/// this will return simply "dotnet".
/// </summary>
internal static string GetDotNetPathOrDefault() =>
Environment.GetEnvironmentVariable(DotNetHostPathEnvironmentName) ??
(PlatformInformation.IsWindows ? "dotnet.exe" : "dotnet");
internal static string GetDotNetPathOrDefault()
{
if (Environment.GetEnvironmentVariable(DotNetHostPathEnvironmentName) is string pathToDotNet)
{
return pathToDotNet;
}

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

var path = Environment.GetEnvironmentVariable("PATH") ?? "";
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;
}
#else

internal static bool IsCoreClrRuntime => false;
Expand Down

0 comments on commit e17345b

Please sign in to comment.