Skip to content

Commit c7a2adb

Browse files
authored
Fix crash when PowerShell selection is changed (#2261)
* Fix crash when PowerShell selection is changed * Change commented out dotnet global tool code * Add tests
1 parent d76f2e2 commit c7a2adb

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/platform.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,17 @@ export class PowerShellExeFinder {
116116
* @param configuredPowerShellPath the PowerShell path configured by the user.
117117
*/
118118
public fixWindowsPowerShellPath(configuredPowerShellPath: string): string {
119+
const altWinPS = this.findWinPS({ useAlternateBitness: true });
120+
121+
if (!altWinPS) {
122+
return configuredPowerShellPath;
123+
}
124+
125+
const lowerAltWinPSPath = altWinPS.exePath.toLocaleLowerCase();
119126
const lowerConfiguredPath = configuredPowerShellPath.toLocaleLowerCase();
120-
const lowerAltWinPSPath = this.alternateBitnessWinPS.exePath.toLocaleLowerCase();
121127

122128
if (lowerConfiguredPath === lowerAltWinPSPath) {
123-
return this.winPS.exePath;
129+
return this.findWinPS().exePath;
124130
}
125131

126132
return configuredPowerShellPath;
@@ -180,7 +186,7 @@ export class PowerShellExeFinder {
180186
// Currently it cannot take startup arguments to start PSES with.
181187
//
182188
// Look for the .NET global tool
183-
// yield this.pwshDotnetGlobalTool;
189+
// yield this.findPSCoreDotnetGlobalTool();
184190

185191
// Look for PSCore preview
186192
yield this.findPSCorePreview();

test/platform.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,47 @@ suite("Platform module", () => {
594594
});
595595
}
596596
});
597+
598+
suite("Windows PowerShell path fix", () => {
599+
teardown(() => {
600+
sinon.restore();
601+
mockFS.restore();
602+
});
603+
604+
for (const testPlatform of successTestCases
605+
.filter((tp) => tp.platformDetails.operatingSystem === platform.OperatingSystem.Windows)) {
606+
607+
test(`Corrects the Windows PowerShell path on ${testPlatform.name}`, () => {
608+
setupTestEnvironment(testPlatform);
609+
610+
function getWinPSPath(systemDir: string) {
611+
return path.join(
612+
testPlatform.environmentVars.windir,
613+
systemDir,
614+
"WindowsPowerShell",
615+
"v1.0",
616+
"powershell.exe");
617+
}
618+
619+
const winPSPath = getWinPSPath("System32");
620+
621+
let altWinPSPath;
622+
if (testPlatform.platformDetails.isProcess64Bit) {
623+
altWinPSPath = getWinPSPath("SysWOW64");
624+
} else if (testPlatform.platformDetails.isOS64Bit) {
625+
altWinPSPath = getWinPSPath("Sysnative");
626+
} else {
627+
altWinPSPath = null;
628+
}
629+
630+
const powerShellExeFinder = new platform.PowerShellExeFinder(testPlatform.platformDetails);
631+
632+
assert.strictEqual(powerShellExeFinder.fixWindowsPowerShellPath(winPSPath), winPSPath);
633+
634+
if (altWinPSPath) {
635+
assert.strictEqual(powerShellExeFinder.fixWindowsPowerShellPath(altWinPSPath), winPSPath);
636+
}
637+
});
638+
}
639+
});
597640
});

0 commit comments

Comments
 (0)