3

When I am trying to debug this Go application using VS Code and dlv, I get the following error:

Failed to launch: could not launch process: fork/exec C:\Users\MyUser\Workspaces\goworkspace\github.com...__debug_bin.exe:%1 is not a valid Win32 application.

But when I comment out the kafka code (please refer main.go) and then try to debug, the debugger works and the breakpoint is hit.

Things that I already tried:

  • Running the application in other machine, which worked!

  • Reinstalling Go on Windows.

  • Moving the application to a different directory.

  • Reinstalling and updating VSCode and the related Golang extension/tools.

  • Checked the environment variables but did not find anything suspicious.

  • Blocked the Windows Defender, but that did not work either.

  • Running the 'Windows Memory Diagnostics' tool.

  • Reinstalling tdm-gcc (used by kafka)

  • Using Jetbrains Goland, but getting a similar error

could not launch process: fork/exec C:\Users\MyUser\AppData\Local\Temp\GoLand___go_build_main_go.exe: %1 is not a valid Win32 application.

  • Using the 'dlv debug' command from Windows terminal, get similar error again

could not launch process: fork/exec C:\Users\MyUser\Workspaces\goworkspace\github.com\org\app-name\cmd__debug_bin: %1 is not a valid Win32 application.

Version Details :

  • Windows 10 Pro 20H2
  • GoLand 2021.2.3
  • go version go1.17.1 windows/amd64
  • Delve Debugger Delve Debugger
  • VS Code Version 1.60.2

Please refer the code below,

main.go

func main() {
log.Println("Starting api server")

cfg, err := config.LoadConfig()
if err != nil {
    log.Fatalf("LoadConfig: %v", err)
}

//Kakfa Code Begin
c, err := kafka.NewKafkaConsumer(cfg)
if err != nil {
    log.Fatalf("Kafka Consumer Creation: %v", err)
}
kafka.ListenToTopics(cfg, c, []string{"topic-name"})
//Kafka Code End

}

kakfa.go

package kafka

import (
    "encoding/binary"
    "fmt"

    "github.com/my-user/config"
    "gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
)

func NewKafkaConsumer(cfg *config.Config) (*kafka.Consumer, error) {

    config := cfg.Kafka

    return kafka.NewConsumer(&kafka.ConfigMap{
        "bootstrap.servers": config.BootstrapServers,
        "security.protocol": config.SecurityProtocol,
        "sasl.mechanisms":   config.SASLMechanisms,
        "sasl.username":     config.SASLUsername,
        "sasl.password":     config.SASLPassword,
        "group.id":          config.GroupID,
        "auto.offset.reset": config.AutoOffsetReset,
    })
}

func ListenToTopics(cfg *config.Config, c *kafka.Consumer, topics []string) {
    c.SubscribeTopics(topics, nil)

    for {
        msg, err := c.ReadMessage(-1)
        defer c.Close()
        if err == nil {
            schemaID := binary.BigEndian.Uint32(msg.Value[1:5])
            schema, err := getLatestAVROSchemaById(cfg, int(schemaID))
            if err != nil {
                panic(fmt.Sprintf("Error getting the schema with id '%d' %s", schemaID, err))
            }
            native, _, _ := schema.Codec.NativeFromBinary(msg.Value[5:])
            value, _ := schema.Codec.TextualFromNative(nil, native)
            fmt.Printf("Here is the message %s\n", string(value))
        } else {
            fmt.Printf("Error consuming the message: %v (%v)\n", err, msg)
        }
    }
}

go env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\MyUser\AppData\Local\go-build
set GOENV=C:\Users\MyUser\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\MyUser\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\MyUser\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.1
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\MyUser\AppData\Local\Temp\go-build3863829792=/tmp/go-build -gno-record-gcc-switches

Please let me know if I should post this question to some specific forum/community.

SDB
  • 61
  • 1
  • 4
  • Are you on Windows with x86 or x64 architecture? – s0xzwasd Oct 05 '21 at 13:57
  • @s0xzwasd I am on Windows x64 – SDB Oct 05 '21 at 14:29
  • What is your `go env` output? – s0xzwasd Oct 05 '21 at 14:37
  • @s0xzwasd Updated my question with go env output. Please check the question. – SDB Oct 05 '21 at 14:47
  • What `wmic OS get OSArchitecture` in the terminal returns? – s0xzwasd Oct 06 '21 at 11:26
  • @s0xzwasd OSArchitecture 64-bit – SDB Oct 06 '21 at 11:44
  • I have two envs, where setup is exactly the same (gcc, vscode, go, dlv) however one has similar issue as you do and I am banging my head against wall to understand why. Currently I have tracked it down that this seems to be related how delve builds your solution before debugging when using CGO. If you do `go build -gcflags="all=-N -l"` the solution works fine (gcc/cgo is ok). And then as a workaround you can do `("mode": "exec", "program": "program.exe")` in the launch.json. However its not clear to me yet what is causing the dlv to behave like this when building for debugging from vscode. – meistars Dec 03 '21 at 14:43

0 Answers0