-
Notifications
You must be signed in to change notification settings - Fork 510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove-Variable can cause cls
and Clear-Host
to throw You cannot call a method on a null-valued expression.
#3718
Remove-Variable can cause cls
and Clear-Host
to throw You cannot call a method on a null-valued expression.
#3718
Comments
You cannot call a method on a null-valued expression.
cls
and Clear-Host
to throw You cannot call a method on a null-valued expression.
I'm not really sure what can be done here. @daxian-dbw do you know if it's possible to make a variable non-removable via
|
I'm wondering if creating the variable with |
But for the record this is not a particularly advisable practice. A better approach is to consistently declare variables, e.g. # Less advisable example
foreach ($thing in $stuff) {
if ($thing -eq 5) {
$found = $true
}
}
if (-not $found) { throw }
# Better
$found = $false
foreach ($thing in $stuff) {
if ($thing -eq 5) {
$found = $true
}
}
if (-not $found) { throw } That said, there is a middle ground: $existingVariables = Get-Variable
try {
whole script here
} finally {
Get-Variable |
Where-Object Name -notin $existingVariables.Name |
Remove-Variable
} |
|
Good point, could break someone if they happen to use |
Thanks both! I'll resolve this with a code change that prevents users from accidentally removing the variable. |
🙏 |
I was able to repro this, fix it, and verify the fix: @andys-mac-mini ~
> Remove-Variable psEditor
Remove-Variable: Cannot remove variable psEditor because it is constant or read-only. If the variable is read-only, try the operation again specifying the Force option.
@andys-mac-mini ~
> $psEditor.EditorServicesVersion
Major Minor Build Revision
----- ----- ----- --------
3 0 2 0 |
Prerequisites
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
orClear-Host
in a script or in the integrated terminal, I receive the following error: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 apwsh
terminal does not cause the error to appear oncls
.Restarting the terminal (clicking the trash icon, for instance) "fixes" the issue until the command is re-executed.
Adding
$psEditor
to the-Exclude
parameter inGet-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
Visual Studio Code Version
Extension Version
Steps to Reproduce
Visuals
Logs
logs20211210.zip
The text was updated successfully, but these errors were encountered: