Fix bump-go.sh to tolerate missing toolchain directive#12581
Fix bump-go.sh to tolerate missing toolchain directive#12581
Conversation
- Add early trap setup to avoid exit code issues - Handle missing toolchain directive gracefully with `|| true` - Add logic to detect when toolchain is expected to be missing - Add informative messages about missing toolchain - Implement smart toolchain handling: - Skip toolchain when go version matches latest (redundant) - Add toolchain when go version is older than latest - Update toolchain when it exists but is outdated Co-authored-by: williammartin <1611510+williammartin@users.noreply.github.com>
- Compare major.minor versions instead of exact string match - Preserve go directive if already at latest major.minor - Handle cases where go.mod has full patch version (e.g., 1.25.6) - Update PR body to use actual go directive from modified go.mod - Add detailed comments explaining version handling logic Co-authored-by: williammartin <1611510+williammartin@users.noreply.github.com>
- Clarify comment to say "matches" instead of "matches or exceeds" - Update PR body to accurately reflect final go.mod state - Show actual toolchain if present, or note it's not specified - Provide clear explanation when toolchain is omitted Co-authored-by: williammartin <1611510+williammartin@users.noreply.github.com>
|
@copilot can you rewrite this using |
Replace manual parsing and editing with go mod edit: - Use 'go mod edit -json' to read current go and toolchain directives - Use 'go mod edit -go' to update go directive - Use 'go mod edit -toolchain' to update toolchain directive - Remove manual sed/grep parsing and .bak file handling - More reliable and maintainable than custom text manipulation Co-authored-by: williammartin <1611510+williammartin@users.noreply.github.com>
Done! Rewrote the script to use Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
.github/workflows/scripts/bump-go.sh
Outdated
| [[ "$CURRENT_GO_DIRECTIVE" == "null" || -z "$CURRENT_GO_DIRECTIVE" ]] && CURRENT_GO_DIRECTIVE="" | ||
| [[ "$CURRENT_TOOLCHAIN_DIRECTIVE" == "null" || -z "$CURRENT_TOOLCHAIN_DIRECTIVE" ]] && CURRENT_TOOLCHAIN_DIRECTIVE="" |
There was a problem hiding this comment.
@copilot These vars cannot be "null", right? They're just defaulted to "".
There was a problem hiding this comment.
Correct! With jq -r '.Field // ""', the output is always an empty string for null/missing values, never the literal "null". Removed the unnecessary checks in commit b7fd470.
.github/workflows/scripts/bump-go.sh
Outdated
| FINAL_TOOLCHAIN_DIRECTIVE=$(jq -r '.Toolchain // ""' <<< "$FINAL_GO_MOD_JSON") | ||
|
|
||
| # Handle empty/null values | ||
| [[ "$FINAL_TOOLCHAIN_DIRECTIVE" == "null" || -z "$FINAL_TOOLCHAIN_DIRECTIVE" ]] && FINAL_TOOLCHAIN_DIRECTIVE="" |
The jq -r flag with // "" operator already returns empty string for null/missing values, never the literal string "null". The checks for == "null" are unnecessary. Co-authored-by: babakks <36728931+babakks@users.noreply.github.com>
| if [[ "$CURRENT_MAJOR_MINOR" = "$LATEST_MAJOR_MINOR" ]]; then | ||
| # Current go directive is at the same major.minor as latest | ||
| if [[ -z "$CURRENT_TOOLCHAIN_DIRECTIVE" ]]; then | ||
| # No toolchain directive present - this is expected when go version matches latest | ||
| echo "Already on latest Go version: $CURRENT_GO_DIRECTIVE (latest toolchain: $TOOLCHAIN_VERSION)" | ||
| echo " → Note: No toolchain directive (expected when go version matches latest toolchain)" | ||
| exit 0 | ||
| elif [[ "$CURRENT_TOOLCHAIN_DIRECTIVE" = "go$TOOLCHAIN_VERSION" ]]; then | ||
| echo "Already on latest Go version: $CURRENT_GO_DIRECTIVE (toolchain: $CURRENT_TOOLCHAIN_DIRECTIVE)" | ||
| exit 0 | ||
| fi | ||
| # Current go directive is latest but toolchain is outdated - continue to update toolchain | ||
| fi | ||
|
|
||
| echo "Creating branch $BRANCH" | ||
| git switch -c "$BRANCH" >/dev/null 2>&1 | ||
|
|
||
| # ---- Patch go.mod ----------------------------------------------------------- | ||
| if [[ "$CURRENT_GO_DIRECTIVE" != "$GO_DIRECTIVE_VERSION" ]]; then | ||
| sed -Ei.bak "s/^go [0-9]+\.[0-9]+.*$/go $GO_DIRECTIVE_VERSION/" "$GO_MOD" | ||
| echo " • go directive $CURRENT_GO_DIRECTIVE → $GO_DIRECTIVE_VERSION" | ||
| BRANCH_CREATED=1 |
There was a problem hiding this comment.
@williammartin maybe we should get rid of this part, and apply the toolchain regardess. Then run a tidy at the end to wrap up?
Summary: Fix bump-go.sh to tolerate missing toolchain directive
Successfully updated the script to handle missing toolchain directives and rewrote it using
go mod editcommands.Latest Changes (This Commit)
Simplified jq output handling based on code review feedback:
== "null"checks (lines 56-57, 160)jq -r '.Field // ""'already returns empty string for null/missing values-r(raw) flag ensures we get unquoted output// ""operator provides empty string as defaultPrevious Changes
Replaced manual parsing with go mod edit:
go mod edit -json | jqto read go and toolchain directivesgo mod edit -goand-toolchainto update directivesSmart version comparison:
Proper error handling:
All scenarios tested and working correctly.
Original prompt
This pull request was created from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.