Skip to content

Remove-Variable can cause cls and Clear-Host to throw You cannot call a method on a null-valued expression. #3718

Closed
PowerShell/PowerShellEditorServices
#1647
@ruffin--

Description

@ruffin--

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.

Summary

If I use this command, somewhat blindly copied from this answer:

Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0

And then use cls or Clear-Host in a script or in the integrated terminal, I receive the following error:

You cannot call a method on a null-valued expression.
At C:\Users\MyUser\.vscode\extensions\ms-vscode.powershell-2021.10.2\modules\PowerShellEditorServices\Commands\Public\Clear-Host.ps1:12 char:9
+         $psEditor.Window.Terminal.Clear()
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

The terminal is otherwise cleared.

Because it's referencing a script in the .vscode folder, I'm assuming this is an extension issue. Entering this command in a pwsh terminal does not cause the error to appear on cls.

Restarting the terminal (clicking the trash icon, for instance) "fixes" the issue until the command is re-executed.

restarting the terminal with the kill switch

Adding $psEditor to the -Exclude parameter in Get-Variable also seems to sidestep the issue.

Get-Variable -Exclude PWD,*Preference,psEditor | Remove-Variable -EA 0

The bug is likely (?) that $psEditor is exposed to the terminal window and is removable.

PowerShell Version

PS C:\projects\pslint> $PSVersionTable


Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}       
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

PS C:\projects\pslint> code --version

1.62.3
ccbaa2d27e38e5afa3e5c21c1c7bef4657064247
x64

Extension Version

PS C:\projects\pslint> code --list-extensions --show-versions | Select-String powershell


ms-vscode.powershell@2021.10.2

Steps to Reproduce

PS> Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0
PS> cls

Visuals

image

Logs

logs20211210.zip

Metadata

Metadata

Assignees

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions