24

VScodeDebugGoAppEngine

Hello World tutorial that shows how to setup VS Code to debug Golang App Engine code with Visual Studio (aka VScode )

This is using using the Helloworld code from AppEngine documentation:

go get -u -d github.com/directmeasure/VScodeDebugGoAppEngine.git

on a Mac running osX 10.13.3.

I've tested the code and the server works locally. I'm trying to figure out how to enter into the code with the debugger so I can learn how to use the debugger on other projects.

These were the best instructions I could find for using VScode with GAE but they seem to be outdated based on updates to Golang(e.g. switch to Gcloud, -go_debugging flag and change of directory structure):
https://medium.com/@dbenque/debugging-golang-appengine-module-with-visual-studio-code-85b3aa59e0f

Here are the steps I took:

set up Environment

  • added to .bash_profile

    export BASEFOLDER="/Users/Bryan/google-cloud-sdk/" . 
    export GOROOT="/usr/local/go" # this shoudln't have to be set with current Version, doing it to follow the tutorial . 
    

How I have attempted to get debugger to run:

start local server .

dev_appserver.py --go_debugging=true app.yaml

attach local binary to Delve

 ps aux | grep _go_app 

dlv attach <#using the PID from the server binary>

Delve successfully attaches to the binary.

When I start the Debug session, the blue progress bar never stops scanning horizontally.

The VARIABLE sidebar is never populated with the variables in hello.go

The Breakpoint is set at hello.go: line 21

The Debug REPL terminal displays:

Verbose logs are written to:  
/var/folders/mw/0y88j8_54bjc93d_lg3120qw0000gp/T/vscode-go-debug.txt  
16:02:31, 2018-4-5  
InitializeRequest  
InitializeResponse  
Using GOPATH: /Users/Bryan/go  
fmt.Print(u)  
Please start a debug session to evaluate  

Here is the launch.json config:

{
    "version": "0.2.0",  
    "configurations": [   
    {
        "name": "Launch",
        "type": "go",
        "request": "launch",
        "mode": "debug",
        "remotePath": "",
        //"port": 1234,  
        "port": 2345   // docs say port should match assigned port headless server, https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code#remote-debugging
                         // this creates bind error
        "host": "127.0.0.1",
        "program": "${workspaceFolder}/hello.go",
        "env": {},
        "args": [],
        "showLog": true,
        "trace": true,
    }
    ]
}

Here are the versions I have installed:

go version go1.10 darwin/amd64  
$ gcloud version . 
Google Cloud SDK 197.0.0
app-engine-go 
app-engine-python 1.9.68
bq 2.0.31
core 2018.04.06
gsutil 4.30

VS code extension:
Go 0.6.78

EDIT###########################

$ lsof -n -i :8080
Bryan@Bryans-MacBook-Pro Thu Apr 12 17:02:04 ~ 
$ lsof -n -i :2345

Bryan@Bryans-MacBook-Pro Thu Apr 12 17:03:34 ~ 
$ ps aux | grep _go_app
Bryan             7433   0.0  0.0  2434840    800 s000  S+    5:03PM   0:00.00 grep _go_app
Bryan             7426   0.0  0.0 556603172   3896 s002  S+    5:02PM   0:00.01 /var/folders/mw/0y88j8_54bjc93d_lg3120qw0000gp/T/tmp8GWk1gappengine-go-bin/_go_app

Bryan@Bryans-MacBook-Pro Thu Apr 12 17:03:52 ~ 
$ dlv attach --headless -l "localhost:2345" 7426 /var/folders/mw/0y88j8_54bjc93d_lg3120qw0000gp/T/tmp8GWk1gappengine-go-bin/_go_app
API server listening at: 127.0.0.1:2345

When I start the Debugger, REPL shows:

Verbose logs are written to:
/var/folders/mw/0y88j8_54bjc93d_lg3120qw0000gp/T/vscode-go-debug.txt
couldn't start listener: listen tcp 127.0.0.1:2345: bind: address already in use
Process exiting with code: 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
BryanWheelock
  • 12,146
  • 18
  • 64
  • 109

2 Answers2

14

VS Code never attaches to Delve because it is waiting to connect to the remote Delve server at 127.0.0.1:2345. If you dlv attach in headless mode, listening at the correct address, you should hopefully be able to connect.

The steps below describe how to debug a Go App Engine application running with dev_appserver.py and no other tools/helpers. However, when you make changes to your Go code, dev_appserver.py recompiles and restarts the application, changing the PID Delve needs to debug. http://github.com/dbenque/delveAppengine can help keep Delve attached to the right process. See here for a tutorial.

  1. Install the VS Code Go extension.
  2. go get -u -d github.com/directmeasure/VScodeDebugGoAppEngine.git
  3. cd $GOPATH/src/src/github.com/GoogleCloudPlatform/golang-samples/appengine/helloworld

    Note: if your GOPATH has more than one entry, cd to the directory go get downloaded to.

  4. Start the App Engine development server:

    dev_appserver.py --go_debugging=true app.yaml

  5. Visit http://localhost:8080 to ensure the server is running.
  6. Find the PID of the Go process:

    ps aux | grep _go_app

  7. Start the Delve server (select any port available on your system):

    dlv --headless -l "localhost:2345" attach $GO_APP_PID

  8. Open the VS Code debug tab (⇧⌘D on macOS, Ctrl+Shift+D on Windows & Linux).
  9. Create a new launch configuration by clicking the gear and selecting any entry (see official docs here).
  10. Create a "Go: Connect to server" entry:create config dropdown

    Note: this is only a template - you can edit it later.

  11. Customize the config to point to the port you specified when starting Delve. Here is my full configuration:

    {
        "name": "Launch",
        "type": "go",
        "request": "launch",
        "mode": "debug",
        "remotePath": "",
        "port": 2345,
        "host": "127.0.0.1",
        "program": "${fileDirname}",
        "env": {},
        "args": [],
        "showLog": true
    }
    
  12. Add breakpoints as desired and visit http://localhost:8080 again. Execution should stop when a breakpoint is reached, variables should be listed in the variables section in VS Code, and the call stack should be in the call stack section.

For general help with debugging Go code in VS Code (not run with App Engine), see https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code.

Tyler B
  • 1,618
  • 9
  • 20
  • Shouldn't the port in the launch.json be the same as the assigned port in the the Delve headless server? – BryanWheelock Apr 10 '18 at 20:13
  • Yes. You're right. I edited the post to correct the port. – Tyler B Apr 10 '18 at 20:26
  • The documents say headless Delve server assigned port should match port config in launch.js but that creates a bind error. When I change port in config, Debug REPL shows API server listening but debugger sill doesn't show Variables. – BryanWheelock Apr 11 '18 at 14:56
  • The port specified in `dlv --headless -l "localhost:2345" attach` should definitely match the `"port": 2345,` line in the configuration. I'd suggest checking `lsof -n -i :2345` and `lsof -n -i :8080` (or whatever ports you're trying to use) to make sure everything is clear before starting everything up. – Tyler B Apr 11 '18 at 17:40
  • I checked that port 2345 was unused before attaching Delve and I still got a bind error. See the EDIT above. – BryanWheelock Apr 12 '18 at 21:10
1

Yes, it is outdated. The page you are getting from, does not exist. Instead, you can run

go get github.com/GoogleCloudPlatform/golang-samples/tree/master/appengine/helloworld/...
Jonathan Van Dam
  • 630
  • 9
  • 23
Victor Oliveira
  • 1,109
  • 1
  • 12
  • 29
  • The problem isn't getting the Helloworld code. I have downloaded the helloworld code. It works. I can't figure out how to get the debugger to start. – BryanWheelock Mar 17 '18 at 00:03