Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Wrong variable scope displayed when debugging #1974

Closed
christian-boks opened this issue Oct 7, 2018 · 9 comments
Closed

Wrong variable scope displayed when debugging #1974

christian-boks opened this issue Oct 7, 2018 · 9 comments
Assignees
Labels

Comments

@christian-boks
Copy link

The debugger shows variable content from the wrong scope:

image

Program output:
Inner animal: cat
Outer animal: dog

Using:
Delve Debugger
Version: 1.1.0
Build: $Id: 1990ba12450cab9425a2ae62e6ab988725023d5c $

And Go Extension v 0.6.90

@ramya-rao-a
Copy link
Contributor

We get the variable information from delve.

Can you try running dlv from the command line to debug your code, run locals to get the local variables and see if animal points to dog or cat?

@chainhelen
Copy link

image

@christian-boks
Copy link
Author

This is the output I get when running dlv

> main.main() c:/gohome/src/play/scope_bug/main.go:9 (PC: 0x48df25)
     4:
     5: func main() {
     6:         animal := "dog"
     7:         if true {
     8:                 animal := "cat"
=>   9:                 fmt.Println("Inner animal:", animal)
    10:         }
    11:         fmt.Println("Outer animal:", animal)
    12: }
(dlv) locals
(animal) = "dog"
animal = "cat"

@jhendrixMSFT jhendrixMSFT self-assigned this Jan 16, 2019
@jhendrixMSFT
Copy link
Member

I have a fix for this, one thing that comes to mind is if we should also display the shadowed value(s) like delve does in the variables pane.

@ramya-rao-a
Copy link
Contributor

I would stick to just showing the non-shadowed value.

@jhendrixMSFT
Copy link
Member

I tend to agree however there are cases where having both values would be useful. IMO what would be ideal is if we could display the shadowed value when hovering over the shadowed variable. Can we subscribe to some event when hovering over a variable?

@ramya-rao-a
Copy link
Contributor

Can we subscribe to some event when hovering over a variable?

Extensions dont get to subscribe to such events. When not in debugging mode, the hoverprovider implemented by the extension is called by VS Code to get the data for display.

In debugging mode, whatever text is being hovered on is then looked up in the variables that the debug adapter has returned in the variablesRequest.

So, as far as I know we cannot do much here.

But showing the shadowed variable with () in the variable pane is something we can definitely do

@jhendrixMSFT
Copy link
Member

This starts to get a little unwieldy if a variable is shadowed more than once. Perhaps it would be better if we displayed the shadowed variables as children of one another, what do you think (assuming vscode don't have problems with child vars with the same name)?

@ramya-rao-a
Copy link
Contributor

Shadowed variables are now showing inside parentheses in the latest update (0.9.0) to the Go extension.
Thanks @jhendrixMSFT!

@vscodebot vscodebot bot locked and limited conversation to collaborators Mar 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants