Debugging go in vscode doesn't stop at breakpoints, says "Could not find file ..." when debugger starts
Asked Answered
A

1

11

Ubuntu. vscode 1.62.1. go1.17.3. vscode go extension v0.29.0. delve v1.7.1.

I'm new to vscode and Go. I have many years of experience debugging Java apps in Eclipse.

I've constructed a small multi-module Go app. I can set a breakpoint in main and other functions in other modules. Inside main.go, I select "Start Debugging".

It starts the application, and I can tell it's working from the console, and that the REST endpoint responds with my dummy response.

However, it will NOT stop at breakpoints. As soon as I start the session, the red breakpoint markers suddenly become hollow, and hovering on one of them shows a message "Could not find file ...", which prints the full path to the source file in question.

When I start it, it shows the following in the console:

Starting: /home/.../go/bin/dlv-dap dap --check-go-version=false --listen=127.0.0.1:43347 --log-dest=3 from /home/.../... DAP server listening at: 127.0.0.1:43347

I haven't modified the launch.json (I hope someday a friendlier interface to editing launch configurations is provided).

What else could I be doing wrong?

Update:

This is a screenshot showing main.go just before I press F5 (Start Debugging):

Before pressing F5

Notice that I have a breakpoint on the print statement, on the first line of main.

This is what I see after I press F5:

After pressing F5

Notice that it printed "At start of main" in the console. It didn't stop at the breakpoint. Also notice message in tooltip when hovering over the breakpoint.

Update:

This is a view of my directory structure:

project directory structure

Antependium answered 11/11, 2021 at 23:15 Comment(3)
Could you also provide a screenshot of your current workspace? Does the root of your vscode workspace contain main.go or is it in a subdirectory?Sharonsharona
main.go, along with go.mod and go.sum, are in the root. Other modules are in subfolders (in different packages).Antependium
@aside what difference would it make if it is subdirectory?Battista
A
4

First, just make sure you have initiated your project with go mod init voltagems: that would explain the import "voltagems/xxx", but also helps delve to find your main.go file at debug time.
You should have go.mod and go.sum files beside main.go.

Second, check your go env output, making sure GOPATH and GOROOT are set to default paths.

The OP David M. Karr adds in the comments:

I did run "go mod init" when I first created the project, but I realized that I didn't like the root module name, so I changed it to "voltagems"

I believe you can edit directly go.mod first line, and make sure it says:

module voltagems

Then go mod verify + go mod tidy

Finally, go build .. Restart your VSCode (or the command Reload Window), and see if the issue persists.


The OP David M. Karr points out to a root cause:

There are symbolic links in my project path.

There is a "substitutePath" configuration in VSCode-Go that is used to map to absolute paths.

You can see this parameter mentioned in Debugging with Legacy Debug Adapter

substitutePath

Path mappings to apply to get from a path in the editor to a path in the compiled program (default: []).

That comes from issue 622 "debug: breakpoints don't work when working with symlink".
And commit 93f32bb

src/debugAdapter: add substitutePath config for debugging

This change adds a new configuration option to both launch and attach requests.
substituePath takes an array that maps from string to string that is used to translate paths passed to the debugger and then back to the client.

This allows users to translate their symlinked directories to the files that were actually used to build the binary.
In addition this can also be used for remote debugging, and when the location of the files has moved since the program was built.

Example: you need a from and to key:

    "substitutePath": [
        {
            "from": "/symlink/path/dir/on/local/machine",
            "to": "/absolute/path/dir/on/local/machine",
        },
Annal answered 15/11, 2021 at 21:24 Comment(11)
I did run "go mod init" when I first created the project, but I realized that I didn't like the root module name, so I changed it to "voltagems". Can I just run that command again? If this is what I needed, you'd think vscode-go would know something was out of sync and notified me with a warning of some type.Antependium
In the root of the project, I have go.mod, go.sun, and main.go.Antependium
GOPATH="/home/dk068x/go"; GOPATH="/home/dk068x/go"Antependium
@DavidM.Karr I have edited the answer to address your comment.Annal
Unfortunately, none of that made any difference. Same result at the end. Is there a "delve" log file, and can we perhaps increase the log level for it?Antependium
@DavidM.Karr Not that I know of (for instance github.com/go-delve/delve/issues/1217). At this stage, I would create a new minimal go mod project in a new folder, just to see if the issue persists in said new project.Annal
I've managed to resolve this. There are symbolic links in my project path. There is a "substitutePath" configuration in vscode-go (that I don't find much documentation for) that is used to map to absolute paths.Antependium
@DavidM.Karr Thank you for this feedback. I have included your comment in the answer for more visibility, and added documentation around substitutePath.Annal
And note that none of this documentation actually mentions the format that is actually required. It needs to say this exactly. It needs a map with keys named "from" and "to". Without that, people are just guessing.Antependium
@DavidM.Karr Indeed. I have edited the answer to add an example, making that map visible for other readers.Annal
substitudePath worked for me. Thanks.Culprit

© 2022 - 2024 — McMap. All rights reserved.