Skip to content

Commit 0c167a0

Browse files
Youssef1313Copilot
andauthored
Mimic dotnet run handling of DOTNET_ROOT in dotnet test (#50047)
Co-authored-by: Copilot <[email protected]>
1 parent 9ce4f15 commit 0c167a0

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public bool MoveNext()
112112
}
113113

114114

115-
internal sealed record TestModule(RunProperties RunProperties, string? ProjectFullPath, string? TargetFramework, bool IsTestingPlatformApplication, bool IsTestProject, ProjectLaunchSettingsModel? LaunchSettings, string TargetPath);
115+
internal sealed record TestModule(RunProperties RunProperties, string? ProjectFullPath, string? TargetFramework, bool IsTestingPlatformApplication, bool IsTestProject, ProjectLaunchSettingsModel? LaunchSettings, string TargetPath, string? DotnetRootArchVariableName);
116116

117117
internal sealed record Handshake(Dictionary<byte, string>? Properties);
118118

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.CommandLine;
45
using System.Diagnostics;
56
using Microsoft.Build.Evaluation;
67
using Microsoft.Build.Execution;
@@ -250,7 +251,17 @@ public static IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModule
250251
// TODO: Support --launch-profile and pass it here.
251252
var launchSettings = TryGetLaunchProfileSettings(Path.GetDirectoryName(projectFullPath)!, Path.GetFileNameWithoutExtension(projectFullPath), project.GetPropertyValue(ProjectProperties.AppDesignerFolder), buildOptions, profileName: null);
252253

253-
return new TestModule(runProperties, PathUtility.FixFilePath(projectFullPath), targetFramework, isTestingPlatformApplication, isTestProject, launchSettings, project.GetPropertyValue(ProjectProperties.TargetPath));
254+
var rootVariableName = EnvironmentVariableNames.TryGetDotNetRootArchVariableName(
255+
project.GetPropertyValue("RuntimeIdentifier"),
256+
project.GetPropertyValue("DefaultAppHostRuntimeIdentifier"));
257+
258+
if (rootVariableName is not null && Environment.GetEnvironmentVariable(rootVariableName) != null)
259+
{
260+
// If already set, we do not override it.
261+
rootVariableName = null;
262+
}
263+
264+
return new TestModule(runProperties, PathUtility.FixFilePath(projectFullPath), targetFramework, isTestingPlatformApplication, isTestProject, launchSettings, project.GetPropertyValue(ProjectProperties.TargetPath), rootVariableName);
254265

255266
static RunProperties GetRunProperties(ProjectInstance project, ICollection<ILogger>? loggers)
256267
{

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#nullable disable
55

6+
using System.CommandLine;
67
using System.Diagnostics;
78
using System.IO.Pipes;
89
using Microsoft.DotNet.Cli.Commands.Test.IPC;
@@ -62,7 +63,7 @@ private ProcessStartInfo CreateProcessStartInfo(TestOptions testOptions)
6263
FileName = Module.RunProperties.RunCommand,
6364
Arguments = GetArguments(testOptions),
6465
RedirectStandardOutput = true,
65-
RedirectStandardError = true
66+
RedirectStandardError = true,
6667
};
6768

6869
if (!string.IsNullOrEmpty(Module.RunProperties.RunWorkingDirectory))
@@ -75,7 +76,7 @@ private ProcessStartInfo CreateProcessStartInfo(TestOptions testOptions)
7576
foreach (var entry in Module.LaunchSettings.EnvironmentVariables)
7677
{
7778
string value = Environment.ExpandEnvironmentVariables(entry.Value);
78-
processStartInfo.EnvironmentVariables[entry.Key] = value;
79+
processStartInfo.Environment[entry.Key] = value;
7980
}
8081

8182
if (!_buildOptions.NoLaunchProfileArguments &&
@@ -85,6 +86,11 @@ private ProcessStartInfo CreateProcessStartInfo(TestOptions testOptions)
8586
}
8687
}
8788

89+
if (Module.DotnetRootArchVariableName is not null)
90+
{
91+
processStartInfo.Environment[Module.DotnetRootArchVariableName] = Path.GetDirectoryName(new Muxer().MuxerPath);
92+
}
93+
8894
return processStartInfo;
8995
}
9096

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public bool RunWithTestModulesFilter(ParseResult parseResult)
5858
? new RunProperties(muxerPath, $@"exec ""{testModule}""", null)
5959
: new RunProperties(testModule, null, null);
6060

61-
var testApp = new ParallelizableTestModuleGroupWithSequentialInnerModules(new TestModule(runProperties, null, null, true, true, null, testModule));
61+
var testApp = new ParallelizableTestModuleGroupWithSequentialInnerModules(new TestModule(runProperties, null, null, true, true, null, testModule, DotnetRootArchVariableName: null));
6262
// Write the test application to the channel
6363
_actionQueue.Enqueue(testApp);
6464
}

src/Common/EnvironmentVariableNames.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ static class EnvironmentVariableNames
3535
public static string? TryGetDotNetRootVariableName(string runtimeIdentifier, string defaultAppHostRuntimeIdentifier, Version? targetFrameworkVersion)
3636
=> TryGetDotNetRootVariableNameImpl(runtimeIdentifier, defaultAppHostRuntimeIdentifier, targetFrameworkVersion, RuntimeInformation.ProcessArchitecture, Environment.Is64BitProcess);
3737

38-
internal static string? TryGetDotNetRootVariableNameImpl(string runtimeIdentifier, string defaultAppHostRuntimeIdentifier, Version? targetFrameworkVersion, Architecture currentArchitecture, bool is64bit)
38+
internal static string? TryGetDotNetRootVariableNameImpl(string runtimeIdentifier, string defaultAppHostRuntimeIdentifier, Version? targetFrameworkVersion, Architecture currentArchitecture, bool is64bit, bool onlyUseArchSpecific = false)
3939
{
4040
// If the app targets the same architecture as SDK is running on or an unknown architecture, set DOTNET_ROOT, DOTNET_ROOT(x86) for 32-bit, DOTNET_ROOT_arch for TFM 6+.
4141
// If the app targets different architecture from the SDK, do not set DOTNET_ROOT.
4242

4343
if (!TryParseArchitecture(runtimeIdentifier, out var targetArchitecture) && !TryParseArchitecture(defaultAppHostRuntimeIdentifier, out targetArchitecture) ||
4444
targetArchitecture == currentArchitecture)
4545
{
46-
var suffix = targetFrameworkVersion != null && targetFrameworkVersion >= s_version6_0 ?
46+
var suffix = onlyUseArchSpecific || (targetFrameworkVersion != null && targetFrameworkVersion >= s_version6_0) ?
4747
$"_{currentArchitecture.ToString().ToUpperInvariant()}" :
4848
is64bit ? "" : "(x86)";
4949

@@ -53,6 +53,9 @@ static class EnvironmentVariableNames
5353
return null;
5454
}
5555

56+
internal static string? TryGetDotNetRootArchVariableName(string runtimeIdentifier, string defaultAppHostRuntimeIdentifier)
57+
=> TryGetDotNetRootVariableNameImpl(runtimeIdentifier, defaultAppHostRuntimeIdentifier, null, RuntimeInformation.ProcessArchitecture, Environment.Is64BitProcess, onlyUseArchSpecific: true);
58+
5659
internal static bool TryParseArchitecture(string runtimeIdentifier, out Architecture architecture)
5760
{
5861
// RID is [os].[version]-[architecture]-[additional qualifiers]

0 commit comments

Comments
 (0)