Skip to content

Commit aabb6ba

Browse files
diamondburnedaykevl
authored andcommitted
main: use zversion.go and VERSION files to detect version, fixes #433
1 parent 6611578 commit aabb6ba

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

target.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"os/user"
1212
"path/filepath"
13+
"regexp"
1314
"runtime"
1415
"strconv"
1516
"strings"
@@ -384,14 +385,30 @@ func isGoroot(goroot string) bool {
384385
// getGorootVersion returns the major and minor version for a given GOROOT path.
385386
// If the goroot cannot be determined, (0, 0) is returned.
386387
func getGorootVersion(goroot string) (major, minor int, err error) {
387-
data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION"))
388-
if err != nil {
388+
var s string
389+
390+
if data, err := ioutil.ReadFile(filepath.Join(
391+
goroot, "src", "runtime", "internal", "sys", "zversion.go")); err == nil {
392+
393+
r := regexp.MustCompile("const TheVersion = `(.*)`")
394+
matches := r.FindSubmatch(data)
395+
if len(matches) != 2 {
396+
return 0, 0, errors.New("Invalid go version output:\n" + string(data))
397+
}
398+
399+
s = string(matches[1])
400+
401+
} else if data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION")); err == nil {
402+
s = string(data)
403+
404+
} else {
389405
return 0, 0, err
390406
}
391-
s := string(data)
392-
if s[:2] != "go" {
407+
408+
if s == "" || s[:2] != "go" {
393409
return 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix")
394410
}
411+
395412
parts := strings.Split(s[2:], ".")
396413
if len(parts) < 2 {
397414
return 0, 0, errors.New("could not parse Go version: version has less than two parts")

0 commit comments

Comments
 (0)