Why does `go test` not work outside go module folder?

61 views Asked by At

I have then follwing repo structure:

.git

mysrc/
  |- go.mod
  |- a/some.go
   ...

in my repo. When I am inside the repo and execute go test mysrc/a , it does not work. as go tells me it cannot find the go.mod file.

go: cannot find main module, but found .git/config in /home/nixos/Desktop/Repos/myrepo
        to create a module there, run:
        go mod init

But the normal cd mysrc && go test a works fine.

How can I execute go test inside the root folder.

PS: I need this to make the debug adapter nvim-dap configurations work, which starts dlv inside the root repo which build with go build -o __debug_bin2596382558 -gcflags all=-N -l ./mysrc/a

1

There are 1 answers

0
Gabriel On

The solution is to use the following dap configuration for go in nvim with mfussenegger/nvim-dap

dap.configurations.go = {
  -- This are the requests documented here:
  -- https://github.com/go-delve/delve/blob/master/Documentation/api/dap/README.md
  -- works with go.mod packages and sub packages
  {
    type = "delve",
    name = "[githooks] Debug Test (go.mod)",
    request = "launch",
    mode = "test",
    program = "${fileDirname}",

    -- Because we are in a subdirevtory, this is needed.
    dlvCwd = "mysrc",
  },
}