From 9a2ef369b2ee71481ff62ffc9ad699bc6c20aacc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 18 Feb 2024 09:51:43 +0000 Subject: [PATCH] fail early if we know we lack Go linker patches Right now, we only have linker patches for Go 1.22.x. We only ever maintain those for one or two major Go versions at a time. If a user tries to use the Go toolchain from 1.21, we already fail with "Go version too old" messages early on, but we don't for 1.23, causing a relatively confusing error later on when we link a binary: cannot get modified linker: cannot retrieve linker patches: open patches/go1.23: file does not exist Instead, fail early and with a good error message. --- main.go | 9 ++++++++- testdata/script/goversion.txtar | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index cd75b1e..a30914b 100644 --- a/main.go +++ b/main.go @@ -268,7 +268,10 @@ type errJustExit int func (e errJustExit) Error() string { return fmt.Sprintf("exit: %d", e) } func goVersionOK() bool { - const minGoVersion = "go1.22" + const ( + minGoVersion = "go1.22" // the first major version we support + maxGoVersion = "go1.23" // the first major version we don't support + ) // rxVersion looks for a version like "go1.2" or "go1.2.3" in `go env GOVERSION`. rxVersion := regexp.MustCompile(`go\d+\.\d+(?:\.\d+)?`) @@ -285,6 +288,10 @@ func goVersionOK() bool { fmt.Fprintf(os.Stderr, "Go version %q is too old; please upgrade to %s or newer\n", toolchainVersionFull, minGoVersion) return false } + if version.Compare(sharedCache.GoVersion, maxGoVersion) >= 0 { + fmt.Fprintf(os.Stderr, "Go version %q is too new; Go linker patches aren't available for %s or later yet\n", toolchainVersionFull, maxGoVersion) + return false + } // Ensure that the version of Go that built the garble binary is equal or // newer than cache.GoVersionSemver. diff --git a/testdata/script/goversion.txtar b/testdata/script/goversion.txtar index 9ef85cc..fb07ae2 100644 --- a/testdata/script/goversion.txtar +++ b/testdata/script/goversion.txtar @@ -32,15 +32,15 @@ env TOOLCHAIN_GOVERSION='go1.14' ! exec garble build stderr 'Go version "go1\.14" is too old; please upgrade to go1\.22 or newer' -# We should accept a future stable version. +# We should reject a future stable version, as we don't have linker patches yet. # Note that we need to bump the version of Go that supposedly built it, too. env GARBLE_TEST_GOVERSION='go1.28.2' env TOOLCHAIN_GOVERSION='go1.28.2' ! exec garble build -stderr 'mocking the real build' +stderr 'Go version "go1\.28\.2" is too new; Go linker patches aren''t available for go1\.23 or later yet' # We should accept custom devel strings. -env TOOLCHAIN_GOVERSION='devel go1.23-somecustomversion' +env TOOLCHAIN_GOVERSION='devel go1.22-somecustomversion' ! exec garble build stderr 'mocking the real build'