Search code examples
govisual-studio-codevscode-debuggerdelve

How can I simulate interactive console in VSCode debugger?


I am trying to debug this Go program which reads text and outputs it to console via VSCode debugger.

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    text, _ := reader.ReadString('\n')
    fmt.Println(text)
}

It runs fine on terminal, but when I debug it with VSCode I can't type anything even if I focus the debug output.

There's a console in the debug section but it's REPL evaluator so it also isn't related to terminal console.

How can I enable console in VSCode so I can type a text to the program?


Solution

  • This is followed by Microsoft/vscode-go issue 219, and still open.

    Yes - the VS Code debugger console doesn't currently support piping any input through to stdin.

    FYI, you can tell the Code debugger to use an external console in the launch config:

    "externalConsole": true
    

    It has a possible workaround, using remote debug + vscode task, but that is not trivial.

    tasks.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
                }
            }
        ]
    }
    

    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(command/control + shift + B), vscode will start a new shell and run the delve server.

    Press F5 to debug the .go file. It works fine for me.