'go vet' not finding methods exported in _test.go files?

329 views
Skip to first unread message

William Ivory

unread,
Aug 27, 2021, 4:11:07 AM8/27/21
to golan...@googlegroups.com
Hi,

We've recently moved from using 'go tool vet' to 'go vet' (we need to stick to the Go versions shipped with Debian) and have found that it is not finding public methods that we export for test only by putting them in _test.go files (error is 'undeclared name'). Disable go vet, and you can build and run the test code fine. Problematic code is something like this:

export_for_test.go:

package foo

func ExportedForTest() {
// Do something
}

---

foo_test.go

package foo_test

import (
"foo"
"testing"
)

func TestFoo(t *testing.T) {
foo.ExportedForTest()
}

---

We are using Go 1.15 in Debian 10.

This would appear to be a bug in 'go vet' given the actual build and running of test code works fine. Is there any way to work around this, and has it been fixed in more recent releases (not that this would solve our problem as we are stuck with the buster-backport version of Go for now, and Debian 11 is also using Go 1.15).

Thanks,

William


Sean Liao

unread,
Aug 27, 2021, 6:22:42 AM8/27/21
to golang-nuts
I can't reproduce the error you're seeing either in 1.15 or in later versions with the code you provided

wji...@gmail.com

unread,
Aug 31, 2021, 3:25:35 AM8/31/21
to golang-nuts
Hmm ... obviously not as simple a problem as I first thought. I'll need to start with the definitely failing code and reduce it to as simple a case as possible that still exhibits the problem. Had (wrongly) assumed it would happen in all cases hence the simple example given initially.

wji...@gmail.com

unread,
Aug 31, 2021, 5:31:27 AM8/31/21
to golang-nuts
Dug a bit further. The way our build works (debian build which copies files into a build area) meant that 'go vet' was running on the files in 2 different locations. All worked fine in the build area, but in the source area, it was generating the spurious 'undefined method' error. By filtering out the source area directories from the directories I ran go vet on, the problem is resolved.

William
Reply all
Reply to author
Forward
0 new messages