Skip to content

Add an environment variable which overrides the Go version detection #1147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ You can also configure the hard-coded credentials rule `G101` with additional pa

Some rules require a specific Go version which is retrieved from the Go module file present in the project. If this version cannot be found, it will fallback to Go runtime version.

The Go module version is parsed using the `go list` command which in some cases might lead to performance degradation. In this situation, the go module version can be easily disabled by setting the environment variable `GOSECNOMODVERSION=on`.
The Go module version is parsed using the `go list` command which in some cases might lead to performance degradation. In this situation, the go module version can be easily provided by setting the environment variable `GOSECGOVERSION=go1.21.1`.

### Dependencies

Expand Down
19 changes: 11 additions & 8 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"strings"
)

// noGoModVersion disables the parsing of go version from go module file present in the project
const noGoModVersion = "GOSECNOMODVERSION"
// envGoModVersion overrides the Go version detection.
const envGoModVersion = "GOSECGOVERSION"

// MatchCallByPackage ensures that the specified package is imported,
// adjusts the name for any aliases and ignores cases that are
Expand Down Expand Up @@ -501,13 +501,16 @@ func RootPath(root string) (string, error) {

// GoVersion returns parsed version of Go mod version and fallback to runtime version if not found.
func GoVersion() (int, int, int) {
_, ok := os.LookupEnv(noGoModVersion)
if !ok {
if goModVersion, err := goModVersion(); err == nil {
return parseGoVersion(goModVersion)
}
if env, ok := os.LookupEnv(envGoModVersion); ok {
return parseGoVersion(strings.TrimPrefix(env, "go"))
}
return parseGoVersion(strings.TrimPrefix(runtime.Version(), "go"))

goVersion, err := goModVersion()
if err != nil {
return parseGoVersion(strings.TrimPrefix(runtime.Version(), "go"))
}

return parseGoVersion(goVersion)
}

type goListOutput struct {
Expand Down
Loading