question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
andschwacommented, Dec 10, 2021

Thanks both! I’ll resolve this with a code change that prevents users from accidentally removing the variable.

1reaction
ruffin--commented, Dec 15, 2021

🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell Error: You cannot call a method on a null-valued ...
TerminatingError(Invoke-Expression): "You cannot call a method on a null-valued expression.". Even running the following script (E:\Scripts\ ...
Read more >
You cannot call a method on a null-valued expression
The simple answer for this one is that you have an undeclared (null) variable. In this case it is $md5 . From the...
Read more >
You cannot call a method on a null-valued expression - Reddit
On the first loop it always throws this error on Wait-Process -Name $ProcessName -ErrorAction SilentlyContinue , every following loop is fine.
Read more >
Optimize Cumulative Layout Shift - web.dev
In this guide, we'll cover optimizing common causes of CLS such as ... Layout shifts can suddenly push the content you are reading...
Read more >
Measuring Cumulative Layout Shift - Request Metrics
Cumulative Layout Shift (CLS), sometimes known as jank, is a measurement of how much elements move due to late-rendered content. You can think...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found