How to use Delve debugger in Visual Studio Code
Asked Answered
Q

6

37

I have installed the Go extension for VS Code, but unable to make it work.

"dlv debug" works alright from the terminal.

dlv debug src/github.com/user/hello

The launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceRoot}",
            "env": {},
            "args": []
        }
    ]
}

Do you know how to set it up?

Quiberon answered 20/8, 2016 at 21:22 Comment(0)
J
72

For using Delve debugger in Visual Studio Code with Golang, do the following steps:

( Note: for Windows OS replace all $GOPATH with %GOPATH% )
  • Install Latest Golang and set GOROOT and GOPATH
  • Add $GOPATH/bin to your OS PATH environment variable.
  • set environment variable: GO15VENDOREXPERIMENT = 1
  • run: go get github.com/derekparker/delve/cmd/dlv and make sure dlv binary generated in your $GOPATH/bin
  • Install Visual Studio Code
  • Launch VS Code Quick Open (Ctrl+P), paste this command: ext install Go , and press enter.
  • click install Rich Go language support for Visual Studio Code
  • click Enable and restart Visual Studio Code
  • Inside Visual Studio Code Open Folder Ctrl+Shift+E , e.g.: $GOPATH\src\hello\
  • Then Open hello.go from that folder (or make new file Ctrl+N and save it on this folder):
package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
    i := 101
    fmt.Println(i)
}
  • Then Open Debugger Ctrl+Shift+D
  • on this line: i := 101 press F9 to set or toggle beakpoint.
  • Press F5 to start debugging or to Run the application, if asked to select environment: select Go.
  • Press F10 to Step Over.
  • Press F11 to Step Into.
  • Press Shift+F11 to Step Out.
  • Press Shift+F5 to Stop Debugging.
  • Press Ctrl+Shift+F5 to Restart Debugging.

My launch.json untouched:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}",
            "env": {},
            "args": [],
            "showLog": true
        }
    ]
}

Result:

enter image description here

Jonson answered 21/8, 2016 at 9:24 Comment(12)
@chris-g I hope this helps.Jonson
If you have some truble with VS Code, For fresh debug session, try: File/Close Folder, File/Open Folder, From Explorer on left panel click hello.go` and open it, Press F9 for break point, Press F5 select Go , Close json file, Click debugger, Press F5`Jonson
Thank you! Though I still can not get the debugger to work. I get: can't load package: package .: no buildable Go source files in /Users/xx/godeep exit status 1Quiberon
I tried to start VS code from the terminal to adopt $GOPATH and just from the Application folder.Quiberon
see: Make sure you are using that command in the Go project source folder: #24095504Jonson
Thanks; That is for building/installing the right package - this works alright in the terminal incl. dlv. Should I set this somewhere in launch or settings?Quiberon
you ware right all along, for some reason I missed your 9. step - starting "vs code" in the package folder. Thanks again!Quiberon
Thanks! This is way more useful than anything else I could find using Google (at least as of now).Lordinwaiting
I get an error for the launch config. Error: unknown flag: --wd Process exiting with code: 0Sherrellsherrer
2018 June : Delve , Go and VSCode just don't work reliably. Don't even bother trying, and save some pain. Debugger is unreliable, watches hardly ever work, stepping is erratic etc etc.Prosthesis
where do i find launch.json file?Sextan
I believe this answer is obsolete. Please use @amolgautam's answer as it worked for me and is WAY SIMPLERPetty
W
9

You have to do three things here:

  • Install Delve. Looking at your question it seems that you have already installed Delve.
  • Install Go extension for VS Code. The extension can be found at : https://github.com/golang/vscode-go
  • Install dlv tool for Go. You can do that by opening up Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and select Go: Install/Update Tools then search/select dlv

Now you can start debugging with delve in VS code.

More more detailed instruction please follow : https://dev.to/nyxtom/debugging-in-go-in-vs-code-1c7f

Willdon answered 4/8, 2021 at 13:38 Comment(1)
Thanks! This seems to be the real answer. The above is no longer relevant.Petty
M
4

This launch.json worked for me to run the Golang debugger in VSCode:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch file",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${file}",
      "env": {
        "PATH": "/usr/local/go/bin:${fileDirname}"
      },
      "args": []
    }
  ]
}

VSCode Variables Reference: If file /home/your-username/your-project/folder/main.go is open in VSCode and

directory /home/your-username/your-project is your root workspace, then

${file} = /home/your-username/your-project/folder/main.go

${fileDirname} = /home/your-username/your-project/folder


My specific values:

$GOROOT: /usr/local/go

$GOPATH: /Users/myname/code

${file}: /Users/myname/code/src/github.com/githubName/appName/main.go

${fileDirname}: /Users/myname/code/src/github.com/githubName/appName

Monomerous answered 1/9, 2019 at 14:49 Comment(0)
T
0

FTA (in case it is hard to find), if when using delve and you get cannot find package error even though your GOPATH is set correctly, check out this bug of vscode-go, it is affecting both MAC OS and Linux, as of October, 2017.

The solution is posted there as well:

... adding the GOPATH as an env var in the env property in the launch.json file solved the problem

Teat answered 9/10, 2017 at 17:28 Comment(0)
S
0

Content launch.json for gdb and delve

{
// Используйте IntelliSense, чтобы узнать о возможных атрибутах.
// Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов.
// Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Delve",
        "type": "go",
        "request": "launch",
        "mode": "debug",
        "remotePath": "",
        "port": 2345,
        "host": "127.0.0.1",
        "program": "${workspaceRoot}/src/hello/hello.go",
        "env": {},
        "args": [],
        "showLog": true
    }
   ,
    {
        "type": "gdb",
        "request": "launch",
        "name": "GDB",

        "target": "${workspaceRoot}/src/hello/hello",
        "cwd": "${workspaceRoot}",
        "linux": {
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
    }
]

}

Subtitle answered 13/4, 2018 at 15:14 Comment(1)
While this code snippet may solve the question, including an explanation helps to improve the quality of your response. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Tomokotomorrow
A
0

If you got error : Failed to continue: "Cannot find Delve debugger. Install from https://github.com/go-delve/delve & ensure it is in your Go tools path, "GOPATH/bin" or "PATH"."

Maybe you can use my solution (on mac) :

  1. First follow how to install delve on here : https://github.com/go-delve/delve/tree/master/Documentation/installation
  2. Open your vscode, go to your project directory, then on vscode terminal : go mod init example.com/m/v1
  3. run : go get github.com/go-delve/delve/cmd/dlv
  4. Open mac terminal and run : open ~/.zshrc (if not exist, run : touch ~/.zshrc)
  5. Make sure that file contains this script :

export GOPATH=$HOME/go

export PATH=$PATH:$GOPATH/bin

export PATH=$PATH:$GOROOT/bin

export GOROOT=/usr/local/go

  1. Then copy all script on .zshrc
  2. Still on mac terminal, run : open ~/.bashrc (if not exist, run : touch ~/.bashrc)
  3. Paste the script to .bashrc file
  4. Then save .zshrc and .bashrc
  5. close terminal & close vscode
  6. Open vscode again
  7. The last thing, make sure you already create launch.json (in .vscode folder), then you can follow my launch.json :
{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}",
            "args": []
        }
    ]
} 
  1. Then finish ! You can try again run your program and follow step2 using delve on accepted answer
Augustina answered 27/6, 2022 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.