Skip to content

Commit 1b4646d

Browse files
authored
Fix forwarding DOTNET_ROOT (#49634)
1 parent 2117439 commit 1b4646d

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

src/Cli/dotnet/Commands/Test/TestCommand.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,9 @@ private static TestCommand FromParseResult(ParseResult result, string[] settings
240240
}
241241
}
242242

243-
// Set DOTNET_PATH if it isn't already set in the environment as it is required
244-
// by the testhost which uses the apphost feature (Windows only).
245-
(bool hasRootVariable, string rootVariableName, string rootValue) = VSTestForwardingApp.GetRootVariable();
246-
if (!hasRootVariable)
247-
{
243+
244+
Dictionary<string, string> variables = VSTestForwardingApp.GetVSTestRootVariables();
245+
foreach (var (rootVariableName, rootValue) in variables) {
248246
testCommand.EnvironmentVariable(rootVariableName, rootValue);
249247
VSTestTrace.SafeWriteTrace(() => $"Root variable set {rootVariableName}:{rootValue}");
250248
}

src/Cli/dotnet/Commands/Test/VSTestForwardingApp.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public class VSTestForwardingApp : ForwardingApp
1414
public VSTestForwardingApp(IEnumerable<string> argsToForward)
1515
: base(GetVSTestExePath(), argsToForward)
1616
{
17-
(bool hasRootVariable, string rootVariableName, string rootValue) = GetRootVariable();
18-
if (!hasRootVariable)
17+
Dictionary<string, string> variables = GetVSTestRootVariables();
18+
foreach (var (rootVariableName, rootValue) in variables)
1919
{
2020
WithEnvironmentVariable(rootVariableName, rootValue);
2121
VSTestTrace.SafeWriteTrace(() => $"Root variable set {rootVariableName}:{rootValue}");
2222
}
23-
23+
2424
VSTestTrace.SafeWriteTrace(() => $"Forwarding to '{GetVSTestExePath()}' with args \"{argsToForward?.Aggregate((a, b) => $"{a} | {b}")}\"");
2525
}
2626

@@ -38,14 +38,17 @@ private static string GetVSTestExePath()
3838
return Path.Combine(AppContext.BaseDirectory, VstestAppName);
3939
}
4040

41-
internal static (bool hasRootVariable, string rootVariableName, string rootValue) GetRootVariable()
41+
internal static Dictionary<string, string> GetVSTestRootVariables()
4242
{
43-
string rootVariableName = Environment.Is64BitProcess ? "DOTNET_ROOT" : "DOTNET_ROOT(x86)";
44-
bool hasRootVariable = Environment.GetEnvironmentVariable(rootVariableName) != null;
45-
string rootValue = hasRootVariable ? null : Path.GetDirectoryName(new Muxer().MuxerPath);
46-
47-
// We rename env variable to support --arch switch that relies on DOTNET_ROOT/DOTNET_ROOT(x86)
48-
// We provide VSTEST_WINAPPHOST_ only in case of testhost*.exe removing VSTEST_WINAPPHOST_ prefix and passing as env vars.
49-
return (hasRootVariable, $"VSTEST_WINAPPHOST_{rootVariableName}", rootValue);
43+
// Gather the current .NET SDK dotnet.exe location and forward it to vstest.console.dll so it can use it
44+
// to setup DOTNET_ROOT for testhost.exe, to find the same installation of NET SDK that is running `dotnet test`.
45+
// This way if we have private installation of .NET SDK, the testhost.exe will be able to use the same private installation.
46+
// The way to set the environment is complicated and depends on the version of testhost, so we leave that implementation to vstest console,
47+
// 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.
48+
return new()
49+
{
50+
["VSTEST_DOTNET_ROOT_PATH"] = Path.GetDirectoryName(new Muxer().MuxerPath),
51+
["VSTEST_DOTNET_ROOT_ARCHITECTURE"] = RuntimeInformation.ProcessArchitecture.ToString()
52+
};
5053
}
5154
}

test/TestAssets/TestProjects/VSTestForwardDotnetRootEnvironmentVariables/Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void TestForwardDotnetRootEnvironmentVariables()
1616
// This project is compiled, and executed by the tests in "test/dotnet-test.Tests/GivenDotnetTestForwardDotnetRootEnvironmentVariables.cs"
1717
foreach (DictionaryEntry env in Environment.GetEnvironmentVariables())
1818
{
19-
if (env.Key.ToString().Contains("VSTEST_WINAPPHOST_"))
19+
if (env.Key.ToString().Contains("VSTEST_"))
2020
{
2121
Console.WriteLine($"{env.Key.ToString()}={env.Value.ToString()}");
2222
}

test/dotnet.Tests/CommandTests/Test/GivenDotnetTestForwardDotnetRootEnvironmentVariables.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public void ShouldForwardDotnetRootEnvironmentVariablesIfNotProvided()
3333
.Should().Contain("Total tests: 1")
3434
.And.Contain("Passed: 1")
3535
.And.Contain("Passed TestForwardDotnetRootEnvironmentVariables")
36-
.And.Contain("VSTEST_WINAPPHOST_");
36+
.And.Contain("VSTEST_DOTNET_ROOT_PATH")
37+
.And.Contain("VSTEST_DOTNET_ROOT_ARCHITECTURE");
3738
}
3839

3940
result.ExitCode.Should().Be(0);

0 commit comments

Comments
 (0)