Browse Source

main: show a better error when version detection of GOROOT failed

pull/396/head
Ayke van Laethem 6 years ago
committed by Ron Evans
parent
commit
776dc1e0d9
  1. 8
      main.go
  2. 18
      target.go

8
main.go

@ -81,11 +81,11 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
return errors.New("cannot locate $GOROOT, please set it manually") return errors.New("cannot locate $GOROOT, please set it manually")
} }
tags := spec.BuildTags tags := spec.BuildTags
major, minor := getGorootVersion(goroot) major, minor, err := getGorootVersion(goroot)
if err != nil {
return fmt.Errorf("could not read version from GOROOT (%v): %v", goroot, err)
}
if major != 1 { if major != 1 {
if major == 0 {
return errors.New("could not read version from GOROOT: " + goroot)
}
return fmt.Errorf("expected major version 1, got go%d.%d", major, minor) return fmt.Errorf("expected major version 1, got go%d.%d", major, minor)
} }
for i := 1; i <= minor; i++ { for i := 1; i <= minor; i++ {

18
target.go

@ -375,23 +375,29 @@ func isGoroot(goroot string) bool {
// getGorootVersion returns the major and minor version for a given GOROOT path. // getGorootVersion returns the major and minor version for a given GOROOT path.
// If the goroot cannot be determined, (0, 0) is returned. // If the goroot cannot be determined, (0, 0) is returned.
func getGorootVersion(goroot string) (major, minor int) { func getGorootVersion(goroot string) (major, minor int, err error) {
data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION")) data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION"))
if err != nil { if err != nil {
return return 0, 0, err
} }
s := string(data) s := string(data)
if s[:2] != "go" { if s[:2] != "go" {
return 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(s[2:], ".")
if len(parts) < 2 { if len(parts) < 2 {
return return 0, 0, errors.New("could not parse Go version: version has less than two parts")
} }
// Ignore the errors, strconv.Atoi will return 0 on most errors and we // Ignore the errors, strconv.Atoi will return 0 on most errors and we
// don't really handle errors here anyway. // don't really handle errors here anyway.
major, _ = strconv.Atoi(parts[0]) major, err = strconv.Atoi(parts[0])
minor, _ = strconv.Atoi(parts[1]) if err != nil {
return 0, 0, fmt.Errorf("failed to parse major version: %s", err)
}
minor, err = strconv.Atoi(parts[1])
if err != nil {
return 0, 0, fmt.Errorf("failed to parse minor version: %s", err)
}
return return
} }

Loading…
Cancel
Save