codeql
codeql copied to clipboard
Go: Use `toolchain` directives for version selection if available, and add tests
Context
In Go 1.21, the Go team started making a distinction between language and toolchain versions. Historically, the Go version is declared with a go directive in a go.mod file, or a go.work file. Since Go 1.21, go directives are used to declare the language version. A new toolchain directive may be used to explicitly declare the toolchain version. For backwards compatibility, if there is no toolchain directive, the language version is used as the toolchain version. In the Go autobuilder, we have numerous places where we try to determine the "version" of Go that is in use, should be installed, etc. Here, we are mainly interested in the toolchain version.
What this PR addresses
So far, we have mainly been looking at go directives in go.mod files, and recently go.work files, for this. However, if a toolchain directive is present in either type of file, then this determines the toolchain version. We have not been considering this and this PR addresses that shortcoming by modifying the autobuilder to check for the presence of toolchain directives when determining the version that is in use.
This PR also adds a number of tests for the functions involved in this process.
How to review
Best reviewed commit-by-commit.
@owen-mc I addressed your comments, but I also realised that the first batch of changes in this PR meant that GetWorkspaceInfo now returns version strings in the format used by semver (starting with "v"). Rather than trying to identify how that affects downstream code, I tried to make all consumers of the information obtained from GetWorkspaceInfo resilient to both of the different formats (with and without "v"). I also added tests for that.
In retrospect, maybe it would be better to introduce a new type that's a wrapper around string for all the versions that are in the semver format.
Replaced by https://github.com/github/codeql/pull/16703