go: cmd/go: runtime/debug build information not populated for test binaries when package name is not 'main'

What did you do?

https://play.golang.org/p/0ITNcl4kXGN

$ gotip version
go version devel +8c5de667 Fri Aug 30 08:28:40 2019 +0000 linux/amd64

foo$ gotip mod init foo
go: creating new go.mod: module foo

foo$ cat > foo_test.go
package foo_test

import (
        "runtime/debug"
        "testing"
)

func TestBuildInfo(t *testing.T) {
        info, ok := debug.ReadBuildInfo()
        if !ok {
                t.Fatal("no debug info")
        }
        t.Log(info.Main.Version)
}

foo$ go test

What did you expect to see?

=== RUN   TestBuildInfo
--- PASS: TestBuildInfo (0.00s)
    foo_test.go:13: (devel)
PASS

(https://play.golang.org/p/0ITNcl4kXGN)

What did you see instead?

--- FAIL: TestBuildInfo (0.00s)
    foo_test.go:11: no debug info
FAIL

CC @jayconrod @rsc; see also #33975.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 17
  • Comments: 17 (13 by maintainers)

Most upvoted comments

I also have found that when the package is named main the BuildInfo.Path and .Main fields are populated but BuildInfo.Deps field is nil even when the package under test depends on code from other modules.

Is there any idea on how this could be fixed?

Probably the condition here needs to be updated: https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/load/pkg.go;l=1965;drc=a6e5be0d30770e8bd1ba1e4bac2089218df121d9

I’m not sure exactly what to update it to, though.