3

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?

ik1ne
  • 1,041
  • 15
  • 20

3 Answers3

3

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.

SBhojani
  • 499
  • 1
  • 4
  • 19
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

To use interactive console you can include

"console": "externalTerminal"

in launch.json, instead of

"externalConsole": true

since debugger may not allow "externalConsole" (as in my case)

Ignatij
  • 36
  • 2
0

I solved this using this workaround - https://github.com/golang/vscode-go/issues/124#issuecomment-999064812

asilverman
  • 38
  • 9
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 21 '21 at 23:12
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30645780) – tomerpacific Dec 23 '21 at 06:22