Resolve evaluate/watch variables from the correct scope#2311
Open
andyleejordan wants to merge 1 commit into
Open
Resolve evaluate/watch variables from the correct scope#2311andyleejordan wants to merge 1 commit into
andyleejordan wants to merge 1 commit into
Conversation
When a watch or `evaluate` request resolved a naked variable reference, `GetVariableFromExpression` searched the flat `variables` list, which is populated broadest-to-narrowest (global, then script, then local, then stack frames). `FirstOrDefault` therefore returned the global/parent-scope copy of a variable whenever the same name also existed in a more local scope, even though the Variables explorer and PowerShell itself show the local value. We now search the local, script, and global scope containers in that order first, falling back to the full flat list so names that only live in a frame still resolve. This matches PowerShell's own variable resolution semantics. Added a regression test (and `VariableScopeTest.ps1`) that shadows a variable across scopes and asserts the local value wins. Fixes #1882. Drafted by Copilot (Claude Opus 4.8). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a scoping bug (#1882) where the debugger's evaluate/watch feature would return a variable's value from a parent scope instead of the current local scope when variables of the same name exist in multiple scopes.
Changes:
- Reorder the variable search in
GetVariableFromExpressionto enumerate scope containers from narrowest to broadest (local → script → global), with a flat variable list fallback, ensuring correct variable shadowing behavior. - Add a regression test with a dedicated PowerShell test script that verifies the most-local variable value is returned.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs |
Replace flat variables list lookup with scope-ordered search (local → script → global → fallback) in GetVariableFromExpression |
test/PowerShellEditorServices.Test.Shared/Debugging/VariableScopeTest.ps1 |
New test script with a parent-scope and local-scope variable of the same name |
test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs |
New regression test asserting the local-scope value is resolved correctly |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
Author
|
I'm flabberghasted. That's pretty much the problem and a proper fix for it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #1882.
While stopped at a breakpoint, a watch /
evaluaterequest for a naked variable could return the value from a parent scope instead of the current one. Given:evaluating
$myVarNamereturned123instead of456, even though the Variables explorer (and PowerShell itself) correctly show456.Cause
DebugService.GetVariableFromExpressionsearched the flatvariableslist withFirstOrDefault. That list is populated broadest-to-narrowest (global → script → local → stack frames), so the global/parent copy of a shadowed name was always matched first.Fix
Search the
Local,Script, thenGlobalscope containers in that order first, falling back to the full flat list so frame-only names still resolve. This matches PowerShell's own variable resolution and what the Variables explorer displays. Only the name lookup changed; ID-based lookups elsewhere are unaffected.Testing
DebuggerResolvesVariableFromMostLocalScopeplus a sharedVariableScopeTest.ps1that shadows a variable across scopes and asserts the local value wins.DebugServiceTestspass on net8.0.