Skip to content

Commit ac5f84e

Browse files
committed
builder: check for Go toolchain version used to compile TinyGo
This shows a much better error message for issues like this one: NixOS/nixpkgs#341170 (comment) The new error message would be: cannot compile with Go toolchain version go1.23 (TinyGo was built using toolchain version go1.21.4)
1 parent 9583439 commit ac5f84e

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

builder/config.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package builder
22

33
import (
44
"fmt"
5+
"runtime"
56

67
"github.com/tinygo-org/tinygo/compileopts"
78
"github.com/tinygo-org/tinygo/goenv"
@@ -23,20 +24,37 @@ func NewConfig(options *compileopts.Options) (*compileopts.Config, error) {
2324
spec.OpenOCDCommands = options.OpenOCDCommands
2425
}
2526

26-
major, minor, err := goenv.GetGorootVersion()
27+
// Version range supported by TinyGo.
28+
const minorMin = 19
29+
const minorMax = 23
30+
31+
// Check that we support this Go toolchain version.
32+
gorootMajor, gorootMinor, err := goenv.GetGorootVersion()
2733
if err != nil {
2834
return nil, err
2935
}
30-
if major != 1 || minor < 19 || minor > 23 {
36+
if gorootMajor != 1 || gorootMinor < minorMin || gorootMinor > minorMax {
3137
// Note: when this gets updated, also update the Go compatibility matrix:
3238
// https://github.com/tinygo-org/tinygo-site/blob/dev/content/docs/reference/go-compat-matrix.md
33-
return nil, fmt.Errorf("requires go version 1.19 through 1.23, got go%d.%d", major, minor)
39+
return nil, fmt.Errorf("requires go version 1.19 through 1.23, got go%d.%d", gorootMajor, gorootMinor)
40+
}
41+
42+
// Check that the Go toolchain version isn't too new, if we haven't been
43+
// compiled with the latest Go version.
44+
// This may be a bit too aggressive: if the newer version doesn't change the
45+
// Go language we will most likely be able to compile it.
46+
buildMajor, buildMinor, err := goenv.Parse(runtime.Version())
47+
if err != nil {
48+
return nil, err
49+
}
50+
if buildMajor != 1 || buildMinor < gorootMinor {
51+
return nil, fmt.Errorf("cannot compile with Go toolchain version go%d.%d (TinyGo was built using toolchain version %s)", gorootMajor, gorootMinor, runtime.Version())
3452
}
3553

3654
return &compileopts.Config{
3755
Options: options,
3856
Target: spec,
39-
GoMinorVersion: minor,
57+
GoMinorVersion: gorootMinor,
4058
TestConfig: options.TestConfig,
4159
}, nil
4260
}

goenv/version.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,25 @@ func GetGorootVersion() (major, minor int, err error) {
3434
if err != nil {
3535
return 0, 0, err
3636
}
37+
return Parse(s)
38+
}
3739

38-
if s == "" || s[:2] != "go" {
40+
// Parse parses the Go version (like "go1.3.2") in the parameter and return the
41+
// major and minor version: 1 and 3 in this example. If there is an error, (0,
42+
// 0) and an error will be returned.
43+
func Parse(version string) (major, minor int, err error) {
44+
if version == "" || version[:2] != "go" {
3945
return 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix")
4046
}
4147

42-
parts := strings.Split(s[2:], ".")
48+
parts := strings.Split(version[2:], ".")
4349
if len(parts) < 2 {
4450
return 0, 0, errors.New("could not parse Go version: version has less than two parts")
4551
}
4652

4753
// Ignore the errors, we don't really handle errors here anyway.
4854
var trailing string
49-
n, err := fmt.Sscanf(s, "go%d.%d%s", &major, &minor, &trailing)
55+
n, err := fmt.Sscanf(version, "go%d.%d%s", &major, &minor, &trailing)
5056
if n == 2 && err == io.EOF {
5157
// Means there were no trailing characters (i.e., not an alpha/beta)
5258
err = nil

0 commit comments

Comments
 (0)