diff --git a/scripts/Start-EditorServices.ps1 b/scripts/Start-EditorServices.ps1 index ff7198aff2..ce2545d0fa 100644 --- a/scripts/Start-EditorServices.ps1 +++ b/scripts/Start-EditorServices.ps1 @@ -65,24 +65,38 @@ 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 + ExitWithError "Unsupported PowerShell version $($PSVersionTable.PSVersion), language features are disabled." +} - Write-Host "Unsupported PowerShell version $($PSVersionTable.PSVersion), language features are disabled.`n" +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() + } - exit 0; + ExitWithError "PowerShell is configured with an unsupported LanguageMode (ConstrainedLanguage), language features are disabled." } # Are we running in PowerShell 5 or later? @@ -240,5 +254,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 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;