how to use vscode to debug go 1.11.x - go

I am new to golang, but working on go 1.11.x.
My team use go module. The first time I clone the repository, I need to run GO111MODULE=on go mod download to download dependencies modules.
Then I need to run GO111MODULE=on go run main.go to run my app.
There is no one use vscode debugger, they prefer console log instead.
Is there any way to debug go 1.11.x using vscode?
Thanks.

I found the root cause right now. It is the source code of my team, not related to vscode or go 1.11.
My working launch.json is here
{
"version": "0.2.0",
"configurations": [
{
"name": "Go debug",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}/main.go",
"env": {
// "GO111MODULE": "on"
},
"args": [],
"showLog": true
}
]
}

Related

Cannot run deno debugger on vscode

I am a reccent deno user. Been using node for a long time, switched to deno and am very happy with it. It's really good
However, I have an issue.
Whenever I try to debug a deno file, the vscode debugger starts running for like half a second and then stops, and nothing happens.
It doesnt freeze or anything, it just starts for a moment and stops.
I am using this as launch configuration
{
"version": "0.2.0",
"configurations": [
{
"name": "Deno1",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"outputCapture": "std",
"runtimeArgs": ["run", "--inspect-brk", "-A", "${fileName}"],
"port": 9229,
}
]
}
I took it from this post
I should add that I was able to debug this file already, but one day it just started showing this issue i just described without (to my knowledge) any change on my part.
I am trying to debug this file
How can I fix this issue?
To make it work you need to add a "program" field to launch.json and move the path of the file there, which is briefly mentioned in this answer from the post you linked to. But also you need to change "port" to "attachSimplePort":
{
"version": "0.2.0",
"configurations": [
{
"request": "launch",
"name": "Launch Program",
"type": "node",
"program": "${workspaceFolder}/tests/grammar.test.ts",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": [
"run",
"--inspect-brk",
"--allow-all"
],
"attachSimplePort": 9229,
"outputCapture": "std",
}
]
}
To debug another part of the application just change the path in program, for example to an entrypoint file like main.ts. With --inspect-brk the debugger will first break at the first line of the program and then you can for example continue to a breakpoint with F5 or the continue button in the debugger panel.
(Deno v1.14)

VSCode debugging C++ launches external terminal, but doesn't run program

I've set up a launch.json file such that a C++ program uses an external console (so that it can receive user inputs), but when launching, VSCode simply opens a terminal window without running the program in it. If "externalConsole": true, is set to false, the program runs and can be debugged fine, just can't take inputs.
Note: No task.json file is used, CMake is used to create the executable binary.
Launch file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/bin/program_bin",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build/bin",
"environment": [],
"MIMode": "lldb",
"externalConsole": true,
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true,
}
]
}
]
}
Is it possible that VSCode doesn't have 'permission' to run an external terminal? Using on MacOS.
I have the same issue. This may not be the working answer, but hopefully it will get us farther down the correct path.
I did some digging and found the following:
VS Code Documentation mentioned that it opens an external console via lldb-mi.
A search for lldb-mi led to this post on the Apple Developer forums.
...which leads to an open source Github Repo with a build of lldb-mi
I need to read through the documentation for that build first, and then I'll give it a shot. I'll post results if it solves the issue.
I've tried to use standard (recomended ways of debugging from vscode documentation) and ran into the same issues with external terminal.
I'm also using mac and switching to CodeLLDB plugin to use LLDB for debugging helped
https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb
just follow documentation: https://github.com/vadimcn/vscode-lldb/blob/v1.8.1/MANUAL.md
But as a hint here is my working setup:
(I couldn't though make it work on windows, but I'm running windows via Parallel II, so maybe natively this plugin will work too)
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/g++",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
"configurations": [
{
"name": "(lldb) Launch",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
// "stdio": ["input.txt", null, null], // https://github.com/vadimcn/vscode-lldb/blob/v1.8.1/MANUAL.md#stdio-redirection
"cwd": "${fileDirname}",
"preLaunchTask": "C/C++: clang build active file" // this have to be the same as "label" in tasks.json
}
],
"version": "2.0.0"
}
just make sure you have all tools available, check by running in cli:
/usr/bin/clang --version
# and
lldb --version
good luck

VS Code debug golang. Delve debuger

I have a problem with the Delve debuger in visual studio code. Debugging begins but nothing happens. There is only information in the console:
time="2018-06-23T16:35:55+02:00" level=info msg="launching process with args: [C:\\Users\\LenovoPC\\go\\src\\test\\debug]" layer=debugger
Configuration of launch.json
"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
}
]
Based on how you install delve it will either end up in your PATH or
GOPATH/bin. If dlv binary is in your GOPATH/bin and this GOPATH is not
set as an environment variable, then make sure your PATH points to
this GOPATH/bin so that the Go extension can find the dlv binary.
Try to change the directory to filename to run the project from main.go of your project folder as
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
}
]
}
Make sure $GOPATH is set (e.g. as ~/.go)
On terminal run the command
go env
to check for GOROOT and GOPATH variables to see if delve is synchronized with them
For more information Checkout How to configure Delve

Is it possible to debug go revel framework from Visual Studio Code?

I'm trying to debug a revel app with visual studio but I can't get it to work.
I've seen this question how to debug revel framework(golang) application in visual studio code(vscode) but no answers yet...
I've tried with this config:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "~/code/go/bin/revel",
"env": {},
"args": [],
"showLog": true
}
]
}
But I'm getting this error:
Failed to continue: "The program attribute must point to valid directory, .go file or executable."
I think it must be the rebel binary the one to be run here, but I don't know how to pass the app path, should it go in "args"?
Yes it's possible.
Suppose that the GOPATH is C:\Work\golang
Revel project name is myapp, thus the location of the project (workspace) will be C:\Work\golang\src\myapp.
Make some changes to the controllers etc...
Run the application with revel run myapp, then press CTRL+C to exit. This step is necessary to generate corresponding go files. The generated file, i.e. the main package will be available under ${workspaceRoot}/app/tmp/main.go
Configure launch.json as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"env": {},
"showLog": true,
"program": "${workspaceRoot}/app/tmp/",
"args": ["-importPath", "myapp", "-srcPath", "c:\\work\\golang\\src", "-runMode", "dev"]
}
]
}
The important parts are program and args parameters, while the other parameters are unmodified.
Set breakpoint and start the delve debugger...
EDIT:
Setting args parameter to ["-importPath", "myapp", "-srcPath", "${workspaceRoot}/..", "-runMode", "dev"] also work, and I think this should work in other platforms (Mac, Linux) too.
The error message is related to delve issue. See https://github.com/Microsoft/vscode-go/issues/986

How to use Delve debugger in Visual Studio Code

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?
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:
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
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
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
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
}
]
},
}
]
}
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) :
First follow how to install delve on here : https://github.com/go-delve/delve/tree/master/Documentation/installation
Open your vscode, go to your project directory, then on vscode terminal : go mod init example.com/m/v1
run : go get github.com/go-delve/delve/cmd/dlv
Open mac terminal and run : open ~/.zshrc (if not exist, run : touch ~/.zshrc)
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
Then copy all script on .zshrc
Still on mac terminal, run : open ~/.bashrc (if not exist, run : touch ~/.bashrc)
Paste the script to .bashrc file
Then save .zshrc and .bashrc
close terminal & close vscode
Open vscode again
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": []
}
]
}
Then finish ! You can try again run your program and follow step2 using delve on accepted answer

Resources