From 306a520b2be7dba781d3341bd45c72905f152bb2 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 16 Mar 2017 10:37:00 -0700 Subject: [PATCH 1/2] Fix #401: Unsupported PowerShell version should display an error This change improves a previous fix where we report an unsupported PowerShell version when the user attempts to start the extension. The previous fix utilized ConvertTo-Json to write out the session details; this caused a failure since this cmdlet was introduced in PowerShell v3. We now write out teh JSON manually in this case. --- scripts/Start-EditorServices.ps1 | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/scripts/Start-EditorServices.ps1 b/scripts/Start-EditorServices.ps1 index ff7198aff2..9f96d917d4 100644 --- a/scripts/Start-EditorServices.ps1 +++ b/scripts/Start-EditorServices.ps1 @@ -65,24 +65,28 @@ param( $ConfirmInstall ) -function WriteSessionFile($sessionInfo) { - ConvertTo-Json -InputObject $sessionInfo -Compress | Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop +function ExitWithError($errorString) { + + Write-Host -ForegroundColor Red "`n`n$errorString" + + # Sleep for a while to make sure the user has time to see and copy the + # error message + Start-Sleep -Seconds 300 + + exit 1; } # Are we running in PowerShell 2 or earlier? if ($PSVersionTable.PSVersion.Major -le 2) { - $resultDetails = @{ - "status" = "failed" - "reason" = "unsupported" - "powerShellVersion" = $PSVersionTable.PSVersion.ToString() - }; + # No ConvertTo-Json on PSv2 and below, so write out the JSON manually + "{`"status`": `"failed`", `"reason`": `"unsupported`", `"powerShellVersion`": `"$($PSVersionTable.PSVersion.ToString())`"}" | + Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop - # Notify the client that the services have started - WriteSessionFile $resultDetails - - Write-Host "Unsupported PowerShell version $($PSVersionTable.PSVersion), language features are disabled.`n" + ExitWithError "Unsupported PowerShell version $($PSVersionTable.PSVersion), language features are disabled." +} - exit 0; +function WriteSessionFile($sessionInfo) { + ConvertTo-Json -InputObject $sessionInfo -Compress | Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop } # Are we running in PowerShell 5 or later? @@ -240,5 +244,5 @@ catch [System.Exception] { $e = $e.InnerException; } - Write-Error ("`r`nCaught error while waiting for EditorServicesHost to complete:`r`n" + $errorString) + ExitWithError ("Caught error while waiting for EditorServicesHost to complete:`r`n" + $errorString) } \ No newline at end of file From c2eb462b5db7ca92c393ddd8ebf9af949ad13fd4 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 16 Mar 2017 11:20:34 -0700 Subject: [PATCH 2/2] Fix #421: Unsupported PowerShell LanguageMode should be reported This change adds an additional check in the Start-EditorServices.ps1 script to see whether the LanguageMode has been locked to an unsupported mode like ConstrainedLanguage. The language server cannot be loaded and used in this case so the user should be notified. --- scripts/Start-EditorServices.ps1 | 10 ++++++++++ src/session.ts | 4 ++++ src/utils.ts | 1 + 3 files changed, 15 insertions(+) diff --git a/scripts/Start-EditorServices.ps1 b/scripts/Start-EditorServices.ps1 index 9f96d917d4..ce2545d0fa 100644 --- a/scripts/Start-EditorServices.ps1 +++ b/scripts/Start-EditorServices.ps1 @@ -89,6 +89,16 @@ function WriteSessionFile($sessionInfo) { ConvertTo-Json -InputObject $sessionInfo -Compress | Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop } +if ($host.Runspace.LanguageMode -eq 'ConstrainedLanguage') { + WriteSessionFile @{ + "status" = "failed" + "reason" = "languageMode" + "detail" = $host.Runspace.LanguageMode.ToString() + } + + ExitWithError "PowerShell is configured with an unsupported LanguageMode (ConstrainedLanguage), language features are disabled." +} + # Are we running in PowerShell 5 or later? $isPS5orLater = $PSVersionTable.PSVersion.Major -ge 5 diff --git a/src/session.ts b/src/session.ts index fad251e751..fdb9d1e48f 100644 --- a/src/session.ts +++ b/src/session.ts @@ -327,6 +327,10 @@ export class SessionManager { this.setSessionFailure( `PowerShell language features are only supported on PowerShell version 3 and above. The current version is ${sessionDetails.powerShellVersion}.`) } + else if (sessionDetails.reason === "languageMode") { + this.setSessionFailure( + `PowerShell language features are disabled due to an unsupported LanguageMode: ${sessionDetails.detail}`); + } else { this.setSessionFailure(`PowerShell could not be started for an unknown reason '${sessionDetails.reason}'`) } diff --git a/src/utils.ts b/src/utils.ts index 0db0a5619b..6dcb0bbf18 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -57,6 +57,7 @@ export function getPipePath(pipeName: string) { export interface EditorServicesSessionDetails { status: string; reason: string; + detail: string; powerShellVersion: string; channel: string; languageServicePort: number;