-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve runtime version parsing (#4961)
- Loading branch information
Showing
6 changed files
with
238 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package goutil | ||
|
||
import ( | ||
"fmt" | ||
"go/version" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
|
||
hcversion "github.com/hashicorp/go-version" | ||
) | ||
|
||
func CheckGoVersion(goVersion string) error { | ||
rv, err := CleanRuntimeVersion() | ||
if err != nil { | ||
return fmt.Errorf("clean runtime version: %w", err) | ||
} | ||
|
||
langVersion := version.Lang(rv) | ||
|
||
runtimeVersion, err := hcversion.NewVersion(strings.TrimPrefix(langVersion, "go")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
targetedVersion, err := hcversion.NewVersion(TrimGoVersion(goVersion)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if runtimeVersion.LessThan(targetedVersion) { | ||
return fmt.Errorf("the Go language version (%s) used to build golangci-lint is lower than the targeted Go version (%s)", | ||
langVersion, goVersion) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// TrimGoVersion Trims the Go version to keep only M.m. | ||
// Since Go 1.21 the version inside the go.mod can be a patched version (ex: 1.21.0). | ||
// The version can also include information which we want to remove (ex: 1.21alpha1) | ||
// https://go.dev/doc/toolchain#versions | ||
// This a problem with staticcheck and gocritic. | ||
func TrimGoVersion(v string) string { | ||
if v == "" { | ||
return "" | ||
} | ||
|
||
exp := regexp.MustCompile(`(\d\.\d+)(?:\.\d+|[a-z]+\d)`) | ||
|
||
if exp.MatchString(v) { | ||
return exp.FindStringSubmatch(v)[1] | ||
} | ||
|
||
return v | ||
} | ||
|
||
func CleanRuntimeVersion() (string, error) { | ||
return cleanRuntimeVersion(runtime.Version()) | ||
} | ||
|
||
func cleanRuntimeVersion(rv string) (string, error) { | ||
parts := strings.Fields(rv) | ||
|
||
for _, part := range parts { | ||
// Allow to handle: | ||
// - GOEXPERIMENT -> "go1.23.0 X:boringcrypto" | ||
// - devel -> "devel go1.24-e705a2d Wed Aug 7 01:16:42 2024 +0000 linux/amd64" | ||
if strings.HasPrefix(part, "go1.") { | ||
return part, nil | ||
} | ||
} | ||
|
||
return "", fmt.Errorf("invalid Go runtime version: %s", rv) | ||
} |
Oops, something went wrong.