Add support for rc and beta versions in go.mod file (#701)

* Add support for rc and beta versions in go.mod file

The regex in `parseGoVersionFile()` didn't consider rc and beta
versions. The regex has been extended.

In `findMatch()` the match is checked using `semver.satisfies()`. But
`semver.satisfies()` doesn't no about Go's non-semver rc and beta
version formats (1.16c1 instead of 1.16.0-rc.1).

We cannot use `makeSemver()` on `versionSpec` and compare the result
using `semver.satisfies()` because `versionSpec` could be, well, a
spec. Instead we first check if there is a strict match between the
candidate version and the versionSpec.

Fixes #525.

* Add support for beta versions in go.mod toolchain directive

* Support prerelease versions in the toolchain cache
This commit is contained in:
Arne Jørgensen
2026-11-03 21:14:03 +01:00
committed by GitHub
parent b2a9ab24f0
commit 7d2730e317
4 changed files with 306 additions and 11 deletions
+12 -4
View File
@@ -128,8 +128,13 @@ export async function getGo(
? customToolCacheName(customBaseUrl)
: 'go';
// Convert prerelease specs (1.21rc2) to semver (1.21.0-rc.2).
const cacheSpec = /^\d+\.\d+(\.\d+)?(beta|rc)\d+$/.test(versionSpec)
? makeSemver(versionSpec)
: versionSpec;
// check cache
const toolPath = tc.find(toolCacheName, versionSpec, arch);
const toolPath = tc.find(toolCacheName, cacheSpec, arch);
// If not found in cache, download
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
@@ -598,7 +603,10 @@ export async function findMatch(
const version = makeSemver(candidate.version);
core.debug(`check ${version} satisfies ${versionSpec}`);
if (semver.satisfies(version, versionSpec)) {
if (
candidate.version.replace(/^go/, '') === versionSpec ||
semver.satisfies(version, versionSpec)
) {
goFile = candidate.files.find(file => {
core.debug(
`${file.arch}===${archFilter} && ${file.os}===${platFilter}`
@@ -677,7 +685,7 @@ export function parseGoVersionFile(versionFilePath: string): string {
if (process.env[GOTOOLCHAIN_ENV_VAR] !== GOTOOLCHAIN_LOCAL_VAL) {
// toolchain directive: https://go.dev/ref/mod#go-mod-file-toolchain
const matchToolchain = contents.match(
/^toolchain go(1\.\d+(?:\.\d+|rc\d+)?)/m
/^toolchain go(1\.\d+(?:\.\d+|(beta|rc)\d+)?)/m
);
if (matchToolchain) {
return matchToolchain[1];
@@ -685,7 +693,7 @@ export function parseGoVersionFile(versionFilePath: string): string {
}
// go directive: https://go.dev/ref/mod#go-mod-file-go
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
const matchGo = contents.match(/^go (\d+(\.\d+)*(?:\.\d+|(beta|rc)\d+)?)/m);
return matchGo ? matchGo[1] : '';
} else if (path.basename(versionFilePath) === '.tool-versions') {
const match = contents.match(/^golang\s+([^\n#]+)/m);