Vscode Go debug breakpoint doesn't work
Problem
When clicking debug the test above a go test func, the breakpoint doesn’t work.
Starting: ~/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:44285 --log-dest=3 from ./apis/core/v1
DAP server listening at: 127.0.0.1:44285
Type 'dlv help' for list of commands.
got kubeconfig: ~/.kube/kubeconfig.ly
PASS
Process 582447 has exited with status 0
Detaching
Solution
Run dlv directly
dlvtest() {
TEST_NAME="$1"
if [ -z "$TEST_NAME" ]; then
echo "Usage: dlv_test <TestFunctionName>"
return 1
fi
echo "π Searching for package containing $TEST_NAME..."
# 1. Find the file defining the test function (recursive grep)
# We search for "func TestName" to avoid finding calls to the function.
MATCHING_FILE=$(grep -r -l "func $TEST_NAME" . | head -n 1)
if [ -z "$MATCHING_FILE" ]; then
echo "β Error: Could not find definition for 'func $TEST_NAME' in current directory."
return 1
fi
# 2. Get the directory (package path)
PKG_DIR=$(dirname "$MATCHING_FILE")
echo "β
Found in: $PKG_DIR"
# 3. Run dlv test
# -gcflags="all=-N -l" is added implicitly by 'dlv test', ensuring breakpoints work.
echo "π Starting dlv debugger on :2345..."
dlv test "$PKG_DIR" \
--listen=:2345 \
--headless \
--api-version=2 \
-- \
-test.v \
-test.run "^$TEST_NAME$"
}
Vscode config add an entry
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "${workspaceFolder}",
"port": 2345,
"host": "127.0.0.1"
}
]
}
Test it works
Add breakpoint now, it should be working now.