-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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/go: build tag constraint vs go.mod version #64308
Comments
I think this question is more related to the Go command? Feel free to switch it back to cmd/compile -- sorry for the noise if I'm wrong. Also, for future reference, I think situations where you're unsure if there's a bug in the toolchain or if you're just misunderstanding something, a good place to start is the golang-nuts mailing list. |
@oliverpool, a negative build constraint is not the right tool, unfortunately. A toolchain that knows how to compile See: |
This seems to be working as designed given #59033, although it's unfortunate that that makes the polyfill use-case more difficult. Given that you have the polyfill file anyway, is there some reason you can't use the polyfill definition of |
In this particular case, you could take advantage of the property that the meaning of the go directive changed between 1.20 and 1.21. In 1.20, it was advisory, and in 1.21, it becomes the minimum required. Specifically, a way for you to use the predeclared module code.pfad.fr/exp
-go 1.20
+go 1.21 $ go run .
min 1
$ GOTOOLCHAIN=go1.20.11 go run .
min 1 This is a one-time transitional quirk that won't apply in the future though. |
@dmitshur thanks for the tip! The main drawback is that I won't know if I use any features of go1.20 without a proper polyfill (without actually using go1.20). Another (cumbersome, but I believe still available in the future) solution is to rename to
//go:build go1.21
package main
func min_(a, b int) int {
return min(a, b)
}
//go:build !go1.21
package main
// min is a pseudo-polyfill for go1.21 min function
func min(a, b int) int {
if a < b {
return a
}
return b
} In any case, I find think this unexpected behavior should be better documented (if not changed): the language version declared by the Should I close this issue in favor of #61894 ? |
Thinking more about this, it is only relevant for Because it only concerns builtins:
I guess the second option is much easier, what do you think? |
given #59033 i think it should work in the future when the go directive is > 1.21.0 ? |
@seankhliao apparently it is already implemented: #59033 (comment) My "2-file" example seems to take advantage of it: #64308 (comment) (however my go.mod states "go 1.20", so maybe this is unrelated?) |
I think this can be folded into #61894 |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I want to publish a package compatible with go1.20 and go1.21, which uses the
min
function.Since the builtin was introduced in go1.21, I added a pseudo-polyfill in a file with a build constraint
//go:build !go1.21
.main.go
min_polyfill.go
go.mod
Complete program available here: https://git.sr.ht/~oliverpool/exp/tree/main/item/min_polyfill
I then run my program using
go run ./min_polyfill
What did you expect to see?
Either the build constraint indicates that go is not 1.21 yet and the polyfill should be used, or the builtin should be used
What did you see instead?
Maybe a build constraint is not the right tool, or I a misusing it?
Anyway, how can I use the
min
builtin from go1.21 with a fallback for go1.20?The text was updated successfully, but these errors were encountered: