Vscode-powershell: Integrated Console trims leading and trailing whitespace before executing commands, results in multiple PSReadLine HistoryItem's in the current session

Created on 20 Feb 2021  路  6Comments  路  Source: PowerShell/vscode-powershell

System Details

Extenion: Preview 2020.9.0 (and stable 2020.6.0)
System Details Output

### VSCode extensions:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]


### PSES version: 2.3.0.0

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      7.1.2
PSEdition                      Core
GitCommitId                    7.1.2
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Description

Revised 2021-02-21 as the scope of the issue become larger.

If a command is entered in to the integrated console with leading and/or trailing whitespace, the whitespace is trimmed before the command is executed. Then, recalling command history shows two nearly identical commands, the most recent with the whitespace trimmed, and the next recent with the original whitespace.

I originally found this with multiline commands where the last line was blank. This was of no real consequence, other than the result of the command history having two entries. However getting a better understanding, I've realized this applies to all whitespace, and trailing whitespace may actually be of importance and should not be removed.

If the following commands are entered one at a time, they will result in different output in the integrated console than they will in a normal PowerShell session.

# note there is a trailing space after the backtick on the next line!
$a = echo hello`  
$a | format-hex

Correct result:

                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 68 65 6C 6C 6F 20                               hello 

Incorrect result (integrated console):

                00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 68 65 6C 6C 6F 60                               hello`

This also affects using the F8 (Run Selection) command. If the selection contains intentional trailing whitespace, it will be removed. However this will not result in two command history entries.

There are three separate but related issues:

  • console input commands are trimmed of leading and trailing whitespace which is not the normal console behavior
  • console input commands are added to the command history system twice, once by PSReadLine when the command line is returned, and again by the integrated console just after getting it from PSReadLine.
  • commands/scripts not console input (ie: F8) are also trimmed of leading and trailing whitespace, which may cause unexpected results since it is not consistent with normal console behavior nor with script file execution behavior.
Area-Integrated Console Issue-Bug

Most helpful comment

I am pretty sure PSReadLine does not trim trailing white space, as the issue only occurs in the Integrated Console, and not in regular PowerShell console instances, and likewise the only place the trimming is happening is in the integrated console.

As a side issue, observe the output of the following script on both the integrated console and a regular (not integrated) powershell session:

# note there is a trailing space after the backtick on the next line!
$a = echo hello`  
$a | format-hex

If these lines are pasted 1 at a time in to integrated console, the hex output will be missing the space (Edit: actually it will have a backtick instead of a space) following the word hello. If they are pasted together, the space will be present. A regular powershell session will have the same result either way the commands are entered.

correct

                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 68 65 6C 6C 6F 20                               hello 

wrong (integrated console)

                00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 68 65 6C 6C 6F 60                               hello`

All 6 comments

I can confirm this happens to all commands with trailing white space as well as trailing blank lines. If I echo test, there will be two history entries, one with out the trailing spaces (most recent) and one with the original spaces.

This looks like an enhancement request for PSReadLine rather than in the PS extension specifically. @daxian-dbw I don't seem to be able to transfer it -- can you?

This does not occur in a regular console.

Oh I see. @daxian-dbw do you know if PSRL is designed to trim trailing whitespace before deciding how/whether to store something in history?

I am pretty sure PSReadLine does not trim trailing white space, as the issue only occurs in the Integrated Console, and not in regular PowerShell console instances, and likewise the only place the trimming is happening is in the integrated console.

As a side issue, observe the output of the following script on both the integrated console and a regular (not integrated) powershell session:

# note there is a trailing space after the backtick on the next line!
$a = echo hello`  
$a | format-hex

If these lines are pasted 1 at a time in to integrated console, the hex output will be missing the space (Edit: actually it will have a backtick instead of a space) following the word hello. If they are pasted together, the space will be present. A regular powershell session will have the same result either way the commands are entered.

correct

                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 68 65 6C 6C 6F 20                               hello 

wrong (integrated console)

                00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 68 65 6C 6C 6F 60                               hello`

It appears that this issue probably belongs in EditorServices, but VSCode only knows of one repository for a given extension when reporting issues.

It also appears that this behavior is related to approximately:
https://github.com/PowerShell/PowerShellEditorServices/blob/29e44b6ebdc63de70bd06538db03f3298ba2cfc6/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs#L1035

But this is just a guess. This looks like the function that might be executed if I was to run F8 with a section of a script file highlighted. It is seeming to be that this same function is getting called when ReadLine returns a line to be executed, but ReadLine has already registered the command in History. (In PSReadLine's ReadLine.cs InputLoop() line 490)

Possibly here:
https://github.com/PowerShell/PowerShellEditorServices/blob/29e44b6ebdc63de70bd06538db03f3298ba2cfc6/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs#L868-L873

Ah so it's a bug in the current console implementation in PSES. In that case it's probably something to be fixed as part of https://github.com/PowerShell/PowerShellEditorServices/issues/1295

Was this page helpful?
0 / 5 - 0 ratings