@@ -16,12 +16,18 @@ import { IFeature } from './feature';
1616import { Message } from 'vscode-jsonrpc' ;
1717import { PowerShellProcess } from './process' ;
1818import { StringDecoder } from 'string_decoder' ;
19+
1920import {
2021 LanguageClient , LanguageClientOptions , Executable ,
2122 RequestType , RequestType0 , NotificationType ,
2223 StreamInfo , ErrorAction , CloseAction , RevealOutputChannelOn ,
2324 Middleware , ResolveCodeLensSignature } from 'vscode-languageclient' ;
2425
26+ import {
27+ OperatingSystem , PlatformDetails , getDefaultPowerShellPath ,
28+ getPlatformDetails , fixWindowsPowerShellPath ,
29+ getPowerShellExeItems } from './platform' ;
30+
2531export enum SessionStatus {
2632 NotStarted ,
2733 Initializing ,
@@ -35,12 +41,12 @@ export class SessionManager implements Middleware {
3541 private ShowSessionMenuCommandName = "PowerShell.ShowSessionMenu" ;
3642
3743 private hostVersion : string ;
38- private isWindowsOS : boolean ;
3944 private editorServicesArgs : string ;
4045 private powerShellExePath : string = "" ;
4146 private sessionStatus : SessionStatus ;
4247 private suppressRestartPrompt : boolean ;
4348 private focusConsoleOnExecute : boolean ;
49+ private platformDetails : PlatformDetails ;
4450 private extensionFeatures : IFeature [ ] = [ ] ;
4551 private statusBarItem : vscode . StatusBarItem ;
4652 private languageServerProcess : PowerShellProcess ;
@@ -61,7 +67,7 @@ export class SessionManager implements Middleware {
6167 private requiredEditorServicesVersion : string ,
6268 private log : Logger ) {
6369
64- this . isWindowsOS = os . platform ( ) == "win32" ;
70+ this . platformDetails = getPlatformDetails ( ) ;
6571
6672 // Get the current version of this extension
6773 this . hostVersion =
@@ -542,9 +548,53 @@ export class SessionManager implements Middleware {
542548 this . sessionSettings . developer . powerShellExePath ||
543549 "" ) . trim ( ) ;
544550
551+ if ( this . platformDetails . operatingSystem === OperatingSystem . Windows &&
552+ powerShellExePath . length > 0 ) {
553+
554+ // Check the path bitness
555+ let fixedPath =
556+ fixWindowsPowerShellPath (
557+ powerShellExePath ,
558+ this . platformDetails ) ;
559+
560+ if ( fixedPath !== powerShellExePath ) {
561+ let bitness = this . platformDetails . isOS64Bit ? 64 : 32 ;
562+ // Show deprecation message with fix action.
563+ // We don't need to wait on this to complete
564+ // because we can finish gathering the configured
565+ // PowerShell path without the fix
566+ vscode
567+ . window
568+ . showWarningMessage (
569+ `The specified PowerShell path is incorrect for ${ bitness } -bit VS Code, using '${ fixedPath } ' instead.` ,
570+ "Fix Setting Automatically" )
571+ . then ( choice => {
572+ if ( choice ) {
573+ this . suppressRestartPrompt = true ;
574+ Settings
575+ . change (
576+ "powerShellExePath" ,
577+ this . sessionSettings . developer . powerShellExePath ,
578+ true )
579+ . then ( ( ) => {
580+ return Settings . change (
581+ "developer.powerShellExePath" ,
582+ undefined ,
583+ true )
584+ } )
585+ . then ( ( ) => {
586+ this . suppressRestartPrompt = false ;
587+ } ) ;
588+ }
589+ } ) ;
590+
591+ powerShellExePath = fixedPath ;
592+ }
593+ }
594+
545595 return powerShellExePath . length > 0
546596 ? this . resolvePowerShellPath ( powerShellExePath )
547- : this . getDefaultPowerShellPath ( this . sessionSettings . useX86Host ) ;
597+ : getDefaultPowerShellPath ( this . platformDetails , this . sessionSettings . useX86Host ) ;
548598 }
549599
550600 private changePowerShellExePath ( exePath : string ) {
@@ -554,78 +604,6 @@ export class SessionManager implements Middleware {
554604 . then ( ( ) => this . restartSession ( ) ) ;
555605 }
556606
557- private getPowerShellExeItems ( ) : PowerShellExeDetails [ ] {
558-
559- var paths : PowerShellExeDetails [ ] = [ ] ;
560-
561- if ( this . isWindowsOS ) {
562- const is64Bit = process . env . hasOwnProperty ( 'PROCESSOR_ARCHITEW6432' ) ;
563- const rootInstallPath = ( is64Bit ? process . env . ProgramW6432 : process . env . ProgramFiles ) + '\\PowerShell' ;
564-
565- if ( fs . existsSync ( rootInstallPath ) ) {
566- var psCorePaths =
567- fs . readdirSync ( rootInstallPath )
568- . map ( item => path . join ( rootInstallPath , item ) )
569- . filter ( item => fs . lstatSync ( item ) . isDirectory ( ) )
570- . map ( item => {
571- return {
572- versionName : `PowerShell Core ${ path . parse ( item ) . base } ` ,
573- exePath : path . join ( item , "powershell.exe" )
574- } ;
575- } ) ;
576-
577- if ( psCorePaths ) {
578- paths = paths . concat ( psCorePaths ) ;
579- }
580- }
581-
582- if ( is64Bit ) {
583- paths . push ( {
584- versionName : "Windows PowerShell (x64)" ,
585- exePath : process . env . windir + '\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe'
586- } )
587- }
588-
589- paths . push ( {
590- versionName : "Windows PowerShell (x86)" ,
591- exePath : process . env . windir + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
592- } )
593- }
594- else {
595- paths . push ( {
596- versionName : "PowerShell Core" ,
597- exePath :
598- os . platform ( ) === "darwin"
599- ? "/usr/local/bin/powershell"
600- : "/usr/bin/powershell"
601- } ) ;
602- }
603-
604- return paths ;
605- }
606-
607- private getDefaultPowerShellPath ( use32Bit : boolean ) : string | null {
608-
609- // Find the path to powershell.exe based on the current platform
610- // and the user's desire to run the x86 version of PowerShell
611- var powerShellExePath = undefined ;
612-
613- if ( this . isWindowsOS ) {
614- powerShellExePath =
615- use32Bit || ! process . env . hasOwnProperty ( 'PROCESSOR_ARCHITEW6432' )
616- ? process . env . windir + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
617- : process . env . windir + '\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe' ;
618- }
619- else if ( os . platform ( ) == "darwin" ) {
620- powerShellExePath = "/usr/local/bin/powershell" ;
621- }
622- else {
623- powerShellExePath = "/usr/bin/powershell" ;
624- }
625-
626- return this . resolvePowerShellPath ( powerShellExePath ) ;
627- }
628-
629607 private resolvePowerShellPath ( powerShellExePath : string ) : string {
630608 var resolvedPath = path . resolve ( __dirname , powerShellExePath ) ;
631609
@@ -679,7 +657,7 @@ export class SessionManager implements Middleware {
679657
680658 var currentExePath = this . powerShellExePath . toLowerCase ( ) ;
681659 var powerShellItems =
682- this . getPowerShellExeItems ( )
660+ getPowerShellExeItems ( this . platformDetails )
683661 . filter ( item => item . exePath . toLowerCase ( ) !== currentExePath )
684662 . map ( item => {
685663 return new SessionMenuItem (
@@ -748,11 +726,6 @@ export class SessionManager implements Middleware {
748726 }
749727}
750728
751- interface PowerShellExeDetails {
752- versionName : string ;
753- exePath : string ;
754- }
755-
756729class SessionMenuItem implements vscode . QuickPickItem {
757730 public description : string ;
758731
0 commit comments