Skip to content

Commit 27a670e

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` could be, well, a spec. Instead we first check if there is a strict match between the candidate version and the versionSpec. Fixes #525.
1 parent 4a36011 commit 27a670e

File tree

4 files changed

+228
-6
lines changed

4 files changed

+228
-6
lines changed

__tests__/data/golang-dl.json

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,181 @@
11
[
2+
{
3+
"version": "go1.18beta2",
4+
"stable": false,
5+
"files": [
6+
{
7+
"filename": "go1.18beta2.src.tar.gz",
8+
"os": "",
9+
"arch": "",
10+
"version": "go1.18beta2",
11+
"sha256": "3cb14e2c82da366f7393c988f1f3fc2c16b71a5492bd3d49d35886cdf27a9d13",
12+
"size": 22816245,
13+
"kind": "source"
14+
},
15+
{
16+
"filename": "go1.18beta2.darwin-amd64.tar.gz",
17+
"os": "darwin",
18+
"arch": "amd64",
19+
"version": "go1.18beta2",
20+
"sha256": "83fa94b3101d1a00dc5685645e4b49809a5dad18ce19ab449392806a85c05125",
21+
"size": 143466224,
22+
"kind": "archive"
23+
},
24+
{
25+
"filename": "go1.18beta2.darwin-amd64.pkg",
26+
"os": "darwin",
27+
"arch": "amd64",
28+
"version": "go1.18beta2",
29+
"sha256": "fcab5fbe2b9a78bd677c008914df4ee1079178e1c96592b85fa0bd04e2e8e099",
30+
"size": 143951639,
31+
"kind": "installer"
32+
},
33+
{
34+
"filename": "go1.18beta2.darwin-arm64.tar.gz",
35+
"os": "darwin",
36+
"arch": "arm64",
37+
"version": "go1.18beta2",
38+
"sha256": "b442585cbd50759bd11d7a9971e02ec3b2e8cfc1ff1094fa23afadc9e3889689",
39+
"size": 137682754,
40+
"kind": "archive"
41+
},
42+
{
43+
"filename": "go1.18beta2.darwin-arm64.pkg",
44+
"os": "darwin",
45+
"arch": "arm64",
46+
"version": "go1.18beta2",
47+
"sha256": "61425e05dfffce91066bf3625d945baa377c13c659b4ca6fded39bcb391ef2c7",
48+
"size": 138172943,
49+
"kind": "installer"
50+
},
51+
{
52+
"filename": "go1.18beta2.freebsd-386.tar.gz",
53+
"os": "freebsd",
54+
"arch": "386",
55+
"version": "go1.18beta2",
56+
"sha256": "f45899dcc440c9943b3af11a2856959e1ffd9fb11d0dbdbe6398615a96715751",
57+
"size": 112424968,
58+
"kind": "archive"
59+
},
60+
{
61+
"filename": "go1.18beta2.freebsd-amd64.tar.gz",
62+
"os": "freebsd",
63+
"arch": "amd64",
64+
"version": "go1.18beta2",
65+
"sha256": "f5688d21a0e02991445b7992f01fc5fda459c84a1812b3b08c2ada4a98e74b5e",
66+
"size": 141351456,
67+
"kind": "archive"
68+
},
69+
{
70+
"filename": "go1.18beta2.linux-386.tar.gz",
71+
"os": "linux",
72+
"arch": "386",
73+
"version": "go1.18beta2",
74+
"sha256": "74ac524d7d17df606cc74059bf30efce35bb6930f950110dd79cc58ba057f186",
75+
"size": 112609338,
76+
"kind": "archive"
77+
},
78+
{
79+
"filename": "go1.18beta2.linux-amd64.tar.gz",
80+
"os": "linux",
81+
"arch": "amd64",
82+
"version": "go1.18beta2",
83+
"sha256": "b5dacafa59737cfb0d657902b70c2ad1b6bb4ed15e85ea2806f72ce3d4824688",
84+
"size": 141429704,
85+
"kind": "archive"
86+
},
87+
{
88+
"filename": "go1.18beta2.linux-arm64.tar.gz",
89+
"os": "linux",
90+
"arch": "arm64",
91+
"version": "go1.18beta2",
92+
"sha256": "21e4248594401568c2e8704b9d26c6185a61f46b4f17e1a628bf1b5d9a010503",
93+
"size": 108461508,
94+
"kind": "archive"
95+
},
96+
{
97+
"filename": "go1.18beta2.linux-armv6l.tar.gz",
98+
"os": "linux",
99+
"arch": "armv6l",
100+
"version": "go1.18beta2",
101+
"sha256": "bc958a63b51c44762ec026ab587b0261e94cf6337613bfbbcfbd0414fb95f7b6",
102+
"size": 109806288,
103+
"kind": "archive"
104+
},
105+
{
106+
"filename": "go1.18beta2.linux-ppc64le.tar.gz",
107+
"os": "linux",
108+
"arch": "ppc64le",
109+
"version": "go1.18beta2",
110+
"sha256": "573916974201745360883102c80482e1d6730683d1a4a6bb4b469978d4f99d30",
111+
"size": 108654946,
112+
"kind": "archive"
113+
},
114+
{
115+
"filename": "go1.18beta2.linux-s390x.tar.gz",
116+
"os": "linux",
117+
"arch": "s390x",
118+
"version": "go1.18beta2",
119+
"sha256": "860ee3e5cd68fd36d3d49654adc37b9450584d07f0745f7f461080d9cc7749e1",
120+
"size": 111316368,
121+
"kind": "archive"
122+
},
123+
{
124+
"filename": "go1.18beta2.windows-386.zip",
125+
"os": "windows",
126+
"arch": "386",
127+
"version": "go1.18beta2",
128+
"sha256": "666b808f6835f928aa9d8ffa065304af61540b579b4d0eb5c55b7f2a7ec9fff2",
129+
"size": 128931729,
130+
"kind": "archive"
131+
},
132+
{
133+
"filename": "go1.18beta2.windows-386.msi",
134+
"os": "windows",
135+
"arch": "386",
136+
"version": "go1.18beta2",
137+
"sha256": "b3ea5bb064c87e47871488cd15bc99ebd0919529acc97a684e44ef683e459acb",
138+
"size": 113459200,
139+
"kind": "installer"
140+
},
141+
{
142+
"filename": "go1.18beta2.windows-amd64.zip",
143+
"os": "windows",
144+
"arch": "amd64",
145+
"version": "go1.18beta2",
146+
"sha256": "e2a3e0ee984205be36d59f333a7e327ab14517b7c3a11d9b8651e02895ced69a",
147+
"size": 157785083,
148+
"kind": "archive"
149+
},
150+
{
151+
"filename": "go1.18beta2.windows-amd64.msi",
152+
"os": "windows",
153+
"arch": "amd64",
154+
"version": "go1.18beta2",
155+
"sha256": "36df8cad76eff69033b748cf71343c8d3f73b72067d57be52ec4996eb05539e4",
156+
"size": 138551296,
157+
"kind": "installer"
158+
},
159+
{
160+
"filename": "go1.18beta2.windows-arm64.zip",
161+
"os": "windows",
162+
"arch": "arm64",
163+
"version": "go1.18beta2",
164+
"sha256": "9ed027803361cab0b8c338e698e16586d83d973f01815e17d2ab2990e29b8c66",
165+
"size": 123458965,
166+
"kind": "archive"
167+
},
168+
{
169+
"filename": "go1.18beta2.windows-arm64.msi",
170+
"os": "windows",
171+
"arch": "arm64",
172+
"version": "go1.18beta2",
173+
"sha256": "cfbe500daed74dd26e556619a9f0e0b3d20f8a4cf31b48618c4285f47dca1f0c",
174+
"size": 108797952,
175+
"kind": "installer"
176+
}
177+
]
178+
},
2179
{
3180
"version": "go1.13.7",
4181
"stable": true,

__tests__/setup-go.test.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,11 @@ describe('setup-go', () => {
258258
expect(fileName).toBe('go1.13.7.windows-386.zip');
259259
});
260260

261-
it('finds unstable pre-release version', async () => {
261+
it('finds unstable release candidate', async () => {
262262
os.platform = 'linux';
263263
os.arch = 'x64';
264264

265-
// spec: 1.14, stable=false => go1.14rc1
265+
// spec: 1.14.0-rc.1, stable=false => go1.14rc1
266266
const match: im.IGoVersion | undefined = await im.findMatch('1.14.0-rc.1');
267267
expect(match).toBeDefined();
268268
const version: string = match ? match.version : '';
@@ -271,6 +271,47 @@ describe('setup-go', () => {
271271
expect(fileName).toBe('go1.14rc1.linux-amd64.tar.gz');
272272
});
273273

274+
it('finds unstable beta version', async () => {
275+
os.platform = 'linux';
276+
os.arch = 'x64';
277+
278+
// spec: 1.18.0-beta.2, stable=false => go1.18beta2
279+
const match: im.IGoVersion | undefined = await im.findMatch(
280+
'1.18.0-beta.2'
281+
);
282+
expect(match).toBeDefined();
283+
const version: string = match ? match.version : '';
284+
expect(version).toBe('go1.18beta2');
285+
const fileName = match ? match.files[0].filename : '';
286+
expect(fileName).toBe('go1.18beta2.linux-amd64.tar.gz');
287+
});
288+
289+
it('finds unstable release candidate (go version format)', async () => {
290+
os.platform = 'linux';
291+
os.arch = 'x64';
292+
293+
// spec: 1.14rc1, stable=false => go1.14rc1
294+
const match: im.IGoVersion | undefined = await im.findMatch('1.14rc1');
295+
expect(match).toBeDefined();
296+
const version: string = match ? match.version : '';
297+
expect(version).toBe('go1.14rc1');
298+
const fileName = match ? match.files[0].filename : '';
299+
expect(fileName).toBe('go1.14rc1.linux-amd64.tar.gz');
300+
});
301+
302+
it('finds unstable beta version (go version format)', async () => {
303+
os.platform = 'linux';
304+
os.arch = 'x64';
305+
306+
// spec: 1.18beta2, stable=false => go1.18beta2
307+
const match: im.IGoVersion | undefined = await im.findMatch('1.18beta2');
308+
expect(match).toBeDefined();
309+
const version: string = match ? match.version : '';
310+
expect(version).toBe('go1.18beta2');
311+
const fileName = match ? match.files[0].filename : '';
312+
expect(fileName).toBe('go1.18beta2.linux-amd64.tar.gz');
313+
});
314+
274315
it('evaluates to stable with input as true', async () => {
275316
inputs['go-version'] = '1.13.0';
276317
inputs.stable = 'true';

dist/setup/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77438,7 +77438,8 @@ function findMatch(versionSpec_1) {
7743877438
const candidate = candidates[i];
7743977439
const version = makeSemver(candidate.version);
7744077440
core.debug(`check ${version} satisfies ${versionSpec}`);
77441-
if (semver.satisfies(version, versionSpec)) {
77441+
if (candidate.version.replace(/^go/, '') === versionSpec ||
77442+
semver.satisfies(version, versionSpec)) {
7744277443
goFile = candidate.files.find(file => {
7744377444
core.debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
7744477445
return file.arch === archFilter && file.os === platFilter;
@@ -77506,7 +77507,7 @@ function parseGoVersionFile(versionFilePath) {
7750677507
}
7750777508
}
7750877509
// go directive: https://go.dev/ref/mod#go-mod-file-go
77509-
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
77510+
const matchGo = contents.match(/^go (\d+(\.\d+)*(?:\.\d+|(beta|rc)\d+)?)/m);
7751077511
return matchGo ? matchGo[1] : '';
7751177512
}
7751277513
else if (path.basename(versionFilePath) === '.tool-versions') {

src/installer.ts

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

584584
core.debug(`check ${version} satisfies ${versionSpec}`);
585-
if (semver.satisfies(version, versionSpec)) {
585+
if (
586+
candidate.version.replace(/^go/, '') === versionSpec ||
587+
semver.satisfies(version, versionSpec)
588+
) {
586589
goFile = candidate.files.find(file => {
587590
core.debug(
588591
`${file.arch}===${archFilter} && ${file.os}===${platFilter}`
@@ -670,7 +673,7 @@ export function parseGoVersionFile(versionFilePath: string): string {
670673
}
671674

672675
// go directive: https://go.dev/ref/mod#go-mod-file-go
673-
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
676+
const matchGo = contents.match(/^go (\d+(\.\d+)*(?:\.\d+|(beta|rc)\d+)?)/m);
674677
return matchGo ? matchGo[1] : '';
675678
} else if (path.basename(versionFilePath) === '.tool-versions') {
676679
const match = contents.match(/^golang\s+([^\n#]+)/m);

0 commit comments

Comments
 (0)