Skip to content
Closed
Changes from all 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
38 changes: 35 additions & 3 deletions src/Microsoft.TestPlatform.Build/Tasks/VSTestTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

using Microsoft.Build.Framework;
Expand All @@ -15,8 +17,6 @@ public class VSTestTask : Task, ITestTask
{
private int _activeProcessId;

private const string DotnetExe = "dotnet";

[Required]
public ITaskItem? TestFileFullPath { get; set; }
public string? VSTestSetting { get; set; }
Expand Down Expand Up @@ -73,7 +73,7 @@ public override bool Execute()

var processInfo = new ProcessStartInfo
{
FileName = DotnetExe,
FileName = ResolveDotnetPath(),
Arguments = TestTaskUtils.CreateCommandLineArguments(this),
UseShellExecute = false,
};
Expand All @@ -95,6 +95,38 @@ public override bool Execute()
return activeProcess.ExitCode == 0;
}

private static string ResolveDotnetPath()
{
var dotnetExe = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet.exe" : "dotnet";

var dotnetHostPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
if (!dotnetHostPath.IsNullOrEmpty())
{
var path = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(dotnetHostPath))!, dotnetExe);
if (File.Exists(path))
{
return path;
}
}

if (File.Exists(dotnetExe))
{
return Path.GetFullPath(dotnetExe);
}

var values = Environment.GetEnvironmentVariable("PATH");
foreach (var p in values!.Split(Path.PathSeparator))
{
var fullPath = Path.Combine(p, dotnetExe);
if (File.Exists(fullPath))
{
return fullPath;
}
}

return dotnetExe;
}

public void Cancel()
{
Tracing.Trace("VSTest: Killing the process...");
Expand Down