Skip to content

Commit 9fbf6fb

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 4dc6199 commit 9fbf6fb

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
@@ -252,11 +252,11 @@ describe('setup-go', () => {
252252
expect(fileName).toBe('go1.13.7.windows-386.zip');
253253
});
254254

255-
it('finds unstable pre-release version', async () => {
255+
it('finds unstable release candidate', async () => {
256256
os.platform = 'linux';
257257
os.arch = 'x64';
258258

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

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