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 authored and GitHub committed 2026-11-03 14:14:03 -06:00
1 parent b2a9ab24f0
commit 7d2730e317
4 files changed
+306 -11

No files matched your search

+9 -4
View File
@@ -42010,8 +42010,12 @@ async function getGo(versionSpec, checkLatest, auth, arch = external_os_default(
const toolCacheName = customBaseUrl
? 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 = find(toolCacheName, versionSpec, arch);
const toolPath = find(toolCacheName, cacheSpec, arch);
// If not found in cache, download
if (toolPath) {
core_info(`Found in cache @ ${toolPath}`);
@@ -42320,7 +42324,8 @@ async function findMatch(versionSpec, arch = external_os_default().arch(), dlUrl
const candidate = candidates[i];
const version = makeSemver(candidate.version);
core_debug(`check ${version} satisfies ${versionSpec}`);
if (node_modules_semver.satisfies(version, versionSpec)) {
if (candidate.version.replace(/^go/, '') === versionSpec ||
node_modules_semver.satisfies(version, versionSpec)) {
goFile = candidate.files.find(file => {
core_debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
return file.arch === archFilter && file.os === platFilter;
@@ -42378,13 +42383,13 @@ function parseGoVersionFile(versionFilePath) {
// 'GOTOOLCHAIN' has been explicitly set
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);
const matchToolchain = contents.match(/^toolchain go(1\.\d+(?:\.\d+|(beta|rc)\d+)?)/m);
if (matchToolchain) {
return matchToolchain[1];
}
}
// 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 (external_path_namespaceObject.basename(versionFilePath) === '.tool-versions') {