-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
cmd/cgo: if CGO_ENABLED=0, cgo files are silently ignored #16981
Comments
I initially thought this was a regression, because I never saw it happen before 1.7. Turns out it's more subtle than that: older versions of Go would still ignore
|
This behaviour has landed in Go 1.4. If cgo is not used, go build is ignoring the C files now. When CGO_ENABLED=1 without |
Did you mean 1.4? My test above shows different behaviors is 1.4 and 1.7. I understand this might have made the behavior more consistent, but it made real-world cases like the sqlite one above much more confusing. |
I think the sqlite package should prevent the build from complete
when cgo is not enabled.
The behavior is to make package with cgo and fallback code
compile without annotating each cgo source file with unnecessary
cgo build tag.
|
Rejecting the C files introduced in 1.4. Sorry, it sounds like skipping them has introduced. It is more of a requirement now to ship a package with cgo and !cgo implementations because C compilers are not primarily required for Go. Implicitly scoping C files without a build tag to cgo builds is not bad but handy. sqlite package should maybe add cgo build tag to restrict itself to cgo builds. |
This is working as intended: it lets you write a cgo implementation and a non-cgo implementation in the same directory. It sounds like the package you are building has a non-cgo implementation that does nothing. You can write files that are compiled only when not using cgo by adding "// +build !cgo" tags to them. |
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?What did you do?
Built a package with both cgo and non-cgo files, with CGO_ENABLED=0:
What did you expect to see?
An error about not being able to build cgo files.
What did you see instead?
cgo files were silently dropped, and the binary built anyway.
This is the simplified case, which might seem like the correct behavior, but how I found this was by cross-compiling
github.com/mattn/go-sqlite3/_example/simple
, which imports_ github.com/mattn/go-sqlite3
, which register adatabase/sql
driver.I expected the command to fail, instead it built a binary. I was very puzzled. Eventually, I tried that binary and it complained about an unknown driver. Turns out
github.com/mattn/go-sqlite3
has one file with no cgo directives:doc.go
. Which makes failures to compile the package completely silent.I don't think that should be intended behavior. A cgo-specific error is much more useful than an attempt at building anyway, which can lead to the confusing:
The text was updated successfully, but these errors were encountered: