hack: Prefer go toolchain version in go-version.sh#2713
Conversation
When a `toolchain` directive is present in go.mod, use its version instead of the `go` directive. This ensures that install-go.sh downloads the correct patch-level Go binary and that Makefile's GO_VERSION reflects the intended toolchain. Assisted-by: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Ram Lavi <ralavi@redhat.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
There was a problem hiding this comment.
Code Review
This pull request updates the hack/go-version.sh script to prioritize the toolchain directive over the go directive when determining the Go version from go.mod. The feedback suggests optimizing the script into a single awk command to improve efficiency and prevent potential false matches on non-directive lines.
| toolchain=$(grep '^toolchain' go.mod | awk '{print $2}' | sed 's/^go//') | ||
| if [ -n "$toolchain" ]; then | ||
| echo "$toolchain" | ||
| else | ||
| grep '^go' go.mod | awk '{print $2}' | ||
| fi |
There was a problem hiding this comment.
The current implementation uses multiple pipes and multiple passes over the go.mod file. It also uses grep patterns that could potentially match non-directive lines (e.g., module paths starting with go or toolchain in a require block).
A more robust and efficient approach is to use a single awk command to parse the file in one pass, ensuring only the intended directives are matched and correctly handling the fallback logic.
| toolchain=$(grep '^toolchain' go.mod | awk '{print $2}' | sed 's/^go//') | |
| if [ -n "$toolchain" ]; then | |
| echo "$toolchain" | |
| else | |
| grep '^go' go.mod | awk '{print $2}' | |
| fi | |
| awk '/^toolchain[[:space:]]/ { sub(/^go/, "", $2); print $2; f=1; exit } /^go[[:space:]]/ { v=$2 } END { if (!f && v) print v }' go.mod |
|
/cherry-pick release-0.100 |
|
@RamLavi: once the present PR merges, I will cherry-pick it on top of DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |



What this PR does / why we need it:
When a
toolchaindirective is present in go.mod, use its version instead of thegodirective. This ensures that install-go.sh downloads the correct patch-level Go binary and that Makefile's GO_VERSION reflects the intended toolchain.This PR makes the repo toolchain aware.
Special notes for your reviewer:
Release note: