vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Variable in Watch pane expression is not evaluated in the current function's scope

Open LanceUMatthews opened this issue 1 year ago • 0 comments

Prerequisites

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

Summary

When the debugger breaks inside a function which sets a new value for a variable that was already set in the parent scope, a Watch pane expression monitoring that variable shows the value from the parent scope instead of the value from the current, function scope.

This behavior was previously reported in #1219.

PowerShell Version

PS C:\Code> $PSVersionTable; $Host

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

Name             : Visual Studio Code Host
Version          : 2024.2.2
InstanceId       : 7faeaf28-aa27-43ff-9c1f-0ad26934f3ee
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Visual Studio Code Version

1.91.0
ea1445cc7016315d0f5728f8e8b12a45dc0a7286
x64

Extension Version

[email protected]

Steps to Reproduce

Consider the following script that sets and outputs the value of a $scope variable before, inside, and after a function call:

function Write-Scope
{
	Write-Host -Object "$`scope: $scope"
}

function Foo
{
	Write-Scope
	$scope = 'Foo'
	Write-Scope
}

$scope = 'script'

Write-Scope
Foo
Write-Scope

The script writes the following text to the console:

$scope: script
$scope: script
$scope: Foo   
$scope: script

As seen in the attached screenshot, when the debugger breaks after a new value is assigned to $scope inside the Foo function, the expression $scope in the Watch pane still evaluates to the value from the parent scope ("script"). The expected value for $scope ("Foo") is displayed in the following locations:

  • Inside the Local variable group of the Variables pane.
  • After hovering the cursor over the $scope token in the code editor.
  • After evaluating the expression $scope in the Terminal.

Visuals

WatchPaneVariableScopeBug

Logs

No response

LanceUMatthews avatar Jul 05 '24 07:07 LanceUMatthews