How to debug application made by AWS SAM in VSCode? - debugging

Summary
I made Go application by AWS SAM. Now I try to debug this sample application in VSCode, but It fails so I want to know how to correctly debug it.
Tried
toggl-slack
├── Makefile
├── README.md
├── dlv
├── samconfig.toml
├── hello-world
│ ├── main.go
│ └── main_test.go
└── template.yaml
I put following command on console for debug.
cd toggl-slack
go get -u github.com/go-delve/delve/cmd/dlv
GOARCH=amd64 GOOS=linux go build -o ./dlv github.com/go-delve/delve/cmd/dlv
GOARCH=amd64 GOOS=linux go build -gcflags='-N -l' -o hello-world/hello-world ./hello-world
sam local start-api -d 5986 --debugger-path . --debug-args="-delveAPI=2"
curl http://127.0.0.1:3000/hello
As result, debug does not work and console shows error message.
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2019-12-28 21:23:17 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
Invoking hello-world (go1.x)
Fetching lambci/lambda:go1.x Docker container image......
Mounting /Users/jpskgc/toggl-slack/hello-world as /var/task:ro,delegated inside runtime container
Could not create config directory: mkdir /home/sbx_user1051: permission denied.API server listening at: [::]:5986
2019-12-28T12:23:46Z info layer=debugger launching process with args: [/var/task/hello-world]
2019-12-28T12:23:47Z warning layer=debugger reading debug_info: could not find abstract origin (0x13ed31) of inlined call at 0xfab50
Codes
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to Lambda container",
"type": "go",
"request": "launch",
"mode": "remote",
"remotePath": "",
"port": 5986,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": []
}
]
}
Other code is same as default sample application for AWS SAM.
Full Source Code is here:
https://github.com/jpskgc/toggl-slack/tree/0db02109685ce89f17ed64fdaadd5261e5f61512

The reading debug_info: could not find abstract origin line tells us that the compile flags got scrambled and the debug info was not actually included. What we need is:
GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" -o hello-world/hello-world ./hello-world
Second, VSCode should tell you this, but in launch.json, "request": "launch", is now "request": "attach", for mode remote giving:
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to SAM local",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "",
"port": 5986,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": []
}
]
}
With the above changes you should be able to attach to the debug server using VSCode (or GoLand/Vim/CLI).
Finally, the Could not create config directory: mkdir /home/sbx_user1051: permission denied. line is safe to ignore.
I am contributing this on behalf of my employer, Amazon. My contribution is licensed under the MIT license. See here for a more detailed explanation.

Related

how to debug golang cobra cli app in vscode

I have a golang cobra cli app. have configured my vscode for debugging. I want to debug a specific command using vscode for my application.
I am using this launch.json
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}"
}
If I select main.go and start debugging it just prints the help for my command. How can I debug a particular cli subcommand in vscode? Like say abc create or abc version
If I select a subcommand package and then debug it says : Failed to launch :could not launch process:not an executalbe file
You can configure the VSCode launch configuration to pass arguments using:
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}"
"args": ["arg1", ...]
}
Note that you may prefer to write the path to your main go file in the program field so that you can run/debug no matter what file you are currently on.
Reference:
launch.json: https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
VSCode Variables: https://code.visualstudio.com/docs/editor/variables-reference

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 debug Go tests with param "./..." in VS Code

I usually run go test ./... to run all tests in my project.
How can I set up launch.json to debug every tests that normally go test ./... runs?
Assuming you're using the Go extension for VSCode:
As the vscode-go's documentation says, you could use the following:
{
"name": "Launch test package",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}"
}
Note that you're specifying a new mode called "test".
You have to install delve in order to debug code using VSCode. You can install it by yourself or use the Go: Install/Update Tools command from VSCode. Read the documentation I mention first for more information.
This one should work
{
"name": "Launch test package",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/controllers"
}

Remote Debugging Go REST Endpoints

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.

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