builder: check for Go toolchain version used to compile TinyGo

This shows a much better error message for issues like this one:
https://github.com/NixOS/nixpkgs/pull/341170#issuecomment-2359237471

The new error message would be:

    cannot compile with Go toolchain version go1.23 (TinyGo was built using toolchain version go1.21.4)
This commit is contained in:
Ayke van Laethem
2024-10-12 11:10:21 +02:00
committed by Ayke
parent 9583439be4
commit ac5f84e3d7
2 changed files with 31 additions and 7 deletions
+22 -4
View File
@@ -2,6 +2,7 @@ package builder
import ( import (
"fmt" "fmt"
"runtime"
"github.com/tinygo-org/tinygo/compileopts" "github.com/tinygo-org/tinygo/compileopts"
"github.com/tinygo-org/tinygo/goenv" "github.com/tinygo-org/tinygo/goenv"
@@ -23,20 +24,37 @@ func NewConfig(options *compileopts.Options) (*compileopts.Config, error) {
spec.OpenOCDCommands = options.OpenOCDCommands spec.OpenOCDCommands = options.OpenOCDCommands
} }
major, minor, err := goenv.GetGorootVersion() // Version range supported by TinyGo.
const minorMin = 19
const minorMax = 23
// Check that we support this Go toolchain version.
gorootMajor, gorootMinor, err := goenv.GetGorootVersion()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if major != 1 || minor < 19 || minor > 23 { if gorootMajor != 1 || gorootMinor < minorMin || gorootMinor > minorMax {
// Note: when this gets updated, also update the Go compatibility matrix: // Note: when this gets updated, also update the Go compatibility matrix:
// https://github.com/tinygo-org/tinygo-site/blob/dev/content/docs/reference/go-compat-matrix.md // https://github.com/tinygo-org/tinygo-site/blob/dev/content/docs/reference/go-compat-matrix.md
return nil, fmt.Errorf("requires go version 1.19 through 1.23, got go%d.%d", major, minor) return nil, fmt.Errorf("requires go version 1.19 through 1.23, got go%d.%d", gorootMajor, gorootMinor)
}
// Check that the Go toolchain version isn't too new, if we haven't been
// compiled with the latest Go version.
// This may be a bit too aggressive: if the newer version doesn't change the
// Go language we will most likely be able to compile it.
buildMajor, buildMinor, err := goenv.Parse(runtime.Version())
if err != nil {
return nil, err
}
if buildMajor != 1 || buildMinor < gorootMinor {
return nil, fmt.Errorf("cannot compile with Go toolchain version go%d.%d (TinyGo was built using toolchain version %s)", gorootMajor, gorootMinor, runtime.Version())
} }
return &compileopts.Config{ return &compileopts.Config{
Options: options, Options: options,
Target: spec, Target: spec,
GoMinorVersion: minor, GoMinorVersion: gorootMinor,
TestConfig: options.TestConfig, TestConfig: options.TestConfig,
}, nil }, nil
} }
+9 -3
View File
@@ -34,19 +34,25 @@ func GetGorootVersion() (major, minor int, err error) {
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
return Parse(s)
}
if s == "" || s[:2] != "go" { // Parse parses the Go version (like "go1.3.2") in the parameter and return the
// major and minor version: 1 and 3 in this example. If there is an error, (0,
// 0) and an error will be returned.
func Parse(version string) (major, minor int, err error) {
if version == "" || version[:2] != "go" {
return 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix") return 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix")
} }
parts := strings.Split(s[2:], ".") parts := strings.Split(version[2:], ".")
if len(parts) < 2 { if len(parts) < 2 {
return 0, 0, errors.New("could not parse Go version: version has less than two parts") return 0, 0, errors.New("could not parse Go version: version has less than two parts")
} }
// Ignore the errors, we don't really handle errors here anyway. // Ignore the errors, we don't really handle errors here anyway.
var trailing string var trailing string
n, err := fmt.Sscanf(s, "go%d.%d%s", &major, &minor, &trailing) n, err := fmt.Sscanf(version, "go%d.%d%s", &major, &minor, &trailing)
if n == 2 && err == io.EOF { if n == 2 && err == io.EOF {
// Means there were no trailing characters (i.e., not an alpha/beta) // Means there were no trailing characters (i.e., not an alpha/beta)
err = nil err = nil