This is similar to vscode-go issue 219
The workaround (proposed by KingRikkie) is:
There is a workaround however. I wrote a script that does the following:
- Compile your app without optimizations and inlining
- Start app in a new window
- Attach delve headlessly onto its process id.
I then created a new task in VScode that starts the script and specified said task under preLaunchTask in a remote debug configuration in launch.json.
In my case a powershell script resting inside {workspaceRoot} compiling a package called 'main' in 'main' dir:
$EXECUTABLE_NAME="main"
$EXECUTABLE_PATH=".\main"
$GoPath=((go env | Select-String -Pattern "GOPATH=" | Out-String) -split "=")[1].TrimEnd()
$GoPath+="\bin"
Set-Location $EXECUTABLE_PATH
Start-Process go -ArgumentList 'build -gcflags "-N -l"' -Wait -NoNewWindow # compile without optimizations and inlining
Start-Process ".\$EXECUTABLE_NAME.exe"
$timeOut = 20
$started = $false
# wait for process to start
Do {
Start-Sleep -Milliseconds 250
$timeOut--
$Proc = Get-Process main -ErrorAction SilentlyContinue
If ($Proc) {
$started = $true
}
}
Until ($started -or $timeOut -eq 0)
If (!($started)) {
Write-Error 'Process did not start'
Exit
}
$ProcId=($Proc | Select-Object -expand Id)
Start-Process -FilePath "$GoPath\dlv.exe" -ArgumentList "attach $ProcId --headless --listen=:2345 --log" -WindowStyle Hidden
The task:
"label": "debug-attach",
"type": "shell",
"command": "powershell -ExecutionPolicy UnRestricted -File ${workspaceRoot}\\debug-attach.ps1",
"presentation": {
"reveal": "silent",
"panel": "shared",
"echo": false
}
The launch configuration:
"name": "Attach",
"type": "go",
"request": "launch",
"mode": "remote",
"remotePath": "${workspaceRoot}\\main",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}\\main",
"preLaunchTask": "debug-attach",
"env": {},
"args": [],
"showLog": true
When I hit F5 now my app will pop up and debugging will automatically start, delve is hidden.
Since July 2018, HowieLiuX reported the following workaround in Dec. 2018
using remote debug + vscode task:
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "cd ${fileDirname} && dlv debug --headless --listen=:2345 --log --api-version=2",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
And:
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": "launch",
"mode": "remote",
"remotePath": "${fileDirname}",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}
Run the task using shortcut key (shift + cmd + B in Mac OS): VSCode will start a new shell and run the delve server.
Pressing F5 to debug the .go file.