Skip to content

Commit cfb76c5

Browse files
committed
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` coulb be, well, a spec. Instead we forst check if there is a strict match between the candidate version and `makeSemver(versionSpec)`. Fixes #525.
1 parent 4dc6199 commit cfb76c5

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

__tests__/setup-go.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,19 @@ describe('setup-go', () => {
265265
expect(fileName).toBe('go1.14rc1.linux-amd64.tar.gz');
266266
});
267267

268+
it('finds unstable pre-release version (go version format)', async () => {
269+
os.platform = 'linux';
270+
os.arch = 'x64';
271+
272+
// spec: 1.14rc1, stable=false => go1.14rc1
273+
const match: im.IGoVersion | undefined = await im.findMatch('1.14rc1');
274+
expect(match).toBeDefined();
275+
const version: string = match ? match.version : '';
276+
expect(version).toBe('go1.14rc1');
277+
const fileName = match ? match.files[0].filename : '';
278+
expect(fileName).toBe('go1.14rc1.linux-amd64.tar.gz');
279+
});
280+
268281
it('evaluates to stable with input as true', async () => {
269282
inputs['go-version'] = '1.13.0';
270283
inputs.stable = 'true';

dist/setup/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94763,7 +94763,8 @@ function findMatch(versionSpec_1) {
9476394763
const candidate = candidates[i];
9476494764
const version = makeSemver(candidate.version);
9476594765
core.debug(`check ${version} satisfies ${versionSpec}`);
94766-
if (semver.satisfies(version, versionSpec)) {
94766+
if (version === makeSemver(versionSpec) ||
94767+
semver.satisfies(version, versionSpec)) {
9476794768
goFile = candidate.files.find(file => {
9476894769
core.debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
9476994770
return file.arch === archFilter && file.os === platFilter;
@@ -94831,7 +94832,7 @@ function parseGoVersionFile(versionFilePath) {
9483194832
}
9483294833
}
9483394834
// go directive: https://go.dev/ref/mod#go-mod-file-go
94834-
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
94835+
const matchGo = contents.match(/^go (\d+(\.\d+)*(?:\.\d+|(beta|rc)\d+)?)/m);
9483594836
return matchGo ? matchGo[1] : '';
9483694837
}
9483794838
else if (path.basename(versionFilePath) === '.tool-versions') {

src/installer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,10 @@ export async function findMatch(
423423
const version = makeSemver(candidate.version);
424424

425425
core.debug(`check ${version} satisfies ${versionSpec}`);
426-
if (semver.satisfies(version, versionSpec)) {
426+
if (
427+
version === makeSemver(versionSpec) ||
428+
semver.satisfies(version, versionSpec)
429+
) {
427430
goFile = candidate.files.find(file => {
428431
core.debug(
429432
`${file.arch}===${archFilter} && ${file.os}===${platFilter}`
@@ -511,7 +514,7 @@ export function parseGoVersionFile(versionFilePath: string): string {
511514
}
512515

513516
// go directive: https://go.dev/ref/mod#go-mod-file-go
514-
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
517+
const matchGo = contents.match(/^go (\d+(\.\d+)*(?:\.\d+|(beta|rc)\d+)?)/m);
515518
return matchGo ? matchGo[1] : '';
516519
} else if (path.basename(versionFilePath) === '.tool-versions') {
517520
const match = contents.match(/^golang\s+([^\n#]+)/m);

0 commit comments

Comments
 (0)