Not able to build go package for debugging in vscode

Viewed 2190

Recently I have updated go debugger to dlv-dap and now I have problems debugging go packages. This is my file structure:

my file structure

File contents:

main.go

package main

import (
    "debugger_test/packageA"
    "fmt"
)

func main() {
    res := packageA.DoStuff(5)
    fmt.Println(res)
}

fileA.go

package packageA

func DoStuff(a int) int {
    temp := a*a - 30
    return temp
}

fileA_test.go

package packageA

import (
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestStuff(t *testing.T) {
    arg := 2
    res := DoStuff(arg)
    assert.Equal(t, -26, res)
}

launch.json:

    "version": "0.2.0",
    "configurations": [
        {
            "name": "Test",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "program": "${fileDirname}",
            "substitutePath": [
                {
                    "from": "/home/vlasov",
                    "to": "/opt/storage",
                },
            ],
        },
        {
            "name": "Main",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${fileDirname}",
            "substitutePath": [
                {
                    "from": "/home/vlasov",
                    "to": "/opt/storage",
                },
            ],
        }
    ]
}

This used to work just fine before dlv-dap. Now when I try to debug I get this message:

Starting: /home/vlasov/go/bin/dlv-dap dap --listen=127.0.0.1:46369 --log-dest=3
DAP server listening at: 127.0.0.1:46369
Build Error: go test -c -o /opt/storage/debugger_test/packageA/__debug_bin -gcflags all=-N -l /home/vlasov/debugger_test/packageA
directory /home/vlasov/debugger_test/packageA outside available modules (exit status 1)

A partial fix I was able to find is to change ${fileDirname} to ${fileDirname}/main.go, however this way debugger does not hit breakpoints in main.go. This method does not work for tests, I still get similar message with "program": "${fileDirname}/fileA_test.go".

What is the correct way to configure launch.json to be able to run debug tests and packages normally?

1 Answers

It looks like an issue caused by the transition to dlv dap (a new debug adapter). There is an open issue https://github.com/golang/vscode-go/issues/1677.

I guess the root cause here is the use of symlink. The go command uses the real file path, while the extension passes the file path based on the symlink and made the go command to build the program confused by two file paths.

Until the issue is resolved (we hope to fix it soon), use the legacy debug adapter https://github.com/golang/vscode-go/blob/master/docs/debugging-legacy.md#selecting-legacy-debug-adapter

Thanks for being patient.

Related