Remote Debugging Go REST Endpoints - go

I am new to Go and new to VSCode. I have also never used Delve before. I am trying to set up remote debugging but i can't seem to hit breakpoints.
My project hosts REST endpoints on localhost:8080. What should the launch.json file look like in order to have delve attach and listen so that I can put breakpoints on my REST endpoints? Currently, this is what i have:
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "remote",
"remotePath": "",
"port": 8080,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": [],
"showLog": true
}
Thanks in advance!

This article mentioned:
Unfortunately, you won’t be able to debug an application when running with buffalo dev. You’ll need to build an executable that skips compiler optimizations like function invocation inlining.
If you skip these build flags Gogland won’t stop on your break points.
More on that on "Debugging Go Code with GDB ".
The code generated by the gc compiler includes inlining of function invocations and registerization of variables. These optimizations can sometimes make debugging with gdb harder.
After that, you can follow "Remote Debugging"
To remote debug using VS Code, you must first run a headless Delve server on the target machine. For example:
$ dlv debug --headless --listen=:2345 --log
Then your launcher can apply.

Related

How to run single file from go integration tests that use suite in vscode?

I have integration tests in golang that I want to run in the vscode debugger. After some laying around I got this to mostly function using the launch.json configuration
"version": "0.2.0",
"configurations": [
{
"name": "debug entities test",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/targetingd/integration/entities_integration_test.go",
"buildFlags": "-tags=integration",
"env": {
"PULL_REGISTRY": "xxxx"
}
}
However I say this 'mostly' works because it's actually running the full integration test suite, not just entities_integration_test.go as I wanted. I'd like to configure this to only run one test file and not waste time on the rest.
I know if I was running this from the command line I could use the --run tag but I'm not sure what the equivalent is within the launch.json.
I don't know if it matters but were using both suite and dlv for these tests.

VSCode run all go tests in folder with launch configuration

I'm trying to make a launch configuration that will run all of the go tests I have in a specific folder in my repo.
I can successfully run go test ./src/... in the terminal to run all the tests I care about but I'm having trouble replicating that in a VSCode launch configuration.
Here's my current launch configuration:
{
"name": "run tests",
"type": "go",
"request": "launch",
"mode": "test",
"args": ["./src..."],
"program": "${workspaceFolder}",
}
It seems that using ./src/... as args doesn't behave as I expect it to. Using this launch configuration I get an error that:
no Go files in /home/paymahn/gadic/backend
exit status 1
Process exiting with code: 1
Is there a way to replicate go test ./src/... as a VSCode launch configuration?
When I try to run a simple test I use this setting, reference here.
{
"name": "Tests",
"type": "go",
"request": "launch",
"mode": "test",
"remotePath": "",
"program": "${fileDirname}",
"env": {},
"args": ["^TestGenerateConfigurationFailure$"]
}
And about the specific folder, I understand that the Golang test tool inspects the "test" module package. No, the specific regex name folder for the Golang test tool.
You can check more info in official debugging VS-Code for Golang HERE

How to configure Mocha in VSCode for debugging

I have a simple LinkedList implementation in Node. I want to test it using Mocha -- simply exercise different append/delete operations. And most importantly, I want to be able to stepthrough/debug my linkedlist as called from the mocha tests.
This is my launch.json:
{
"version": "0.1.0",
"configurations": [
{
"type": "node",
"request": "launch",
"protocol": "inspector",
"name": "Mocha All",
"windows":{
"runtimeExecutable": "c:\\Users\\alern\\AppData\\Roaming\\npm\\_mocha.cmd"
},
"args": [
"--colors",
"${workspaceFolder}\\test\\test.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
when I press Run on my "Mocha All" configuration in VSCode,
global installation of mocha.cmd starts running, I see the following in the terminal:
cd 'd:\Projects\Algorithms\LinkedList'; & 'c:\Users\alern\AppData\Roaming\npm\_mocha.cmd' '--inspect-brk=30840' '--colors' 'D:\Projects\Algorithms\LinkedList\test\test.js'
(node:21524) ExperimentalWarning: The ESM module loader is experimental.
The above makes sense.
And then I see:
all of my tests simply rip through --- although I have setup some breakpoints in the Mocha scripts, they get ignored. So thats useless.
once all the tests are done (some successful, some not), at the end I get a popup that says:
"Cannot connect to runtime process, timeout after 10000ms - reason: Cannot connect to target: connect ECONNREFUSED 127.0.0.1:30840" This error feels like my process is done running and gone away, and VSCode or debugger are trying to connect to it?
In any case, what do I tweak to have my breakpoints stop execution? Thank you
The first thing I might try is to change the type to "pwa-node". I think mocha or VSCode recently made some changes that make that mandatory. You might also not want to set the runtime executable, but rather the "program". Generally the runtimeExecutable will be node, and if it's on your path VSCode should be able to find it automatically.
If all else fails I'd also try looking at the default configuration that ships with VSCode and see if that gives you any clues. That can be accessed by adding a new configuration through the "Add Configuration..." button. https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configurations-for-common-scenarios

How to know the run command that vscode creates from launch.json?

I have a launch.json like below:
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": ["server"]
}
]
}
When I launch it using the debug view on VScode it works fine but I need to change the program attribute to relative path of the respective file from my workspace location, strange behaviour is observed, other components in the same package start throwing undefined func error. Probably something goes wrong with current working dir or go module setup.
In order to investigate further, I need to know the command that is generated from this launch.json file. It should be something like go run ...
I have checked the output and debug console, both of them shows nothing about the launch command.
If you know how to see launch command, please help.
You can find the generated command in Visual Studio Code's debug output. When you have started a debug session, open the Debug Console by clicking on the Debug Console icon in the View Bar or by pressing Ctrl + Shift + Y. The debug output will include the generated command line.

Cannot disable optimizations while compiling runtime

When I use VSCODE in my Ubuntu 16.10 to compile my go project, it can't succeed and prompt:
compile: cannot disable optimizations while compiling runtime
exit status 2
Process exiting with code: 1
I checked my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {
"GOPATH":"/home/bill/test/go",
"GOROOT":"/usr/local/go"
},
"args": [],
"showLog": true
}
]
}
What can I do to change this?
This error message seems to come from the Go compiler due to this change which fixes this bug
(the "compile: " prefix is apparently added by vscode).
My take on the reason is as follows:
You have Go runtime source code somehow modified since before you started to experience the present problem.
An attempt to build your program detects the runtime has changed
and needs to be rebuilt as well—simply because parts of it are included
into any program built with Go.
As to how to solve this, I have no clean idea.
Supposedly running
$ cd /usr/local/go/src
$ ./make.bash
should do it.
On a side note, you must not set the GOROOT env. variable
by hand—please leave it to the Go suite instead; since many versions ago,
it knows its GOROOT automatically based on where the go binary
is located.

Resources