Skip to content

Commit ee53878

Browse files
Remove unnecessary PowerShellProcessArchitecture (#1964)
This simply wasn't being used and was overly complicated. The only time we want the architecture is when queried via LSP so that the VS Code client can determine which installer to download for its auto-update feature. If we can, we should deduplicate version logic in the loader and the `VersionUtils` class.
1 parent 533dc64 commit ee53878

File tree

5 files changed

+36
-116
lines changed

5 files changed

+36
-116
lines changed

src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ private void LogOperatingSystemDetails()
354354
");
355355
}
356356

357+
// TODO: Deduplicate this with VersionUtils.
357358
private static string GetOSArchitecture()
358359
{
359360
#if CoreCLR

src/PowerShellEditorServices/Services/PowerShell/Context/PowerShellVersionDetails.cs

+5-68
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,11 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Context
1111
{
1212
using System.Management.Automation;
1313

14-
/// <summary>
15-
/// Defines the possible enumeration values for the PowerShell process architecture.
16-
/// </summary>
17-
internal enum PowerShellProcessArchitecture
18-
{
19-
/// <summary>
20-
/// The processor architecture is unknown or wasn't accessible.
21-
/// </summary>
22-
Unknown,
23-
24-
/// <summary>
25-
/// The processor architecture is 32-bit.
26-
/// </summary>
27-
X86,
28-
29-
/// <summary>
30-
/// The processor architecture is 64-bit.
31-
/// </summary>
32-
X64
33-
}
34-
3514
/// <summary>
3615
/// Provides details about the version of the PowerShell runtime.
3716
/// </summary>
3817
internal class PowerShellVersionDetails
3918
{
40-
#region Properties
41-
4219
/// <summary>
4320
/// Gets the version of the PowerShell runtime.
4421
/// </summary>
@@ -55,40 +32,26 @@ internal class PowerShellVersionDetails
5532
/// </summary>
5633
public string Edition { get; }
5734

58-
/// <summary>
59-
/// Gets the architecture of the PowerShell process.
60-
/// </summary>
61-
public PowerShellProcessArchitecture Architecture { get; }
62-
63-
#endregion
64-
65-
#region Constructors
66-
6735
/// <summary>
6836
/// Creates an instance of the PowerShellVersionDetails class.
6937
/// </summary>
7038
/// <param name="version">The version of the PowerShell runtime.</param>
7139
/// <param name="versionString">A string representation of the PowerShell version.</param>
7240
/// <param name="editionString">The string representation of the PowerShell edition.</param>
73-
/// <param name="architecture">The processor architecture.</param>
7441
public PowerShellVersionDetails(
7542
Version version,
7643
string versionString,
77-
string editionString,
78-
PowerShellProcessArchitecture architecture)
44+
string editionString)
7945
{
8046
Version = version;
8147
VersionString = versionString;
8248
Edition = editionString;
83-
Architecture = architecture;
8449
}
8550

86-
#endregion
87-
88-
#region Public Methods
89-
9051
/// <summary>
91-
/// Gets the PowerShell version details for the given runspace.
52+
/// Gets the PowerShell version details for the given runspace. This doesn't use
53+
/// VersionUtils because we may be remoting, and therefore want the remote runspace's
54+
/// version, not the local process.
9255
/// </summary>
9356
/// <param name="logger">An ILogger implementation used for writing log messages.</param>
9457
/// <param name="pwsh">The PowerShell instance for which to get the version.</param>
@@ -98,7 +61,6 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh
9861
Version powerShellVersion = new(5, 0);
9962
string versionString = null;
10063
string powerShellEdition = "Desktop";
101-
PowerShellProcessArchitecture architecture = PowerShellProcessArchitecture.Unknown;
10264

10365
try
10466
{
@@ -129,25 +91,6 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh
12991
}
13092

13193
versionString = psVersionTable["GitCommitId"] is string gitCommitId ? gitCommitId : powerShellVersion.ToString();
132-
133-
PSCommand procArchCommand = new PSCommand().AddScript("$env:PROCESSOR_ARCHITECTURE", useLocalScope: true);
134-
135-
string arch = pwsh
136-
.AddScript("$env:PROCESSOR_ARCHITECTURE", useLocalScope: true)
137-
.InvokeAndClear<string>()
138-
.FirstOrDefault();
139-
140-
if (arch != null)
141-
{
142-
if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase))
143-
{
144-
architecture = PowerShellProcessArchitecture.X64;
145-
}
146-
else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase))
147-
{
148-
architecture = PowerShellProcessArchitecture.X86;
149-
}
150-
}
15194
}
15295
}
15396
catch (Exception ex)
@@ -156,13 +99,7 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh
15699
"Failed to look up PowerShell version, defaulting to version 5.\r\n\r\n" + ex.ToString());
157100
}
158101

159-
return new PowerShellVersionDetails(
160-
powerShellVersion,
161-
versionString,
162-
powerShellEdition,
163-
architecture);
102+
return new PowerShellVersionDetails(powerShellVersion, versionString, powerShellEdition);
164103
}
165-
166-
#endregion
167104
}
168105
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
@@ -13,35 +12,13 @@ internal class GetVersionHandler : IGetVersionHandler
1312
{
1413
public async Task<PowerShellVersion> Handle(GetVersionParams request, CancellationToken cancellationToken)
1514
{
16-
PowerShellProcessArchitecture architecture = PowerShellProcessArchitecture.Unknown;
17-
// This should be changed to using a .NET call sometime in the future... but it's just for logging purposes.
18-
string arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
19-
if (arch != null)
20-
{
21-
if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase))
22-
{
23-
architecture = PowerShellProcessArchitecture.X64;
24-
}
25-
else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase))
26-
{
27-
architecture = PowerShellProcessArchitecture.X86;
28-
}
29-
}
30-
3115
return new PowerShellVersion
3216
{
3317
Version = VersionUtils.PSVersionString,
3418
Edition = VersionUtils.PSEdition,
3519
DisplayVersion = VersionUtils.PSVersion.ToString(2),
36-
Architecture = architecture.ToString()
20+
Architecture = VersionUtils.Architecture
3721
};
3822
}
39-
40-
private enum PowerShellProcessArchitecture
41-
{
42-
Unknown,
43-
X86,
44-
X64
45-
}
4623
}
4724
}

src/PowerShellEditorServices/Services/PowerShell/Handlers/IGetVersionHandler.cs

+5-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using MediatR;
5-
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Context;
65
using OmniSharp.Extensions.JsonRpc;
76

87
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell
@@ -12,29 +11,11 @@ internal interface IGetVersionHandler : IJsonRpcRequestHandler<GetVersionParams,
1211

1312
internal class GetVersionParams : IRequest<PowerShellVersion> { }
1413

15-
internal class PowerShellVersion
14+
internal record PowerShellVersion
1615
{
17-
public string Version { get; set; }
18-
public string DisplayVersion { get; set; }
19-
public string Edition { get; set; }
20-
public string Architecture { get; set; }
21-
22-
public PowerShellVersion()
23-
{
24-
}
25-
26-
public PowerShellVersion(PowerShellVersionDetails versionDetails)
27-
{
28-
Version = versionDetails.VersionString;
29-
DisplayVersion = $"{versionDetails.Version.Major}.{versionDetails.Version.Minor}";
30-
Edition = versionDetails.Edition;
31-
32-
Architecture = versionDetails.Architecture switch
33-
{
34-
PowerShellProcessArchitecture.X64 => "x64",
35-
PowerShellProcessArchitecture.X86 => "x86",
36-
_ => "Architecture Unknown",
37-
};
38-
}
16+
public string Version { get; init; }
17+
public string DisplayVersion { get; init; }
18+
public string Edition { get; init; }
19+
public string Architecture { get; init; }
3920
}
4021
}

src/PowerShellEditorServices/Utility/VersionUtils.cs

+24
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ internal static class VersionUtils
6161
/// True if we are running on Linux, false otherwise.
6262
/// </summary>
6363
public static bool IsLinux { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
64+
65+
/// <summary>
66+
/// The .NET Architecture as a string.
67+
/// </summary>
68+
public static string Architecture { get; } = PowerShellReflectionUtils.GetOSArchitecture();
6469
}
6570

6671
internal static class PowerShellReflectionUtils
@@ -96,5 +101,24 @@ internal static class PowerShellReflectionUtils
96101
public static string PSVersionString { get; } = s_psCurrentVersionProperty != null
97102
? s_psCurrentVersionProperty.GetValue(null).ToString()
98103
: s_psVersionProperty.GetValue(null).ToString();
104+
105+
public static string GetOSArchitecture()
106+
{
107+
#if CoreCLR
108+
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
109+
{
110+
return RuntimeInformation.OSArchitecture.ToString();
111+
}
112+
#endif
113+
// If on win7 (version 6.1.x), avoid System.Runtime.InteropServices.RuntimeInformation
114+
if (Environment.OSVersion.Version < new Version(6, 2))
115+
{
116+
return Environment.Is64BitProcess
117+
? "X64"
118+
: "X86";
119+
}
120+
121+
return RuntimeInformation.OSArchitecture.ToString();
122+
}
99123
}
100124
}

0 commit comments

Comments
 (0)