Vscode-powershell: write exit in PowerShell Integrated Console

Created on 23 Mar 2017  路  14Comments  路  Source: PowerShell/vscode-powershell

System Details

  • Operating system name and version: Windows 7 Pro 64bit
  • VS Code version: Version 1.10.2 [Commit 8076a19fdcab7e1fc1707952d652f0bb6c6db331]
  • PowerShell extension version: 0.11.0
  • Output from $PSVersionTable:
PS C:\folder> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.0.10586.117
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.117
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\folder>

Issue Description

When I write exit in the new PowerShell Integrated Console, nothing happens.

It would be nice if something happens. Possible solutions:

  • Terminal session closes
  • warning/error: command not allowed

Sample

Starting PowerShell...

PS C:\git\OlapDbMetaData> exit
PS C:\git\OlapDbMetaData>
verify-fix

All 14 comments

Good point. I'm not sure I'd want the session to end, but maybe there's a reason why someone might want to exit it? At the very least I should show a message, as you said.

Restarting the integrated console session could be handy to clear out global session state that could be causing problems. I know there's a Command Palette command for that but being able to do that from the IC seems natural. Perhaps restart is a separate cmdlet and/or perhaps on exit, you can warn that this would eliminate key editing features and instead offer to restart the session?

I considered that too, could be nice. Though I'm not sure that'd be the expected behavior when typing exit the first time. Maybe give a message the first time and then restart the console if they type exit again? Maybe the behavior could be configured?

I think I would expect typing exit to close the entire VSCode session. and maybe like a Restart-Console to well, restart the console. with an alias of restart maybe.
exit in the ISE closes the entire application.

I'm not sure that'd be the expected behavior when typing exit

Agreed. That's why I think the command should provide information about what exit would do and perhaps offer a choice to A) do nothing/cancel and maybe B) really exit the IC. In the text, you could also tell them about how to Restart the Current Session. You could maybe even make that one of the choices for exit.

Presumably, most of the time after this, a restart is what they would want and they wouldn't have to see this lengthy message again. :-) I'm not sure I'd bother distinguishing between the first and nth time exit is invoked. I don't think this would be a frequent thing.

@Dotps1 I'm not sure we should change the expected VSCode terminal behavior. If you type exit in the default terminal, that terminal closes and not VSCode.

We could add an Exit-VSCode command I suppose but I'm not sure there's that much value when I can just press Alt+F4. That's faster than typing exit. :-)

Expected Behavior

Exit keyword should terminate the Integrated Terminal process only (powershell.exe), no prompts or messages in Integrated Terminal. VSCode Editors should remain untouched.

This falls more in-line to expected behavior for Consoles. See about_language_keywords for reference.

_Cool Enhancement:_ send the powershell.exe exit code if non-zero and Standard Error to Output Window. If you really want a message about the exit behavior, you could put it in Output as well.

Use Cases

Two main uses for Exit keyword.

  1. Interactively running commands and ready terminate the Terminal. When running cmd.exe and powershell.exe directly, the Terminal is the only window so the process ends. I would expect an embedded terminal to do the same thing without killing the parent.
  1. Error Handling by terminating powershell.exe with Exit Codes so that apps launching it knows if script or powershell itself failed. Many apps use Exit Codes as simplest way to determine if a process failed or not. In such cases, your app is typically launching powershell.exe with -File or -Command parameters but is not reading Standard Error to know if it fails. They check for non-zero status codes (some give you choice of success code) to report an error to the application.

_Note:_ there is a bug in powershell.exe when using -File parameter, exit code is always 0 (success) regardless if script errors or not. It should actually exit with a non-zero code. So adding your own exit /exitcodes is mandatory to get it to fail properly.

Example

I have a Microsoft SSIS package that runs a powershell script. It has an Execute Task which kicks off powershell.exe -File script.ps1. The simplest way to verify if script ran successful or not is to check status code. So my script contains Catch Blocks with Write-Error commands to write the exception details to Standard Error and exit /exitcode command to end powershell.exe with a non-zero code. Execute Task then can fail and provide within it's logs the exception details captured from Standard Error.

ISE Problem

When developing and testing such scripts, you want to leave exit defined in the Catch block so you don't forget it later, and you want to test it fails with exit codes as expected. ISE, because it is a faux-terminal with editor and terminal sharing same process, exit kills both. Yuck! While developing/debugging, you don't want that to keep terminating your editor experience! This is why it is expected to end terminal and keep editors open.

Second a Restart-Terminal/PSConsole/Console command as a separate item.

This is why it is expected to end terminal and keep editors open.

You do realize that exiting the integrated console would effectively lobotomize the editing experience, right? The integrated console isn't just another terminal type. It is the process that hosts the PowerShellEditorServices which is what provides VSCode with "knowledge" of PowerShell scripts. You'd lose IntelliSense, goto definition/symbol, script analysis, code formatting, expand aliases, code fixes, etc.

Allowing folks to exit the integrated console without at least a warning would result in a lot bug reports IMO. So I don't agree with the premise that exit should just silently exit the PowerShell Integrated Console process.

BTW there is already a Restart the Current Session command that is useful when the PowerShell Integrated Console crashes or gets wedged (or you just want a fresh session).

Your right, but I did a test and it already prompts the user to restart if powershell.exe is terminated as can be seen below:

image

I did 2 tests that align closely with how the Exit keyword works. Steps are below:

Test 1 - Ending Terminal via VSCode:

  1. Open a Powershell Script
  2. Click the _trash can_ icon on right-side of Terminal Panel
    * X icon hides the Terminal Panel
    *
    Trash can ends the Terminal process
  3. Powershell.exe Exit Code should be 0 (success). Can't confirm.

Test 2 - Forcibly kill PowerShell Terminal:

  1. Open a Powershell Script
  2. Open Task Manager
  3. Kill powershell.exe
  4. Powershell.exe Exit Code should be non-zero (error). Can't confirm.

In both cases, I get an immediate prompt to Restart Powershell Session. It accomplishes both of our goals and keeps the implementation consistent with the native Console Host, and hopefully simple.

A few enhancements to make it better:

  • Make it more clear why a Restart is highly recommended (loss of core Editor experiences)
  • Powershell.exe exit code of 0 should adjust the message as a closed terminal and not error. Maybe invoke a Warning if possible.
  • Powershell.exe exit code of non-zero always invoke as Error.
  • Output Window to capture Standard Error and Exit Code of powershell.exe process if non-zero. Not sure if needed if closed successfully.

@dragonwolf83 agreed, a zero exit code should not complain about an error. Doesn't look like I have access to it with the current state of the createTerminal API in VS Code though. I'll ask about that.

Also, I'm tracking the restart command feature here: https://github.com/PowerShell/vscode-powershell/issues/622

Looks like exit code should be coming soon in July 2017 release of VS Code according to https://github.com/Microsoft/vscode/issues/31090. The node-pty piece is done.

Closing this issue as it is now being tracked in #1700

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rkeithhill picture rkeithhill  路  3Comments

daviwil picture daviwil  路  3Comments

lukegriffith picture lukegriffith  路  3Comments

timoline picture timoline  路  3Comments

MiYanni picture MiYanni  路  3Comments