Go: Use toolchain directives for version selection if available, and add tests (v2) #16703
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supercedes #16453 using an implementation based on the new
SemVer
type introduced by #16460 (which should be merged first).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 ago.mod
file, or ago.work
file. Since Go 1.21,go
directives are used to declare the language version. A newtoolchain
directive may be used to explicitly declare the toolchain version. For backwards compatibility, if there is notoolchain
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 ingo.mod
files, and recentlygo.work
files, for this. However, if atoolchain
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 oftoolchain
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.