diff --git a/src/Cli/dotnet/Commands/Test/TestCommand.cs b/src/Cli/dotnet/Commands/Test/TestCommand.cs index 5d603c75181a..7c49bb7d0968 100644 --- a/src/Cli/dotnet/Commands/Test/TestCommand.cs +++ b/src/Cli/dotnet/Commands/Test/TestCommand.cs @@ -241,11 +241,9 @@ private static TestCommand FromParseResult(ParseResult result, string[] settings } } - // Set DOTNET_PATH if it isn't already set in the environment as it is required - // by the testhost which uses the apphost feature (Windows only). - (bool hasRootVariable, string rootVariableName, string rootValue) = VSTestForwardingApp.GetRootVariable(); - if (!hasRootVariable) - { + + Dictionary variables = VSTestForwardingApp.GetVSTestRootVariables(); + foreach (var (rootVariableName, rootValue) in variables) { testCommand.EnvironmentVariable(rootVariableName, rootValue); VSTestTrace.SafeWriteTrace(() => $"Root variable set {rootVariableName}:{rootValue}"); } diff --git a/src/Cli/dotnet/Commands/Test/VSTestForwardingApp.cs b/src/Cli/dotnet/Commands/Test/VSTestForwardingApp.cs index b8c25c009dce..fb81e15466f9 100644 --- a/src/Cli/dotnet/Commands/Test/VSTestForwardingApp.cs +++ b/src/Cli/dotnet/Commands/Test/VSTestForwardingApp.cs @@ -14,13 +14,13 @@ public class VSTestForwardingApp : ForwardingApp public VSTestForwardingApp(IEnumerable argsToForward) : base(GetVSTestExePath(), argsToForward) { - (bool hasRootVariable, string rootVariableName, string rootValue) = GetRootVariable(); - if (!hasRootVariable) + Dictionary variables = GetVSTestRootVariables(); + foreach (var (rootVariableName, rootValue) in variables) { WithEnvironmentVariable(rootVariableName, rootValue); VSTestTrace.SafeWriteTrace(() => $"Root variable set {rootVariableName}:{rootValue}"); } - + VSTestTrace.SafeWriteTrace(() => $"Forwarding to '{GetVSTestExePath()}' with args \"{argsToForward?.Aggregate((a, b) => $"{a} | {b}")}\""); } @@ -38,14 +38,17 @@ private static string GetVSTestExePath() return Path.Combine(AppContext.BaseDirectory, VstestAppName); } - internal static (bool hasRootVariable, string rootVariableName, string rootValue) GetRootVariable() + internal static Dictionary GetVSTestRootVariables() { - string rootVariableName = Environment.Is64BitProcess ? "DOTNET_ROOT" : "DOTNET_ROOT(x86)"; - bool hasRootVariable = Environment.GetEnvironmentVariable(rootVariableName) != null; - string rootValue = hasRootVariable ? null : Path.GetDirectoryName(new Muxer().MuxerPath); - - // We rename env variable to support --arch switch that relies on DOTNET_ROOT/DOTNET_ROOT(x86) - // We provide VSTEST_WINAPPHOST_ only in case of testhost*.exe removing VSTEST_WINAPPHOST_ prefix and passing as env vars. - return (hasRootVariable, $"VSTEST_WINAPPHOST_{rootVariableName}", rootValue); + // Gather the current .NET SDK dotnet.exe location and forward it to vstest.console.dll so it can use it + // to setup DOTNET_ROOT for testhost.exe, to find the same installation of NET SDK that is running `dotnet test`. + // This way if we have private installation of .NET SDK, the testhost.exe will be able to use the same private installation. + // The way to set the environment is complicated and depends on the version of testhost, so we leave that implementation to vstest console, + // we just tell it where the current .net SDK is located, and what is the architecture of it. We don't have more information than that here. + return new() + { + ["VSTEST_DOTNET_ROOT_PATH"] = Path.GetDirectoryName(new Muxer().MuxerPath), + ["VSTEST_DOTNET_ROOT_ARCHITECTURE"] = RuntimeInformation.ProcessArchitecture.ToString() + }; } } diff --git a/test/TestAssets/TestProjects/VSTestForwardDotnetRootEnvironmentVariables/Tests.cs b/test/TestAssets/TestProjects/VSTestForwardDotnetRootEnvironmentVariables/Tests.cs index 3de940ec7980..bd56746713c5 100644 --- a/test/TestAssets/TestProjects/VSTestForwardDotnetRootEnvironmentVariables/Tests.cs +++ b/test/TestAssets/TestProjects/VSTestForwardDotnetRootEnvironmentVariables/Tests.cs @@ -16,7 +16,7 @@ public void TestForwardDotnetRootEnvironmentVariables() // This project is compiled, and executed by the tests in "test/dotnet-test.Tests/GivenDotnetTestForwardDotnetRootEnvironmentVariables.cs" foreach (DictionaryEntry env in Environment.GetEnvironmentVariables()) { - if (env.Key.ToString().Contains("VSTEST_WINAPPHOST_")) + if (env.Key.ToString().Contains("VSTEST_")) { Console.WriteLine($"{env.Key.ToString()}={env.Value.ToString()}"); } diff --git a/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestForwardDotnetRootEnvironmentVariables.cs b/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestForwardDotnetRootEnvironmentVariables.cs index ec1c34a8d2dc..e7f6a68e8fbd 100644 --- a/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestForwardDotnetRootEnvironmentVariables.cs +++ b/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestForwardDotnetRootEnvironmentVariables.cs @@ -33,7 +33,8 @@ public void ShouldForwardDotnetRootEnvironmentVariablesIfNotProvided() .Should().Contain("Total tests: 1") .And.Contain("Passed: 1") .And.Contain("Passed TestForwardDotnetRootEnvironmentVariables") - .And.Contain("VSTEST_WINAPPHOST_"); + .And.Contain("VSTEST_DOTNET_ROOT_PATH") + .And.Contain("VSTEST_DOTNET_ROOT_ARCHITECTURE"); } result.ExitCode.Should().Be(0);