Skip to content

Commit c7ba2ca

Browse files
authored
Reverts change that breaks DOTNET_ROOT (#3844)
1 parent 4fbb74f commit c7ba2ca

File tree

8 files changed

+23
-357
lines changed

8 files changed

+23
-357
lines changed

src/Microsoft.TestPlatform.TestHostProvider/Hosting/DotnetTestHostManager.cs

+18-51
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ public class DotnetTestHostManager : ITestRuntimeProvider2
5353
private const string TestAdapterRegexPattern = @"TestAdapter.dll";
5454
private const string PROCESSOR_ARCHITECTURE = nameof(PROCESSOR_ARCHITECTURE);
5555

56-
private static readonly Version Version6_0 = new(6, 0);
57-
5856
private readonly IDotnetHostHelper _dotnetHostHelper;
5957
private readonly IEnvironment _platformEnvironment;
6058
private readonly IProcessHelper _processHelper;
@@ -471,7 +469,24 @@ public virtual TestProcessStartInfo GetTestHostProcessStartInfo(
471469
// i.e. I've got only private install and no global installation, in this case apphost needs to use env var to locate runtime.
472470
if (testHostExeFound)
473471
{
474-
ForwardDotnetRootEnvironmentVariable(startInfo);
472+
string prefix = "VSTEST_WINAPPHOST_";
473+
string dotnetRootEnvName = $"{prefix}DOTNET_ROOT(x86)";
474+
var dotnetRoot = _environmentVariableHelper.GetEnvironmentVariable(dotnetRootEnvName);
475+
if (dotnetRoot is null)
476+
{
477+
dotnetRootEnvName = $"{prefix}DOTNET_ROOT";
478+
dotnetRoot = _environmentVariableHelper.GetEnvironmentVariable(dotnetRootEnvName);
479+
}
480+
481+
if (dotnetRoot != null)
482+
{
483+
EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: Found '{dotnetRootEnvName}' in env variables, value '{dotnetRoot}', forwarding to '{dotnetRootEnvName.Replace(prefix, string.Empty)}'");
484+
startInfo.EnvironmentVariables.Add(dotnetRootEnvName.Replace(prefix, string.Empty), dotnetRoot);
485+
}
486+
else
487+
{
488+
EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: Prefix '{prefix}*' not found in env variables");
489+
}
475490
}
476491

477492
startInfo.WorkingDirectory = sourceDirectory;
@@ -568,54 +583,6 @@ bool IsNativeModule(string modulePath)
568583
}
569584
}
570585

571-
internal /* for testing purposes */ void ForwardDotnetRootEnvironmentVariable(TestProcessStartInfo startInfo)
572-
{
573-
TPDebug.Assert(_targetFramework is not null, "Initialize must have been called before this method.");
574-
const string prefix = "VSTEST_WINAPPHOST_";
575-
const string dotnetRoot = "DOTNET_ROOT";
576-
string vstestDotnetRootEnvName = $"{prefix}{dotnetRoot}(x86)";
577-
578-
// Check if VSTEST_WINAPPHOST_DOTNET_ROOT(x86) is set, if not then looks for VSTEST_WINAPPHOST_DOTNET_ROOT.
579-
// If none of these variables is set we exit as we have nothing to forward.
580-
var vstestDotnetRootEnvValue = _environmentVariableHelper.GetEnvironmentVariable(vstestDotnetRootEnvName);
581-
if (vstestDotnetRootEnvValue is null)
582-
{
583-
vstestDotnetRootEnvName = $"{prefix}{dotnetRoot}";
584-
vstestDotnetRootEnvValue = _environmentVariableHelper.GetEnvironmentVariable(vstestDotnetRootEnvName);
585-
586-
// None of the forwarding environment variables are set so exit.
587-
if (vstestDotnetRootEnvValue is null)
588-
{
589-
EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: Prefix '{prefix}*' not found in env variables");
590-
return;
591-
}
592-
}
593-
594-
// For .NET 6.0 onward, the DOTNET_ROOT* environment variable to set was changed.
595-
// This implementation is based on the logic defined in SDK:
596-
// https://github.com/dotnet/sdk/blob/c3f8d746f4d5cd87f462d711a3caa7a4f6621826/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs#L264-L279
597-
string dotnetRootEnvName;
598-
if (Version.Parse(_targetFramework.Version) >= Version6_0)
599-
{
600-
dotnetRootEnvName = $"{dotnetRoot}_{_processHelper.GetCurrentProcessArchitecture().ToString().ToUpperInvariant()}";
601-
602-
// SDK side of TP is not checking for the .NET6.0+ environment variables so we want to make sure we
603-
// are not overriding user definition.
604-
if (_environmentVariableHelper.GetEnvironmentVariable(dotnetRootEnvName) is string dotnetRootEnvValue)
605-
{
606-
EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: Found '{vstestDotnetRootEnvName}' in env variables but also found '{dotnetRootEnvName}' with value '{dotnetRootEnvValue}'. Skipping forwarding.");
607-
return;
608-
}
609-
}
610-
else
611-
{
612-
dotnetRootEnvName = vstestDotnetRootEnvName.Replace(prefix, string.Empty);
613-
}
614-
615-
EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: Found '{vstestDotnetRootEnvName}' in env variables, value '{vstestDotnetRootEnvValue}', forwarding to '{dotnetRootEnvName}' (target framework is {_targetFramework.Name}, Version={_targetFramework.Version}).");
616-
startInfo.EnvironmentVariables!.Add(dotnetRootEnvName, vstestDotnetRootEnvValue);
617-
}
618-
619586
/// <inheritdoc/>
620587
public IEnumerable<string> GetTestPlatformExtensions(IEnumerable<string> sources, IEnumerable<string> extensions)
621588
{

test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs

-137
Original file line numberDiff line numberDiff line change
@@ -1025,143 +1025,6 @@ public async Task CleanTestHostAsyncShouldNotThrowIfTestHostIsNotStarted()
10251025
Assert.IsTrue(isVerified);
10261026
}
10271027

1028-
[TestMethod]
1029-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT(x86)", "DOTNET_ROOT(x86)")]
1030-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT", "DOTNET_ROOT")]
1031-
public void ForwardDotnetRootEnvironmentVariableWhenTargetFrameworkIsLessThanNet6SetsCorrectEnvVariable(string envVarName, string expectedForwaredName)
1032-
{
1033-
// Arrange
1034-
const string envVarValue = "c:\\SomePath";
1035-
_mockEnvironmentVariable.Reset();
1036-
_mockEnvironmentVariable.Setup(x => x.GetEnvironmentVariable(envVarName)).Returns(envVarValue);
1037-
string runSettingsXml = """
1038-
<RunSettings>
1039-
<RunConfiguration>
1040-
<TargetFrameworkVersion>net5.0</TargetFrameworkVersion>
1041-
</RunConfiguration>
1042-
</RunSettings>
1043-
""";
1044-
_dotnetHostManager.Initialize(_mockMessageLogger.Object, runSettingsXml);
1045-
1046-
var startInfo = new TestProcessStartInfo { EnvironmentVariables = new Dictionary<string, string?>() };
1047-
// Sanity check
1048-
Assert.AreEqual(0, startInfo.EnvironmentVariables.Count);
1049-
1050-
// Act
1051-
_dotnetHostManager.ForwardDotnetRootEnvironmentVariable(startInfo);
1052-
1053-
// Assert
1054-
Assert.AreEqual(1, startInfo.EnvironmentVariables.Count);
1055-
Assert.IsTrue(startInfo.EnvironmentVariables.TryGetValue(expectedForwaredName, out var actualEnvVarValue));
1056-
Assert.AreEqual(envVarValue, actualEnvVarValue);
1057-
}
1058-
1059-
[TestMethod]
1060-
[DataRow("DOTNET_ROOT(x86)", "net5.0")]
1061-
[DataRow("DOTNET_ROOT(x86)", "net6.0")]
1062-
[DataRow("DOTNET_ROOT", "net5.0")]
1063-
[DataRow("DOTNET_ROOT", "net6.0")]
1064-
[DataRow("DOTNET_ROOT_X86", "net5.0")]
1065-
[DataRow("DOTNET_ROOT_X86", "net6.0")]
1066-
[DataRow("DOTNET_ROOT_X64", "net5.0")]
1067-
[DataRow("DOTNET_ROOT_X64", "net6.0")]
1068-
[DataRow("DOTNET_ROOT_ARM64", "net5.0")]
1069-
[DataRow("DOTNET_ROOT_ARM64", "net6.0")]
1070-
public void ForwardDotnetRootEnvironmentVariableWhenIncorrectEnvVarDoesNothing(string envVarName, string framework)
1071-
{
1072-
// Arrange
1073-
const string envVarValue = "c:\\SomePath";
1074-
_mockEnvironmentVariable.Reset();
1075-
_mockEnvironmentVariable.Setup(x => x.GetEnvironmentVariable(envVarName)).Returns(envVarValue);
1076-
string runSettingsXml = $"""
1077-
<RunSettings>
1078-
<RunConfiguration>
1079-
<TargetFrameworkVersion>{framework}</TargetFrameworkVersion>
1080-
</RunConfiguration>
1081-
</RunSettings>
1082-
""";
1083-
_dotnetHostManager.Initialize(_mockMessageLogger.Object, runSettingsXml);
1084-
1085-
var startInfo = new TestProcessStartInfo { EnvironmentVariables = new Dictionary<string, string?>() };
1086-
1087-
// Act
1088-
_dotnetHostManager.ForwardDotnetRootEnvironmentVariable(startInfo);
1089-
1090-
// Assert
1091-
Assert.AreEqual(0, startInfo.EnvironmentVariables.Count);
1092-
}
1093-
1094-
[TestMethod]
1095-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT(x86)", PlatformArchitecture.X86)]
1096-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT(x86)", PlatformArchitecture.X64)]
1097-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT", PlatformArchitecture.X86)]
1098-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT", PlatformArchitecture.X64)]
1099-
public void ForwardDotnetRootEnvironmentVariableWhenTargetFrameworkIsGreaterOrEqualsToNet6SetsCorrectEnvVariable(string envVarName, PlatformArchitecture platformArchitecture)
1100-
{
1101-
// Arrange
1102-
const string envVarValue = "c:\\SomePath";
1103-
_mockEnvironmentVariable.Reset();
1104-
_mockEnvironmentVariable.Setup(x => x.GetEnvironmentVariable(envVarName)).Returns(envVarValue);
1105-
_mockProcessHelper.Setup(x => x.GetCurrentProcessArchitecture()).Returns(platformArchitecture);
1106-
string runSettingsXml = """
1107-
<RunSettings>
1108-
<RunConfiguration>
1109-
<TargetFrameworkVersion>net6.0</TargetFrameworkVersion>
1110-
</RunConfiguration>
1111-
</RunSettings>
1112-
""";
1113-
_dotnetHostManager.Initialize(_mockMessageLogger.Object, runSettingsXml);
1114-
1115-
var startInfo = new TestProcessStartInfo { EnvironmentVariables = new Dictionary<string, string?>() };
1116-
// Sanity check
1117-
Assert.AreEqual(0, startInfo.EnvironmentVariables.Count);
1118-
1119-
// Act
1120-
_dotnetHostManager.ForwardDotnetRootEnvironmentVariable(startInfo);
1121-
1122-
// Assert
1123-
Assert.AreEqual(1, startInfo.EnvironmentVariables.Count);
1124-
Assert.IsTrue(startInfo.EnvironmentVariables.TryGetValue($"DOTNET_ROOT_{platformArchitecture.ToString().ToUpperInvariant()}", out var actualEnvVarValue));
1125-
Assert.AreEqual(envVarValue, actualEnvVarValue);
1126-
}
1127-
1128-
[TestMethod]
1129-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT(x86)", PlatformArchitecture.X86)]
1130-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT(x86)", PlatformArchitecture.X64)]
1131-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT(x86)", PlatformArchitecture.ARM64)]
1132-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT", PlatformArchitecture.X86)]
1133-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT", PlatformArchitecture.X64)]
1134-
[DataRow("VSTEST_WINAPPHOST_DOTNET_ROOT", PlatformArchitecture.ARM64)]
1135-
public void ForwardDotnetRootEnvironmentVariableWhenTargetFrameworkIsGreaterOrEqualsToNet6DoesNotOverrideEnvVar(string envVarName, PlatformArchitecture platformArchitecture)
1136-
{
1137-
// Arrange
1138-
const string expectedEnvVarValue = "c:\\SomePath";
1139-
const string nonForwardedEnvVarValue = "C:\\SomeOtherPath";
1140-
var expectedForwardedEnvVarName = $"DOTNET_ROOT_{platformArchitecture.ToString().ToUpperInvariant()}";
1141-
_mockEnvironmentVariable.Reset();
1142-
_mockEnvironmentVariable.Setup(x => x.GetEnvironmentVariable(envVarName)).Returns(expectedEnvVarValue);
1143-
_mockEnvironmentVariable.Setup(x => x.GetEnvironmentVariable(expectedForwardedEnvVarName)).Returns(nonForwardedEnvVarValue);
1144-
_mockProcessHelper.Setup(x => x.GetCurrentProcessArchitecture()).Returns(platformArchitecture);
1145-
string runSettingsXml = """
1146-
<RunSettings>
1147-
<RunConfiguration>
1148-
<TargetFrameworkVersion>net6.0</TargetFrameworkVersion>
1149-
</RunConfiguration>
1150-
</RunSettings>
1151-
""";
1152-
_dotnetHostManager.Initialize(_mockMessageLogger.Object, runSettingsXml);
1153-
1154-
var startInfo = new TestProcessStartInfo { EnvironmentVariables = new Dictionary<string, string?>() };
1155-
// Sanity check
1156-
Assert.AreEqual(0, startInfo.EnvironmentVariables.Count);
1157-
1158-
// Act
1159-
_dotnetHostManager.ForwardDotnetRootEnvironmentVariable(startInfo);
1160-
1161-
// Assert
1162-
Assert.AreEqual(0, startInfo.EnvironmentVariables.Count);
1163-
}
1164-
11651028
private void DotnetHostManagerExitCodeTesterHostExited(object? sender, HostProviderEventArgs e)
11661029
{
11671030
_errorMessage = e.Data.TrimEnd(Environment.NewLine.ToCharArray());

test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBase.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public void InvokeVsTest(string? arguments, Dictionary<string, string?>? environ
193193
/// Invokes our local copy of dotnet that is patched with artifacts from the build with specified arguments.
194194
/// </summary>
195195
/// <param name="arguments">Arguments provided to <c>vstest.console</c>.exe</param>
196-
public void InvokeDotnetTest(string arguments, Dictionary<string, string?>? environmentVariables = null, bool useDotnetFromTools = false, string? workingDirectory = null)
196+
public void InvokeDotnetTest(string arguments, Dictionary<string, string?>? environmentVariables = null)
197197
{
198198
var debugEnvironmentVariables = AddDebugEnvironmentVariables(environmentVariables);
199199

@@ -208,7 +208,7 @@ public void InvokeDotnetTest(string arguments, Dictionary<string, string?>? envi
208208
// https://github.com/dotnet/sdk/blob/main/src/Cli/dotnet/commands/dotnet-test/VSTestForwardingApp.cs#L30-L39
209209
debugEnvironmentVariables["VSTEST_CONSOLE_PATH"] = vstestConsolePath;
210210

211-
ExecutePatchedDotnet("test", arguments, out _standardTestOutput, out _standardTestError, out _runnerExitCode, debugEnvironmentVariables, useDotnetFromTools, workingDirectory);
211+
ExecutePatchedDotnet("test", arguments, out _standardTestOutput, out _standardTestError, out _runnerExitCode, debugEnvironmentVariables);
212212
FormatStandardOutCome();
213213
}
214214

@@ -762,7 +762,7 @@ protected void ExecuteVsTestConsole(string? args, out string stdOut, out string
762762
/// <param name="stdError"></param>
763763
/// <param name="exitCode"></param>
764764
private void ExecutePatchedDotnet(string command, string args, out string stdOut, out string stdError, out int exitCode,
765-
Dictionary<string, string?>? environmentVariables = null, bool useDotnetFromTools = false, string? workingDirectory = null)
765+
Dictionary<string, string?>? environmentVariables = null)
766766
{
767767
if (environmentVariables is null)
768768
{
@@ -772,8 +772,8 @@ private void ExecutePatchedDotnet(string command, string args, out string stdOut
772772
environmentVariables["DOTNET_MULTILEVEL_LOOKUP"] = "0";
773773

774774
var executablePath = IsWindows ? @"dotnet\dotnet.exe" : @"dotnet-linux/dotnet";
775-
var patchedDotnetPath = Path.Combine(useDotnetFromTools ? _testEnvironment.ToolsDirectory : _testEnvironment.TestArtifactsDirectory, executablePath);
776-
ExecuteApplication(patchedDotnetPath, string.Join(" ", command, args), out stdOut, out stdError, out exitCode, environmentVariables, workingDirectory);
775+
var patchedDotnetPath = Path.Combine(_testEnvironment.TestArtifactsDirectory, executablePath);
776+
ExecuteApplication(patchedDotnetPath, string.Join(" ", command, args), out stdOut, out stdError, out exitCode, environmentVariables);
777777
}
778778

779779
protected static void ExecuteApplication(string path, string? args, out string stdOut, out string stdError, out int exitCode,

test/TestAssets/ProjectLaunch32BitsProcess/ProjectLaunch32BitsProcess.csproj

-22
This file was deleted.

test/TestAssets/ProjectLaunch32BitsProcess/Test32Bit.cs

-68
This file was deleted.

0 commit comments

Comments
 (0)