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

How to debug Go project located on remote linux server? #2242

Closed
tms320 opened this issue Jan 11, 2019 · 8 comments
Closed

How to debug Go project located on remote linux server? #2242

tms320 opened this issue Jan 11, 2019 · 8 comments
Assignees
Labels

Comments

@tms320
Copy link

tms320 commented Jan 11, 2019

I have some Golang project (simple 'hello' application) on remote Ubuntu server. It is located in '/home/user/go/src/hello' directory. The only project file is 'hello.go':

package main

import "fmt"

func main() {
    fmt.Println("Hello, world")
}

I have installed Go tools and Delve dubugger on server. The project compiled and run succesfully.
In terminal I go to the directory '/home/user/go/src/hello' and run delve server in 'headless' mode:

dlv debug --headless --listen=:2345

Then I see the message in terminal:

API server listening at: [::]:2345

As I understand the debug server has started and ready to work.
I want to debug the remote project from my Windows desktop where VSCode is installed.

So my first question is: What is correct way to open remote sources in VSCode?
I know about 'Remote VSCode' extension which works via SSH. But I didn't want to mess about installing 'rmate', creating SSH tunnel and so on.
There is a SFTP server on my Ubuntu machine. So I used 'Remote Workspace' extension which can work via SFTP. I created special 'code-workspace' file used by this extension to access the remote directory '/home/user/go/src/hello'. Then I open this file in VSCode via 'File --> Open Workspace' menu. Now I can see my remote directory in VSCode Explorer and I can edit my remote 'hello.go' file.

My second question is: How to configure 'launch.json'?
I have created configuration:

		{
			"name": "Remote",
			"type": "go",
			"request": "launch",
			"mode": "remote",
			"remotePath": "???",
			"port": 2345,
			"host": "REMOTE_SERVER_IP",
			"program": "???",
			"env": {}
		}

What should I set up for 'remotePath' and 'program' attributes?
I tried various combinations. Such as: "${workspaceRoot}/hello" and "/home/user/go/src/hello".
For example I set the following attributes:

"remotePath": "${workspaceRoot}/hello",
"program": "${workspaceRoot}/hello/hello.go",

Then I set breakpoint at line ' fmt.Println("Hello, world")' and press "Start Debugging" button.
I see that the remote delve server somehow reacts and displays the following message in remote terminal:

Hello, world
Terminated

But in VSCode I get the error message like this:
"Failed to continue: The program attribute must point to valid directory, .go file or executable."
And in the VSCode Debug Console I get the message:
The program "\home\user\go\src\hello/hello/hello.go" does not exist: Error: ENOENT: no such file or directory, lstat 'C:\home\user\go\src\hello\hello\hello.go'
As I understand VSCode tries to look for source file on my desktop machine (not remote server).

What is correct way to solve my task?

@ericshen881027
Copy link

ericshen881027 commented Feb 3, 2019

I have the same question about how to debug on the remote machine, the "remotePath" and "program" can only find the local path in stead of the remote machine path, otherwise you will get the "Failed to conitnue" error.
But when you select the local source file, the vscode debugger can only do the "start" and "stop", how can I set the breakpoints?
Here is my settings:

{
            "name": "Remote",
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "remotePath": "C:\\Users\\EricS\\go\\src\\test-struct",
            "port": 2345,
            "host": "10.142.114.150",
            "program": "C:\\Users\\EricS\\go\\src\\test-struct",
            "env": {}
}

and the dlv command (running on the remote machine):

dlv debug --listen=:2345 --headless=true --api-version=2 --log

get the message in the remote machine output:

API server listening at: [::]:2345
INFO[0000] launching process with args: [/root/go/src/test/test-remote-debug/debug] layer=debugger

@philippgille
Copy link

I'm currently having issues with this as well, but I found the following related issues that contain some further info:

@wcsiu
Copy link

wcsiu commented Mar 19, 2019

For the "program", you should put the current working directory if you are debugging the whole project, e.g. "${workspaceFolder}", "/Users/user-name/project-name". For "remotePath", the location of project in your remote server, it's similar to "program" but it is the location of code files in your remote server.

I have some questions of myself too.
For go modules, only having one path for "program" is not enough, my dlv giving something like ${workspaceFolder}/go/pkg/mod/some-files when it tried to match the file on my side. Is there any plan to solve it? like "program": "${workspaceFolder}":$GOPATH or using $GOPATH to look for dependency codes when go modules is used.

@tms320
Copy link
Author

tms320 commented Mar 21, 2019

For the "program", you should put the current working directory if you are debugging the whole project, e.g. "${workspaceFolder}", "/Users/user-name/project-name". For "remotePath", the location of project in your remote server, it's similar to "program" but it is the location of code files in your remote server.

You mean that I must have the same project files on my local windows desktop and on the remote Ubuntu server?

@ericshen881027
Copy link

ericshen881027 commented Mar 21, 2019

For the "program", you should put the current working directory if you are debugging the whole project, e.g. "${workspaceFolder}", "/Users/user-name/project-name". For "remotePath", the location of project in your remote server, it's similar to "program" but it is the location of code files in your remote server.

You mean that I must have the same project files on my local windows desktop and on the remote Ubuntu server?

You are right, you have to put the same project files in both sides, and set the correct path of your windows desktop and the remote server as following example:

{
            "name": "Remote",
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "remotePath": "/root/go/src/test/test-remote-debug",
            "port": 2345,
            "host": "10.142.114.151",
            "program": "C:/Users/EricS/go/src/test/test-remote-debug",
            "env": {}
}

@jhendrixMSFT
Copy link
Member

The above answer is correct so I'm going to close this issue. Please ping if you have further questions.

@philippgille
Copy link

@jhendrixMSFT: I think the question by @wcsiu wasn't answered yet:

I have some questions of myself too.
For go modules, only having one path for "program" is not enough, my dlv giving something like ${workspaceFolder}/go/pkg/mod/some-files when it tried to match the file on my side. Is there any plan to solve it? like "program": "${workspaceFolder}":$GOPATH or using $GOPATH to look for dependency codes when go modules is used.

(From #2242 (comment))

@jhendrixMSFT
Copy link
Member

Proper support for modules is still a work-in-progress. @wcsiu have you tried the Go language server? Please see https://github.com/microsoft/vscode-go/wiki/Go-modules-support-in-Visual-Studio-Code for info on how to enable it. If there are specific debugger issues WRT modules I suggest opening a new issue (or issues) so we can track each bug.

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 3, 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

6 participants