leoluz / nvim-dap-go

An extension for nvim-dap providing configurations for launching go debugger (delve) and debugging individual tests
MIT License
436 stars 74 forks source link

Build Error: cannot find package #72

Open 9albi opened 4 months ago

9albi commented 4 months ago

Hello, I'm facing an issue getting the debugger to start.

Test Case

consider the following simple go project:

~/workspace/gotest/
├─ pkg/
│  ├─ file.go
├─ main.go
├─ go.mod
// file.go

package pkg

import "fmt"

func Foo() {
    fmt.Println("foo")
}
// main.go

package main

import "testmodule/pkg"

func main() {
    pkg.Foo()
}

When trying to debug main

image

The following error occurs:

Build Error: go build -o /home/bacteria/__debug_bin3602612132 -gcflags all=-N -l /home/bacteria/workspace/gotest/main.go
workspace/gotest/main.go:3:8: cannot find package "testmodule/pkg" in any of:
    /usr/local/go/src/testmodule/pkg (from $GOROOT)
    /home/bacteria/go/src/testmodule/pkg (from $GOPATH) (exit status 1)

The only workaround I've found is to move the project to $GOPATH/src.

~ echo $GOPATH
  /home/bacteria/go

~ go env GO111MODULE
  auto

I've tried using dlv manually and in vscode. Both seem to work fine. I've seen a similar Issue but sadly nothing there helped. I hope I could find a solution here.

Thank you for your time.

leoluz commented 4 months ago

It seems that you have an issue with how you are declaring your go module. Make sure the name of your module match your import statement.