Jeffrey Yang
Jeffrey Yang

Reputation: 61

Delve is not working during debugging golang in vs code

I am learning golang and try to debug a sample code which comes from one of my golang book. It is very strange that delve debugger is not working as expected.

enter image description here

As you can see in this picture, I can set break point at line 83 and continue to run program to this break point. On the left panel, I can see the variables and call stack are displayed. But when I step into next statement from that break point, the debugger seems to be stopped. All the variables are cleared out but call stack still showing me the problem is running like picture below:

enter image description here

I also tried delve debugger in command line (out side vs-code), I got the same issue on the same break point.

VS-Code launch.json like below:

{
    // 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": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {},
            "args": [],
            "trace": true
        }
    ]
}

Go version: 1.10.3 windows/amd64

Delve debugger version: 1.1.0

VS-Code version: 1.29.0

Do I need other configuration?

I attach source code here, you can give a try and see what happen on your machine.

Source Code:

package main

import (
    "bufio"
    "fmt"
    "io"
    "log"
    "os"
    "path/filepath"
    "regexp"
)

func filenamesFromCmdLine() (inFilename, outFilename string, err error) {
    if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
        err = fmt.Errorf("Usage: %s [<]infile.txt [>]outfile.txt", filepath.Base(os.Args[0]))
        return "", "", err
    }

    if len(os.Args) > 1 {
        inFilename = os.Args[1]
    }
    if len(os.Args) > 2 {
        outFilename = os.Args[2]
    }
    if inFilename != "" && inFilename == outFilename {
        log.Fatal("won't overwrite the infile")
    }

    return inFilename, outFilename, nil
}

func americanise(inFile io.Reader, outFile io.Writer) (err error) {

    reader := bufio.NewReader(inFile)
    writer := bufio.NewWriter(outFile)
    defer func() {
        if err == nil {
            err = writer.Flush()
        }
    }()

    var replacer func(string) string

    wordRx := regexp.MustCompile("[A-Za-z]+")
    eof := false
    for !eof {
        var line string
        line, err = reader.ReadString('\n')
        if err == io.EOF {
            err = nil
            eof = true
        } else if err != nil {
            return err
        }
        line = wordRx.ReplaceAllStringFunc(line, replacer)
        if _, err = writer.WriteString(line); err != nil {
            return err
        }
    }

    return nil
}

func main() {
    inFilename, outFilename, err := filenamesFromCmdLine()
    if err != nil {
        fmt.Println(err)
        os.Exit(0)
    }

    //why we initialize them to stdin and stdout?
    //the file object has been set to os.Stdin and os.Stdout
    inFile, outFile := os.Stdin, os.Stdout

    if inFilename != "" {
        //os.Open() returns *os.File that can be used for reading the file.
        if inFile, err = os.Open(inFilename); err != nil {
            log.Fatal(err)
        }
        //defer inFile.Close()
    }

    if outFilename != "" {
        //create a new file or truncate to zero length for existing file
        if outFile, err = os.Create(outFilename); err != nil {
            log.Fatal(err)
        }
        defer outFile.Close()
    }

    if err = americanise(inFile, outFile); err != nil {
        log.Fatal(err)
    }
}

Upvotes: 1

Views: 4791

Answers (2)

viggy
viggy

Reputation: 690

Unfortunately Delve in VS-Code does not handle "GoTo Next Step" well when you are executing multiple go routines with each waiting for going to Next Step. One workaround I use is that I put another debug point where I want the debugging to stop and then select "Continue" option.

Upvotes: 4

Magiq
Magiq

Reputation: 116

I'm not sure about your situation but there was a bug in vscode-go previous versions. Does your version 0.7.0 ?

Upvotes: 0

Related Questions