PowerShell / PowerShell/vscode-powershell

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

Open
#5,013 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area-Debugging Issue-Bug
Dominant language
TypeScript
Stars
1.9k
Forks
547
PR merge metrics
No merged PRs in 30d

Description

Prerequisites
  • I have written a descriptive issue title.
  • I have searched all open and closed 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.
  • 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
ms-vscode.powershell@2024.2.2
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the PowerShell script in the Steps to Reproduce section, pausing inside Foo after $scope is assigned. Trace the extension's debugger Watch expression evaluation and compare it with the Variables pane and Terminal evaluation. Done means the Watch pane resolves the current function scope and shows "Foo" rather than the parent value "script".

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell, typescript, vscode
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.