Skip to content

Commit 449f7a0

Browse files
authored
Merge pull request #2094 from SydneyhSmith/master
Set Default PowerShell Version on Windows to Latest Available
2 parents 2335806 + 1c620bb commit 449f7a0

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

src/platform.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,55 @@ 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)) {
88116
powerShellExePath = macOSPreviewExePath;
89-
}
117+
}
90118
} else if (platformDetails.operatingSystem === OperatingSystem.Linux) {
91119
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
92120
// as well as the Snaps case - https://snapcraft.io/
@@ -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
@@ -113,34 +113,6 @@ export class SessionManager implements Middleware {
113113

114114
this.powerShellExePath = this.getPowerShellExePath();
115115

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

146118
if (this.powerShellExePath) {

test/platform.test.ts

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

7777
checkDefaultPowerShellPath(
7878
platformDetails,
79-
platform.SysnativePowerShellPath);
79+
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");
8080

8181
checkAvailableWindowsPowerShellPaths(
8282
platformDetails,
@@ -106,7 +106,7 @@ suite("Platform module", () => {
106106

107107
checkDefaultPowerShellPath(
108108
platformDetails,
109-
platform.System32PowerShellPath);
109+
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");
110110

111111
checkAvailableWindowsPowerShellPaths(
112112
platformDetails,

0 commit comments

Comments
 (0)