Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 4d08437

Browse files
committed
Fix autobuilder Go version comparison
The semver package requires versions of the form v1.2.3, and unhelpfully evaluates any malformed versions as equal.
1 parent c6dbb9f commit 4d08437

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ variable is 32.
4747

4848
var goVersion = ""
4949

50+
// Returns the current Go version as returned by 'go version', e.g. go1.14.4
5051
func getEnvGoVersion() string {
5152
if goVersion == "" {
5253
gover, err := exec.Command("go", "version").CombinedOutput()
@@ -58,6 +59,15 @@ func getEnvGoVersion() string {
5859
return goVersion
5960
}
6061

62+
// Returns the current Go version in semver format, e.g. v1.14.4
63+
func getEnvGoSemVer() string {
64+
goVersion := getEnvGoVersion()
65+
if !strings.HasPrefix(goVersion, "go") {
66+
log.Fatalf("Expected 'go version' output of the form 'go1.2.3'; got '%s'", goVersion)
67+
}
68+
return "v" + goVersion[2:]
69+
}
70+
6171
func run(cmd *exec.Cmd) bool {
6272
cmd.Stdout = os.Stdout
6373
cmd.Stderr = os.Stderr
@@ -164,11 +174,12 @@ func (m ModMode) argsForGoVersion(version string) []string {
164174
case ModReadonly:
165175
return []string{"-mod=readonly"}
166176
case ModMod:
167-
if semver.Compare(getEnvGoVersion(), "1.14") < 0 {
168-
log.Printf("%s < %s", getEnvGoVersion(), "1.14")
177+
if !semver.IsValid(version) {
178+
log.Fatalf("Invalid Go semver: '%s'", version)
179+
}
180+
if semver.Compare(version, "v1.14") < 0 {
169181
return []string{} // -mod=mod is the default behaviour for go <= 1.13, and is not accepted as an argument
170182
} else {
171-
log.Printf("%s >= %s", getEnvGoVersion(), "1.14")
172183
return []string{"-mod=mod"}
173184
}
174185
case ModVendor:
@@ -505,7 +516,7 @@ func main() {
505516

506517
extractorArgs := []string{}
507518
if depMode == GoGetWithModules {
508-
extractorArgs = append(extractorArgs, modMode.argsForGoVersion(getEnvGoVersion())...)
519+
extractorArgs = append(extractorArgs, modMode.argsForGoVersion(getEnvGoSemVer())...)
509520
}
510521
extractorArgs = append(extractorArgs, "./...")
511522

0 commit comments

Comments
 (0)