go-delve / delve

Delve is a debugger for the Go programming language.
MIT License
22.37k stars 2.13k forks source link

Breakpoints do not work in packages with a TestMain(*testing.M) function #3735

Closed jnathanh closed 1 month ago

jnathanh commented 1 month ago
  1. What version of Delve are you using (dlv version)?
    • 1.22.1
  2. What version of Go are you using? (go version)?
    • go version go1.22.0 darwin/arm64
  3. What operating system and processor architecture are you using?
    • macOS 14.5, arm64
  4. What did you do?
    $ dlv test ./withtestmain -- -test.run ^TestDebug$
    #Type 'help' for list of commands.
    (dlv) b TestDebug:1
    #Breakpoint 1 set at 0x1021c23cc for github.com/jnathanh/delve-test-main/withtestmain_test.TestDebug() #./withtestmain/debug_test.go:13
    (dlv) c
  5. What did you expect to see?
    > github.com/jnathanh/delve-test-main/withouttestmain.TestDebug() ./withouttestmain/debug_test.go:9 (hits goroutine(21):1 total:1) (PC: 0x1001c5e6c)
     4:         "fmt"
     5:         "testing"
     6: )
     7:
     8: func TestDebug(t *testing.T) {
    =>   9:         fmt.Println("stopping on a breakpoint in this test works with delve")
    10: }
  6. What did you see instead?
    TestMain
    Process 52848 has exited with status 0

To repro:

Clone this repo and try and stop on a breakpoint in the TestDebug function in the withtestmain package. Setting the breakpoint succeeds, but I am unable to stop on it. You can do the same in the withouttestmain package without a problem. The only material difference is the presence of a TestMain function, with the *testing.M parameter.

I originally encountered this in vscode executing the "debug test" functionality and it took a few hours to narrow down the issue. Ideally the breakpoint would work, but if that is not possible, I would at least want delve to throw an error or output a warning to indicate the issue and give some indication of how I can work around it.

aarzilli commented 1 month ago

If you write your own TestMain function you are supposed to call m.Run() yourself, otherwise no tests will be run: https://beta.pkg.go.dev/testing#hdr-Main.