Skip to content

Commit 5d509e4

Browse files
Backporting #2094 to set default PowerShell to latest available (#2149)
* Merge pull request #2094 from SydneyhSmith/master Set Default PowerShell Version on Windows to Latest Available * Update platform.test.ts * Update src/platform.ts Co-Authored-By: Tyler James Leonhardt <tylerl0706@gmail.com>
1 parent 323a8cd commit 5d509e4

File tree

3 files changed

+124
-42
lines changed

3 files changed

+124
-42
lines changed

src/platform.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,50 @@ export function getDefaultPowerShellPath(
6666
use32Bit: boolean = false): string | null {
6767

6868
let powerShellExePath;
69+
let psCoreInstallPath;
6970

70-
// Find the path to powershell.exe based on the current platform
71+
// Find the path to the powershell executable based on the current platform
7172
// and the user's desire to run the x86 version of PowerShell
7273
if (platformDetails.operatingSystem === OperatingSystem.Windows) {
7374
if (use32Bit) {
74-
powerShellExePath =
75-
platformDetails.isOS64Bit && platformDetails.isProcess64Bit
76-
? SysWow64PowerShellPath
77-
: System32PowerShellPath;
75+
psCoreInstallPath =
76+
(platformDetails.isProcess64Bit ? process.env["ProgramFiles(x86)"] : process.env.ProgramFiles)
77+
+ "\\PowerShell";
7878
} else {
79-
powerShellExePath =
80-
!platformDetails.isOS64Bit || platformDetails.isProcess64Bit
81-
? System32PowerShellPath
82-
: SysnativePowerShellPath;
79+
psCoreInstallPath =
80+
(platformDetails.isProcess64Bit ? process.env.ProgramFiles : process.env.ProgramW6432) + "\\PowerShell";
8381
}
84-
} else if (platformDetails.operatingSystem === OperatingSystem.MacOS) {
82+
83+
if (fs.existsSync(psCoreInstallPath)) {
84+
const arch = platformDetails.isProcess64Bit ? "(x64)" : "(x86)";
85+
const psCorePaths =
86+
fs.readdirSync(psCoreInstallPath)
87+
.map((item) => path.join(psCoreInstallPath, item))
88+
.filter((item) => {
89+
const exePath = path.join(item, "pwsh.exe");
90+
return fs.lstatSync(item).isDirectory() && fs.existsSync(exePath);
91+
})
92+
.map((item) => ({
93+
versionName: `PowerShell ${path.parse(item).base} ${arch}`,
94+
exePath: path.join(item, "pwsh.exe"),
95+
}));
96+
97+
if (psCorePaths) {
98+
return powerShellExePath = psCorePaths[0].exePath;
99+
}
100+
}
101+
102+
// No PowerShell 6+ detected so use Windows PowerShell.
103+
if (use32Bit) {
104+
return platformDetails.isOS64Bit && platformDetails.isProcess64Bit
105+
? SysWow64PowerShellPath
106+
: System32PowerShellPath;
107+
}
108+
return !platformDetails.isOS64Bit || platformDetails.isProcess64Bit
109+
? System32PowerShellPath
110+
: SysnativePowerShellPath;
111+
}
112+
if (platformDetails.operatingSystem === OperatingSystem.MacOS) {
85113
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
86114
powerShellExePath = macOSExePath;
87115
if (!fs.existsSync(macOSExePath) && fs.existsSync(macOSPreviewExePath)) {
@@ -179,7 +207,7 @@ export function getAvailablePowerShellExes(
179207
return fs.lstatSync(item).isDirectory() && fs.existsSync(exePath);
180208
})
181209
.map((item) => ({
182-
versionName: `PowerShell Core ${path.parse(item).base} ${arch}`,
210+
versionName: `PowerShell ${path.parse(item).base} ${arch}`,
183211
exePath: path.join(item, "pwsh.exe"),
184212
}));
185213

@@ -200,7 +228,7 @@ export function getAvailablePowerShellExes(
200228
exePaths.forEach((exePath) => {
201229
if (fs.existsSync(exePath)) {
202230
paths.push({
203-
versionName: "PowerShell Core" + (/-preview/.test(exePath) ? " Preview" : ""),
231+
versionName: "PowerShell" + (/-preview/.test(exePath) ? " Preview" : ""),
204232
exePath,
205233
});
206234
}

src/session.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -119,34 +119,6 @@ export class SessionManager implements Middleware {
119119

120120
this.powerShellExePath = this.getPowerShellExePath();
121121

122-
// Check for OpenSSL dependency on macOS when running PowerShell Core alpha. Look for the default
123-
// Homebrew installation path and if that fails check the system-wide library path.
124-
if (os.platform() === "darwin" && this.getPowerShellVersionLabel() === "alpha") {
125-
if (!(utils.checkIfFileExists("/usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib") &&
126-
utils.checkIfFileExists("/usr/local/opt/openssl/lib/libssl.1.0.0.dylib")) &&
127-
!(utils.checkIfFileExists("/usr/local/lib/libcrypto.1.0.0.dylib") &&
128-
utils.checkIfFileExists("/usr/local/lib/libssl.1.0.0.dylib"))) {
129-
const thenable =
130-
vscode.window.showWarningMessage(
131-
"The PowerShell extension will not work without OpenSSL on macOS and OS X when using " +
132-
"PowerShell Core alpha",
133-
"Show Documentation");
134-
135-
thenable.then(
136-
(s) => {
137-
if (s === "Show Documentation") {
138-
cp.exec("open https://github.com/PowerShell/vscode-powershell/blob/master/docs/" +
139-
"troubleshooting.md#1-powershell-intellisense-does-not-work-cant-debug-scripts");
140-
}
141-
});
142-
143-
// Don't continue initializing since Editor Services will not load successfully
144-
this.setSessionFailure(
145-
"Cannot start PowerShell Editor Services due to missing OpenSSL dependency.");
146-
return;
147-
}
148-
}
149-
150122
this.suppressRestartPrompt = false;
151123

152124
if (this.powerShellExePath) {

test/platform.test.ts

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ suite("Platform module", () => {
7777

7878
checkDefaultPowerShellPath(
7979
platformDetails,
80-
platform.SysnativePowerShellPath);
80+
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");
8181

8282
checkAvailableWindowsPowerShellPaths(
8383
platformDetails,
@@ -107,7 +107,7 @@ suite("Platform module", () => {
107107

108108
checkDefaultPowerShellPath(
109109
platformDetails,
110-
platform.System32PowerShellPath);
110+
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");
111111

112112
checkAvailableWindowsPowerShellPaths(
113113
platformDetails,
@@ -118,4 +118,86 @@ suite("Platform module", () => {
118118
},
119119
]);
120120
});
121+
if (process.platform === "win32") {
122+
suite("64-bit Windows, 64-bit VS Code", () => {
123+
const platformDetails: platform.IPlatformDetails = {
124+
operatingSystem: platform.OperatingSystem.Windows,
125+
isOS64Bit: true,
126+
isProcess64Bit: true,
127+
};
128+
129+
checkDefaultPowerShellPath(
130+
platformDetails,
131+
platform.System32PowerShellPath);
132+
133+
checkAvailableWindowsPowerShellPaths(
134+
platformDetails,
135+
[
136+
{
137+
versionName: platform.WindowsPowerShell64BitLabel,
138+
exePath: platform.System32PowerShellPath,
139+
},
140+
{
141+
versionName: platform.WindowsPowerShell32BitLabel,
142+
exePath: platform.SysWow64PowerShellPath,
143+
},
144+
]);
145+
146+
checkFixedWindowsPowerShellpath(
147+
platformDetails,
148+
platform.SysnativePowerShellPath,
149+
platform.System32PowerShellPath);
150+
});
151+
152+
suite("64-bit Windows, 32-bit VS Code", () => {
153+
const platformDetails: platform.IPlatformDetails = {
154+
operatingSystem: platform.OperatingSystem.Windows,
155+
isOS64Bit: true,
156+
isProcess64Bit: false,
157+
};
158+
159+
checkDefaultPowerShellPath(
160+
platformDetails,
161+
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");
162+
163+
checkAvailableWindowsPowerShellPaths(
164+
platformDetails,
165+
[
166+
{
167+
versionName: platform.WindowsPowerShell64BitLabel,
168+
exePath: platform.SysnativePowerShellPath,
169+
},
170+
{
171+
versionName: platform.WindowsPowerShell32BitLabel,
172+
exePath: platform.System32PowerShellPath,
173+
},
174+
]);
175+
176+
checkFixedWindowsPowerShellpath(
177+
platformDetails,
178+
platform.SysWow64PowerShellPath,
179+
platform.System32PowerShellPath);
180+
});
181+
182+
suite("32-bit Windows, 32-bit VS Code", () => {
183+
const platformDetails: platform.IPlatformDetails = {
184+
operatingSystem: platform.OperatingSystem.Windows,
185+
isOS64Bit: false,
186+
isProcess64Bit: false,
187+
};
188+
189+
checkDefaultPowerShellPath(
190+
platformDetails,
191+
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");
192+
193+
checkAvailableWindowsPowerShellPaths(
194+
platformDetails,
195+
[
196+
{
197+
versionName: platform.WindowsPowerShell32BitLabel,
198+
exePath: platform.System32PowerShellPath,
199+
},
200+
]);
201+
});
202+
}
121203
});

0 commit comments

Comments
 (0)