VSCode & Delve dap: Can't get the vscode debugger to work when dlv is launched using 'dlv dap' - docker

I'm running VSCode 1.66.0 and I'm trying to attach the golang debugger on a running instance of a specific golang-docker-image. I'm able to do this when using 'dlv' but not when using 'dlv dap' and I can't understand why.
I'm following the instructions provided here:
https://vscode-debug-specs.github.io/go/#debugging-running-remote-process
Versions used:
VSCode ver. 1.66.0
dlv ver. 1.8.2
Docker Desktop ver. 4.4.4
My launch.json looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Remote Process",
"type": "go",
"request": "attach",
"mode": "remote",
"port": 2345,
"host": "127.0.0.1",
"showLog": true,
"apiVersion": 2,
"dlvLoadConfig": {
"followPointers": true,
"maxVariableRecurse": 1,
"maxStringLen": 200,
"maxArrayValues": 64,
"maxStructFields": -1
}
}
]
}
My dockerfile looks like so (as you can see the resulting image is based on the official image "mcr.microsoft.com/vscode/devcontainers/go:0-1.18"):
# syntax=docker/dockerfile:experimental
FROM golang:1.18 as builder
[...]
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$ARCH go build -gcflags "all=-N -l" -v -o main .
FROM mcr.microsoft.com/vscode/devcontainers/go:0-1.18
WORKDIR /app
CMD ["./main"]
The command I use to launch the 'dlv' process inside the running docker instance is this:
dlv dap --listen=:2345 --log=true --api-version=2 attach 1 ./app/main
The VSCode UI allegedly allows me to attach via:
> Debug: Select and Start Debugging -> Attach to Remote Process
However the debugger doesn't really get activated in the sense that the breakpoints are not honored and the 'dlv' command shown above doesn't print anything in the output.
If I remove the 'dap' part from the 'dlv' command:
dlv --listen=:2345 --log=true --headless=true --log-output=debugger,debuglineerr,gdbwire,lldbout,rpc --api-version=2 --accept-multiclient --headless attach 1 ./app/main
then the debugger attaches perfectly and works just fine (this is the legacy mode of the dlv debugger if I understand correctly). What gives? Am I missing something?

Related

Identify multiple VSCode devcontainers in the same remote docker context

All my team members use the same server as docker remote context. I have set up a project using VSCode-Devcontainer with a devcontainer.json like this:
{
"name": "MyProject - DevContainer",
"dockerFile": "../Dockerfile",
"context": "..",
"workspaceMount": "source=vsc-myprojekt-${localEnv:USERNAME},target=/workspace,type=volume",
"workspaceFolder": "/workspace",
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
],
"postCreateCommand": "/opt/entrypoint.sh",
"mounts": [
"source=/media/Pool/,target=/Pool,type=bind",
"source=cache,target=/cache,type=volume"
]
}
This worked fine for me, but now as my colleges start their devcontainers, we have the problem, that a newly started devcontainer kill other already running devcontainers.
We found that the local folder of the projekt seems to by the way to identify already running devcontainers:
[3216 ms] Start: Run: docker ps -q -a --filter label=devcontainer.local_folder=d:\develop\myproject
[3839 ms] Start: Run: docker inspect --type container 8ca7d3a44662
[4469 ms] Start: Removing Existing Container
As we all use the same path this identification based on the local folder is problematic. Is there a way to use other labels?
Seems to be a bug, because the issue I opened, was accepted as a bug report.

How to debug golang with dlv in remote container with vscode

recently I need to do debug for a single file go binary application which is contained in the docker under k8s environment with its source code. When I package the docker I use the
/dlv --listen=:40000 --headless=true --api-version=2 exec /singleExeFile
and expose the 40000 port to the outer VM like
ports:
- 40000:40000
When I use my dev environment to connect to outer vm with dlv command. It seems that it can be connected. Like the following
foo#foo-vm:~$ dlv connect 110.123.123.123:40000
Type 'help' for list of commands.
(dlv)
But when use vscode to attach to the code, it meets two error(The vscode has installed go extension)
When use legacy connect, there is my launch.json
{
// 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": "Connect to server",
"type": "go",
"debugAdapter": "legacy",
"request": "attach",
"mode": "remote",
"port": 40000,
"host": "110.123.123.123",
"substitutePath": [
{
"from": "${workspaceFolder}/cmd/maine.go",
"to": "/singleExeFile"
}
]
}
]
}
But the vscode raises error and I haven't found similar error in google. Error: Socket connection to remote was closed
Use the dlv-dap method to connect
{
// 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": "Delve into Docker",
"type": "go",
"debugAdapter": "dlv-dap",
"request": "attach",
"mode": "remote",
"port": 40000,
"host": "110.123.123.123",
"substitutePath": [
{
"from": "${workspaceFolder}/cmd/maine.go",
"to": "/singleExeFile"
}
]
}
]
}
And when try to connect, there is no error raised by vscode. Just try to connect and stop by vscode. Even don't know what's the error.
With verbose param, there still isn't any output in the DEBUG console for dlv method. But for legacy method, the following error is outputted. Please check.
By the way, add verbose in legacy method and it raises some detailed message in DEBUG CONSOLE.
AttachRequest
Start remote debugging: connecting 110.123.123.123:40000
To client: {"seq":0,"type":"event","event":"initialized"}
InitializeEvent
To client: {"seq":0,"type":"response","request_seq":2,"command":"attach","success":true}
From client: configurationDone(undefined)
ConfigurationDoneRequest
Socket connection to remote was closed
To client: {"seq":16,"type":"response","request_seq":2,"command":"attach","success":false,"message":"Failed to continue: Check the debug console for details.","body":{"error":{"id":3000,"format":"Failed to continue: Check the debug console for details.","showUser":true}}}
Sending TerminatedEvent as delve is closed
To client: {"seq":0,"type":"event","event":"terminated"}
From client: disconnect({"restart":false})
DisconnectRequest
New Update in 9 July
I made another try to create a simple docker using the following dockerfile
FROM golang:1.16.15
RUN mkdir -p /var/lib/www && mkdir -p /var/lib/temp
WORKDIR /var/lib/temp
COPY . ./
RUN go env -w GOPROXY="https://goproxy.cn,direct"
RUN go install github.com/go-delve/delve/cmd/dlv#latest
RUN go mod tidy
RUN go build
RUN mv ./webproj /var/lib/www/ && rm -rf /var/lib/temp
WORKDIR /var/lib/www
COPY ./build.sh ./
EXPOSE 8080
EXPOSE 2345
RUN chmod 777 ./webproj
RUN chmod 777 ./build.sh
ENTRYPOINT ["/bin/bash","./build.sh"]
And the build.sh code is like
dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./webproj
After that, it works with GoLand debug. GoLand can debug when I send the GET api with designed. But it still can't be work with VSCode. When I use the vscode, it did connect to the docker. But when I add the break point. It shows that this is an unverified BreakPoint and can't stop.
Here is my launch.json
{
// 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": "Connect to server",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "${fileDirname}",
"port": 2345,
"host": "127.0.0.1"
}
]
}
So currently this is blocked. Help is very needed. Thanks.

How do I configure my VS Code Rails debugger to launch when Rails is started from a virtual machine running locally?

I’m using VS Code 1.60.2 for Mac Big Sur. I have my Rails 4 instance running in a virtual machine (vagrant container) and I would like to configure the VS Code debugger. From my local terminal, I could start the server from a shell using the following script …
#!/bin/sh
ssh -t myvirtual.dev "exec bash --login -c 'foreman start -f Procfile.debug'"
So in my .vscode/launch.json file, I configured this
{
"configurations": [
{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"program": "/opt/scripts/start_web.sh",
"args": [
]
},
However, when I restart vs code, click the “Run/Debug” icon from the left, and click the “Run” icon next to “Rails server”, I get this error in the terminal …
Uncaught exception: /opt/scripts/start_web.sh:3: syntax error, unexpected tIDENTIFIER, expecting do or '{' or '('
ssh -t myvirtual.dev "exec bash --login -c 'f...
My virtual container has port 3000 exposed, but not sure what else to do to get the debugger configured properly.

Getting the empty response from the server even when the container is port mapped in docker

I am trying to start my application using the http-server through npm start. It is working fine in the localhost but when i deploy the same application through docker container (port-mapped) , it doesn't appear in my local browser.
I have a directory inside my docker container(Ubuntu) which is runned through the command
docker run -it -p 8001:5500 ubuntu.
I have installed everything inside my container - npm, vim editor ,etc.
My package.json file is
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "http-server -a localhost -p 5500"
},
"author": "",
"license": "ISC",
"dependencies": {
"concurrently": "^5.0.2",
"http-server": "^0.12.1"
}
}
After that i have done npm install and then executed the command npm start.
The output of the command is :
But when i am trying to open my application in chrome browser it is throwing me an empty response.
Also when i am running the same application through my localhost it is accessible.
Why i am not able to access the server through the port mapping of docker container.??
localhost inside container won't be accessible from outside the container. Run your server on 0.0.0.0 inside the container.

Debug Pytest in docker container using VS Code

I have troubles to setup debugging of py.test code in docker container using VS Code.
After studying this: https://code.visualstudio.com/docs/python/debugging
And this: How to remote debug python code in a Docker Container with VS Code
I have setup following debug configuration in vscode:
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/capi",
"port": 3000,
"secret": "secret_text",
"host": "localhost"
}
I have imported this bit into my test file:
import ptvsd
ptvsd.enable_attach("secret_text", address = ('0.0.0.0', 3000))
ptvsd.wait_for_attach()
And I made sure I open that 3000 port in docker-compose file:
ports:
- 3000:3000
I double checked that the port is open:
nmap -p 3000 localhost
Starting Nmap 7.60 ( https://nmap.org ) at 2018-07-19 10:53 CEST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000074s latency).
PORT STATE SERVICE
3000/tcp open ppp
Nmap done: 1 IP address (1 host up) scanned in 0.11 seconds
It seems to be the case. When I run pytest file from the container it starts and waits for debugger to be connected:
===================================================== test session starts =====================================================
platform linux2 -- Python 2.7.15, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /capi, inifile:
plugins: requests-mock-1.5.0, xdist-1.14, metadata-1.7.0, html-1.16.1, cov-2.5.1
collecting 0 items
But when I run this configuration from VS Code nothing seems to happen.
It seems to hang. Nothing in the debug console or in the docker container.
I have setup remote debug for a simple hello-world.py console app just for testing and it seems to work. So my assumption is it has something to do with the fact that I'm trying to debug a pytest.
Have anyone managed to do this? I would appreciate some help.
I encountered the same issue and your post almost solves the problem.
When I tried implementing your solution I encountered the following issue:
ImportError while loading conftest '/app/tests/conftest.py'.
tests/conftest.py:36: in <module>
ptvsd.enable_attach("secret_text", address=("0.0.0.0", 5678))
E TypeError: enable_attach() got multiple values for argument 'address'
Removing the "secret_text" value allowed me to hit the wait_for_attach() point and successfully attach the debugger to the code. I was able to hit breakpoints in my tests. Thank you!
.vscode/launch.json
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}/path/to/code",
"remoteRoot": "/app",
"port": 5678,
"host": "localhost"
}
docker-compose.yml
ports:
- "5678:5678"
conftest.py
import ptvsd
ptvsd.enable_attach(address=("0.0.0.0", 5678))
ptvsd.wait_for_attach()
Note: The ptvsd lines are placed after all the imports.
CLI command to execute tests:
import subprocess
import click
#click.command()
def cli():
return subprocess.call("pytest test", shell=True)
Sequence to debug tests:
docker-compose up (get container running)
docker-compose exec MODULE CONTAINER_NAME FUNCTION_THAT_EXECUTES_TEST
Attach debugger in VSCode
Tests will execute and hit whatever breakpoint you have setup.
I came across this question before discovering that Visual Studio Code has had built-in-support for remote debugging docker containers since 2019!
A quick guide I wrote to getting started
The official documentation

Resources