Skip to content

Commit d3c6439

Browse files
Fix architecture retrival (#3251)
* Fix architecture retrieval (#3250) * Use stable channel for dotnet 6 installation (#3243) Co-authored-by: Jakub Jareš <me@jakubjares.com>
1 parent 4d9f0fb commit d3c6439

File tree

9 files changed

+72
-21
lines changed

9 files changed

+72
-21
lines changed

scripts/common.lib.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ function Install-DotNetCli
9898
Write-Log "Install-DotNetCli: Get the latest dotnet cli toolset..."
9999
$dotnetInstallPath = Join-Path $env:TP_TOOLS_DIR "dotnet"
100100
New-Item -ItemType directory -Path $dotnetInstallPath -Force | Out-Null
101-
& $dotnetInstallScript -Channel 6.0 -Quality Preview -InstallDir $dotnetInstallPath -Version $env:DOTNET_CLI_VERSION
101+
& $dotnetInstallScript -Channel 6.0 -InstallDir $dotnetInstallPath -Version $env:DOTNET_CLI_VERSION
102102

103103
& $dotnetInstallScript -InstallDir "$dotnetInstallPath" -Runtime 'dotnet' -Version '2.1.30' -Channel '2.1' -Architecture x64 -NoPath
104104
$env:DOTNET_ROOT= $dotnetInstallPath
105105

106106
& $dotnetInstallScript -InstallDir "${dotnetInstallPath}_x86" -Runtime 'dotnet' -Version '2.1.30' -Channel '2.1' -Architecture x86 -NoPath
107107
${env:DOTNET_ROOT(x86)} = "${dotnetInstallPath}_x86"
108108

109-
& $dotnetInstallScript -InstallDir "$dotnetInstallPath" -Runtime 'dotnet' -Version '3.1.19' -Channel '3.1' -Architecture x64 -NoPath
109+
& $dotnetInstallScript -InstallDir "$dotnetInstallPath" -Runtime 'dotnet' -Version '3.1.22' -Channel '3.1' -Architecture x64 -NoPath
110110
$env:DOTNET_ROOT= $dotnetInstallPath
111111

112-
& $dotnetInstallScript -InstallDir "${dotnetInstallPath}_x86" -Runtime 'dotnet' -Version '3.1.19' -Channel '3.1' -Architecture x86 -NoPath
112+
& $dotnetInstallScript -InstallDir "${dotnetInstallPath}_x86" -Runtime 'dotnet' -Version '3.1.22' -Channel '3.1' -Architecture x86 -NoPath
113113
${env:DOTNET_ROOT(x86)} = "${dotnetInstallPath}_x86"
114114

115115
& $dotnetInstallScript -InstallDir "$dotnetInstallPath" -Runtime 'dotnet' -Version '5.0.10' -Channel '5.0' -Architecture x64 -NoPath

src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private bool TryGetExecutablePath(string executableBaseName, out string executab
119119

120120
public bool TryGetDotnetPathByArchitecture(PlatformArchitecture targetArchitecture, out string muxerPath)
121121
{
122-
if (this.environment.Architecture == targetArchitecture)
122+
if (this.processHelper.GetCurrentProcessArchitecture() == targetArchitecture)
123123
{
124124
string currentProcessFileName = this.processHelper.GetCurrentProcessFileName();
125125
if (Path.GetFileName(currentProcessFileName) != this.muxerName)

src/Microsoft.TestPlatform.PlatformAbstractions/common/System/ProcessHelper.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,6 @@ public int GetProcessId(object process)
184184
return proc?.Id ?? -1;
185185
}
186186

187-
/// <inheritdoc/>
188-
public PlatformArchitecture GetCurrentProcessArchitecture()
189-
{
190-
if (IntPtr.Size == 8)
191-
{
192-
return PlatformArchitecture.X64;
193-
}
194-
195-
return PlatformArchitecture.X86;
196-
}
197-
198187
/// <inheritdoc/>
199188
public string GetNativeDllDirectory()
200189
{

src/Microsoft.TestPlatform.PlatformAbstractions/net451/System/ProcessHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ public IntPtr GetProcessHandle(int processId)
2323
{
2424
return Process.GetProcessById(processId).Handle;
2525
}
26+
27+
/// <inheritdoc/>
28+
public PlatformArchitecture GetCurrentProcessArchitecture()
29+
{
30+
if (IntPtr.Size == 8)
31+
{
32+
return PlatformArchitecture.X64;
33+
}
34+
35+
return PlatformArchitecture.X86;
36+
}
2637
}
2738
}
2839

src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/ProcessHelper.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions
99
using System.Diagnostics;
1010
using System.IO;
1111
using System.Reflection;
12+
using System.Runtime.InteropServices;
1213
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;
1314

1415
public partial class ProcessHelper : IProcessHelper
@@ -26,6 +27,29 @@ public IntPtr GetProcessHandle(int processId)
2627
// If the handle has been marked invalid with SetHandleAsInvalid, this method still returns the original handle value, which can be a stale value.
2728
return Process.GetProcessById(processId).SafeHandle.DangerousGetHandle();
2829
}
30+
31+
public PlatformArchitecture GetCurrentProcessArchitecture()
32+
{
33+
switch (RuntimeInformation.ProcessArchitecture)
34+
{
35+
case Architecture.X86:
36+
return PlatformArchitecture.X86;
37+
case Architecture.X64:
38+
return PlatformArchitecture.X64;
39+
case Architecture.Arm:
40+
return PlatformArchitecture.ARM;
41+
case Architecture.Arm64:
42+
return PlatformArchitecture.ARM64;
43+
44+
// The symbolic value is only available with .NET 6
45+
// preview 6 or later, so use the numerical value for now.
46+
// case System.Runtime.InteropServices.Architecture.S390x:
47+
case (Architecture)5:
48+
return PlatformArchitecture.S390x;
49+
default:
50+
throw new NotSupportedException();
51+
}
52+
}
2953
}
3054
}
3155

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ public virtual TestProcessStartInfo GetTestHostProcessStartInfo(
407407
// We silently force x64 only if the target architecture is the default one and is not specified by user
408408
// through --arch or runsettings or -- RunConfiguration.TargetPlatform=arch
409409
bool forceToX64 = SilentlyForceToX64() && this.runsettingHelper.IsDefaultTargetArchitecture;
410-
bool isSameArchitecture = IsSameArchitecture(this.architecture, this.platformEnvironment.Architecture);
410+
EqtTrace.Verbose($"DotnetTestHostmanager: Current process architetcure '{this.processHelper.GetCurrentProcessArchitecture()}'");
411+
bool isSameArchitecture = IsSameArchitecture(this.architecture, this.processHelper.GetCurrentProcessArchitecture());
411412
var currentProcessPath = this.processHelper.GetCurrentProcessFileName();
412413
bool isRunningWithDotnetMuxer = IsRunningWithDotnetMuxer(currentProcessPath);
413414
if (useCustomDotnetHostpath)
@@ -424,10 +425,10 @@ public virtual TestProcessStartInfo GetTestHostProcessStartInfo(
424425
else
425426
{
426427
PlatformArchitecture targetArchitecture = TranslateToPlatformArchitecture(this.architecture);
427-
EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: Searching muxer for the architecture '{targetArchitecture}', OS '{this.platformEnvironment.OperatingSystem}' framework '{this.targetFramework}' SDK platform architecture '{this.platformEnvironment.Architecture}'");
428+
EqtTrace.Verbose($"DotnetTestHostmanager: Searching muxer for the architecture '{targetArchitecture}', OS '{this.platformEnvironment.OperatingSystem}' framework '{this.targetFramework}' SDK platform architecture '{this.platformEnvironment.Architecture}'");
428429
if (forceToX64)
429430
{
430-
EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: Forcing the search to x64 architecure, IsDefaultTargetArchitecture '{this.runsettingHelper.IsDefaultTargetArchitecture}' OS '{this.platformEnvironment.OperatingSystem}' framework '{this.targetFramework}'");
431+
EqtTrace.Verbose($"DotnetTestHostmanager: Forcing the search to x64 architecure, IsDefaultTargetArchitecture '{this.runsettingHelper.IsDefaultTargetArchitecture}' OS '{this.platformEnvironment.OperatingSystem}' framework '{this.targetFramework}'");
431432
}
432433

433434
PlatformArchitecture finalTargetArchitecture = forceToX64 ? PlatformArchitecture.X64 : targetArchitecture;
@@ -441,7 +442,7 @@ public virtual TestProcessStartInfo GetTestHostProcessStartInfo(
441442
startInfo.FileName = muxerPath;
442443
}
443444

444-
EqtTrace.Verbose("DotnetTestHostmanager.LaunchTestHostAsync: Full path of testhost.dll is {0}", testHostPath);
445+
EqtTrace.Verbose("DotnetTestHostmanager: Full path of testhost.dll is {0}", testHostPath);
445446
args = "exec" + args;
446447
args += " " + testHostPath.AddDoubleQuote();
447448
}

src/vstest.console/TestPlatformHelpers/TestRequestManager.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers
3636
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;
3737
using Microsoft.VisualStudio.TestPlatform.Utilities;
3838
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Payloads;
39+
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;
3940

4041
/// <summary>
4142
/// Defines the test request manger which can fire off discovery and test run requests.
@@ -618,8 +619,9 @@ private bool UpdateRunSettingsIfRequired(
618619
// it can be specified by user on the command line with --arch or through runsettings.
619620
// If it's not specified by user will be filled by current processor architecture;
620621
// should be the same as SDK.
621-
defaultArchitecture = runConfiguration.TargetPlatform;
622-
EqtTrace.Verbose($"Default architecture: {defaultArchitecture}");
622+
defaultArchitecture = RunSettingsHelper.Instance.IsDefaultTargetArchitecture ?
623+
TranslateToArchitecture(processHelper.GetCurrentProcessArchitecture()) :
624+
runConfiguration.TargetPlatform;
623625
#else
624626
// We are running in vstest.console.exe that was built against .NET
625627
// Framework. This console prefers 32-bit because it needs to run as 32-bit
@@ -630,6 +632,7 @@ private bool UpdateRunSettingsIfRequired(
630632
// We want to find 64-bit SDK because it is more likely to be installed.
631633
defaultArchitecture = Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86;
632634
#endif
635+
EqtTrace.Verbose($"Default architecture: {defaultArchitecture} IsDefaultTargetArchitecture: {RunSettingsHelper.Instance.IsDefaultTargetArchitecture}");
633636
}
634637

635638
settingsUpdated |= this.UpdatePlatform(
@@ -656,6 +659,27 @@ private bool UpdateRunSettingsIfRequired(
656659
}
657660

658661
return settingsUpdated;
662+
663+
#if NETCOREAPP
664+
Architecture TranslateToArchitecture(PlatformArchitecture targetArchitecture)
665+
{
666+
switch (targetArchitecture)
667+
{
668+
case PlatformArchitecture.X86:
669+
return Architecture.X86;
670+
case PlatformArchitecture.X64:
671+
return Architecture.X64;
672+
case PlatformArchitecture.ARM:
673+
return Architecture.ARM;
674+
case PlatformArchitecture.ARM64:
675+
return Architecture.ARM64;
676+
default:
677+
break;
678+
}
679+
680+
throw new TestPlatformException($"Invalid target architecture '{targetArchitecture}'");
681+
}
682+
#endif
659683
}
660684

661685
private bool AddOrUpdateConsoleLogger(

test/Microsoft.TestPlatform.CoreUtilities.UnitTests/Helpers/DotnetHostHelperTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void GetDotnetPathByArchitecture_SameArchitecture()
3434
environmentHelper.SetupGet(x => x.OperatingSystem).Returns(PlatformOperatingSystem.Windows);
3535
environmentHelper.SetupGet(x => x.Architecture).Returns(PlatformArchitecture.X64);
3636
processHelper.Setup(x => x.GetCurrentProcessFileName()).Returns(finalMuxerPath);
37+
processHelper.Setup(x => x.GetCurrentProcessArchitecture()).Returns(PlatformArchitecture.X64);
3738

3839
// Act & Assert
3940
Assert.IsTrue(dotnetHostHelper.TryGetDotnetPathByArchitecture(PlatformArchitecture.X64, out string muxerPath));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public DotnetTestHostManagerTests()
9797
// Setup a dummy current process for tests
9898
this.mockProcessHelper.Setup(ph => ph.GetCurrentProcessFileName()).Returns(DefaultDotnetPath);
9999
this.mockProcessHelper.Setup(ph => ph.GetTestEngineDirectory()).Returns(DefaultDotnetPath);
100+
this.mockProcessHelper.Setup(ph => ph.GetCurrentProcessArchitecture()).Returns(PlatformArchitecture.X64);
100101
this.mockEnvironmentVariable.Setup(ev => ev.GetEnvironmentVariable(It.IsAny<string>())).Returns(Path.GetDirectoryName(DefaultDotnetPath));
101102
this.mockFileHelper.Setup(ph => ph.Exists(this.defaultTestHostPath)).Returns(true);
102103
this.mockFileHelper.Setup(ph => ph.Exists(DefaultDotnetPath)).Returns(true);

0 commit comments

Comments
 (0)