Skip to content

Backporting #2094 to set default PowerShell to latest available #2149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,50 @@ export function getDefaultPowerShellPath(
use32Bit: boolean = false): string | null {

let powerShellExePath;
let psCoreInstallPath;

// Find the path to powershell.exe based on the current platform
// Find the path to the powershell executable based on the current platform
// and the user's desire to run the x86 version of PowerShell
if (platformDetails.operatingSystem === OperatingSystem.Windows) {
if (use32Bit) {
powerShellExePath =
platformDetails.isOS64Bit && platformDetails.isProcess64Bit
? SysWow64PowerShellPath
: System32PowerShellPath;
psCoreInstallPath =
(platformDetails.isProcess64Bit ? process.env["ProgramFiles(x86)"] : process.env.ProgramFiles)
+ "\\PowerShell";
} else {
powerShellExePath =
!platformDetails.isOS64Bit || platformDetails.isProcess64Bit
? System32PowerShellPath
: SysnativePowerShellPath;
psCoreInstallPath =
(platformDetails.isProcess64Bit ? process.env.ProgramFiles : process.env.ProgramW6432) + "\\PowerShell";
}
} else if (platformDetails.operatingSystem === OperatingSystem.MacOS) {

if (fs.existsSync(psCoreInstallPath)) {
const arch = platformDetails.isProcess64Bit ? "(x64)" : "(x86)";
const psCorePaths =
fs.readdirSync(psCoreInstallPath)
.map((item) => path.join(psCoreInstallPath, item))
.filter((item) => {
const exePath = path.join(item, "pwsh.exe");
return fs.lstatSync(item).isDirectory() && fs.existsSync(exePath);
})
.map((item) => ({
versionName: `PowerShell ${path.parse(item).base} ${arch}`,
exePath: path.join(item, "pwsh.exe"),
}));

if (psCorePaths) {
return powerShellExePath = psCorePaths[0].exePath;
}
}

// No PowerShell 6+ detected so use Windows PowerShell.
if (use32Bit) {
return platformDetails.isOS64Bit && platformDetails.isProcess64Bit
? SysWow64PowerShellPath
: System32PowerShellPath;
}
return !platformDetails.isOS64Bit || platformDetails.isProcess64Bit
? System32PowerShellPath
: SysnativePowerShellPath;
}
if (platformDetails.operatingSystem === OperatingSystem.MacOS) {
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
powerShellExePath = macOSExePath;
if (!fs.existsSync(macOSExePath) && fs.existsSync(macOSPreviewExePath)) {
Expand Down Expand Up @@ -179,7 +207,7 @@ export function getAvailablePowerShellExes(
return fs.lstatSync(item).isDirectory() && fs.existsSync(exePath);
})
.map((item) => ({
versionName: `PowerShell Core ${path.parse(item).base} ${arch}`,
versionName: `PowerShell ${path.parse(item).base} ${arch}`,
exePath: path.join(item, "pwsh.exe"),
}));

Expand All @@ -200,7 +228,7 @@ export function getAvailablePowerShellExes(
exePaths.forEach((exePath) => {
if (fs.existsSync(exePath)) {
paths.push({
versionName: "PowerShell Core" + (/-preview/.test(exePath) ? " Preview" : ""),
versionName: "PowerShell" + (/-preview/.test(exePath) ? " Preview" : ""),
exePath,
});
}
Expand Down
28 changes: 0 additions & 28 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,6 @@ export class SessionManager implements Middleware {

this.powerShellExePath = this.getPowerShellExePath();

// Check for OpenSSL dependency on macOS when running PowerShell Core alpha. Look for the default
// Homebrew installation path and if that fails check the system-wide library path.
if (os.platform() === "darwin" && this.getPowerShellVersionLabel() === "alpha") {
if (!(utils.checkIfFileExists("/usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib") &&
utils.checkIfFileExists("/usr/local/opt/openssl/lib/libssl.1.0.0.dylib")) &&
!(utils.checkIfFileExists("/usr/local/lib/libcrypto.1.0.0.dylib") &&
utils.checkIfFileExists("/usr/local/lib/libssl.1.0.0.dylib"))) {
const thenable =
vscode.window.showWarningMessage(
"The PowerShell extension will not work without OpenSSL on macOS and OS X when using " +
"PowerShell Core alpha",
"Show Documentation");

thenable.then(
(s) => {
if (s === "Show Documentation") {
cp.exec("open https://github.com/PowerShell/vscode-powershell/blob/master/docs/" +
"troubleshooting.md#1-powershell-intellisense-does-not-work-cant-debug-scripts");
}
});

// Don't continue initializing since Editor Services will not load successfully
this.setSessionFailure(
"Cannot start PowerShell Editor Services due to missing OpenSSL dependency.");
return;
}
}

this.suppressRestartPrompt = false;

if (this.powerShellExePath) {
Expand Down
86 changes: 84 additions & 2 deletions test/platform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ suite("Platform module", () => {

checkDefaultPowerShellPath(
platformDetails,
platform.SysnativePowerShellPath);
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");

checkAvailableWindowsPowerShellPaths(
platformDetails,
Expand Down Expand Up @@ -107,7 +107,7 @@ suite("Platform module", () => {

checkDefaultPowerShellPath(
platformDetails,
platform.System32PowerShellPath);
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");

checkAvailableWindowsPowerShellPaths(
platformDetails,
Expand All @@ -118,4 +118,86 @@ suite("Platform module", () => {
},
]);
});
if (process.platform === "win32") {
suite("64-bit Windows, 64-bit VS Code", () => {
const platformDetails: platform.IPlatformDetails = {
operatingSystem: platform.OperatingSystem.Windows,
isOS64Bit: true,
isProcess64Bit: true,
};

checkDefaultPowerShellPath(
platformDetails,
platform.System32PowerShellPath);

checkAvailableWindowsPowerShellPaths(
platformDetails,
[
{
versionName: platform.WindowsPowerShell64BitLabel,
exePath: platform.System32PowerShellPath,
},
{
versionName: platform.WindowsPowerShell32BitLabel,
exePath: platform.SysWow64PowerShellPath,
},
]);

checkFixedWindowsPowerShellpath(
platformDetails,
platform.SysnativePowerShellPath,
platform.System32PowerShellPath);
});

suite("64-bit Windows, 32-bit VS Code", () => {
const platformDetails: platform.IPlatformDetails = {
operatingSystem: platform.OperatingSystem.Windows,
isOS64Bit: true,
isProcess64Bit: false,
};

checkDefaultPowerShellPath(
platformDetails,
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");

checkAvailableWindowsPowerShellPaths(
platformDetails,
[
{
versionName: platform.WindowsPowerShell64BitLabel,
exePath: platform.SysnativePowerShellPath,
},
{
versionName: platform.WindowsPowerShell32BitLabel,
exePath: platform.System32PowerShellPath,
},
]);

checkFixedWindowsPowerShellpath(
platformDetails,
platform.SysWow64PowerShellPath,
platform.System32PowerShellPath);
});

suite("32-bit Windows, 32-bit VS Code", () => {
const platformDetails: platform.IPlatformDetails = {
operatingSystem: platform.OperatingSystem.Windows,
isOS64Bit: false,
isProcess64Bit: false,
};

checkDefaultPowerShellPath(
platformDetails,
"C:\\Program Files\\PowerShell\\6\\pwsh.exe");

checkAvailableWindowsPowerShellPaths(
platformDetails,
[
{
versionName: platform.WindowsPowerShell32BitLabel,
exePath: platform.System32PowerShellPath,
},
]);
});
}
});