Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: cmd/go: support cgo in test files #18647

Closed
minux opened this issue Jan 13, 2017 · 5 comments
Closed

proposal: cmd/go: support cgo in test files #18647

minux opened this issue Jan 13, 2017 · 5 comments

Comments

@minux
Copy link
Member

minux commented Jan 13, 2017

Currently it's impossible to use cgo in x_test.go files.

Given that we support coverage tests on cgo using packages,
there really is no reason that we can't support using cgo in
test files (and besides, we don't need to rewrite any x_test.go
files, so I don't know why we have such a limitation in the first
place. perhaps it's because we doesn't has a TestCgoFiles
field in go/build.Package?)

The main problem of not supporting cgo in test file is:
because there is no test build tag, there is no way to
export a cgo function for test that is not also included in the
package (true, deadcode elimination might remove those
functions, but what about C helpers needed by such function?
our linker can't do deadcode elimination on C functions).

@minux minux added the Proposal label Jan 13, 2017
@bradfitz bradfitz added this to the Proposal milestone Jan 13, 2017
@rsc rsc changed the title Proposal: cmd/go: support cgo in test files proposal: cmd/go: support cgo in test files Jan 23, 2017
@rsc
Copy link
Contributor

rsc commented Jan 23, 2017

Allowing cgo in tests adds complication. Why is it important to do this?

@bmatsuo
Copy link

bmatsuo commented Feb 13, 2017

From the outside, I was surprised when I learned that cgo cannot appear in tests and thought it felt arbitrary. I still don't even really understand why, I just work around it.

I do think it can be rather annoying as well. If I define a Go function which takes a C.size_t argument, I can't write a table test for it very simply because I can't define a struct that contains C.size_t within my test file.

It would be great if test files could call C functions, but I really be glad if I were just able to reference C types.

@rsc
Copy link
Contributor

rsc commented Feb 13, 2017

@minux, do you know how many lines would need to change in the go command for this? If it's a small change then maybe it's OK to do.

@rsc
Copy link
Contributor

rsc commented Mar 13, 2017

I understand that this is handy, but I started to look into it and got stuck pretty quickly. The info about the package is https://golang.org/pkg/go/build/#Package and you'd need to add TestCgoFiles and maybe XTestCgoFiles, but then what about CgoCFLAGS? Does that collect all of them? What about CFiles, HFiles, etc?

It seems like a lot of trouble when there's a reasonable workaround for this not-too-common case already.

@rsc
Copy link
Contributor

rsc commented Mar 27, 2017

Closing since it is very difficult and doesn't seem worth it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants