Open bcmills opened 7 years ago
(CC: @alandonovan @ianlancetaylor)
The error use of cgo in test ... not supported
is not coming from go tool doc
, it's coming from the go/build package.
I did some preliminary investigation on this since I was curious. The commit that introduced the error message is from 2011, here:
https://go.googlesource.com/go/+/66bedf82210220e45ca2eb4919fe764e6a022630%5E%21/#F2
(the check now lives in src/go/build/build.go)
I tried removing the check. Go builds and all of the tests pass. Building an example with CGo still fails, however, due to being unable to import anything from the stdlib:
./cgo_test.go:10:2: can't find import: "fmt"
So it appears that there's more to this issue than just removing the check. Something about CGo confuses the importer when building tests.
So the root problem, which is implied by the build.Package documentation, seems to be that a file is expected to appear in exactly one of GoFiles, CgoFiles, TestGoFiles, XTestGoFiles, or IgnoredGoFiles.
Adding support for Cgo in tests appears to involve adding TestCgoFiles and XTestCgoFiles to build.Package, and then teaching src/cmd/go/internal/load/test.go about these fields. I'm going to take a stab at this.
While prototyping a library to work around #13656, I ran in to a pretty major issue with the godoc tool. It doesn't generate any documentation at all for packages whose tests use cgo, which implies that packages designed for use with cgo cannot contain idiomatic examples.
cslice/cslice_test.go: