Skip to content

Commit 145e58d

Browse files
Prefer installing version from toolchain directive
Prefer this over the version from the `go` directive. Per the docs[1] > The toolchain line declares a suggested toolchain to use with the module or workspace It seems reasonable to use this, since running this action in a directory containing a `go.mod` (or `go.work`) suggests the user is wishing to work _with the module or workspace_. See (TODO link issue)
1 parent 763ffeb commit 145e58d

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

__tests__/setup-go.test.ts

+50
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,56 @@ use .
965965
);
966966
});
967967

968+
describe('go-version-file-toolchain', () => {
969+
const goModContents = `module example.com/mymodule
970+
971+
go 1.14
972+
973+
toolchain go1.21.0
974+
975+
require (
976+
example.com/othermodule v1.2.3
977+
example.com/thismodule v1.2.3
978+
example.com/thatmodule v1.2.3
979+
)
980+
981+
replace example.com/thatmodule => ../thatmodule
982+
exclude example.com/thismodule v1.3.0
983+
`;
984+
985+
const goWorkContents = `go 1.19
986+
987+
toolchain go1.21.0
988+
989+
use .
990+
991+
`;
992+
993+
it('reads version from toolchain directive in go.mod', async () => {
994+
inputs['go-version-file'] = 'go.mod';
995+
existsSpy.mockImplementation(() => true);
996+
readFileSpy.mockImplementation(() => Buffer.from(goModContents));
997+
998+
await main.run();
999+
1000+
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.21.0');
1001+
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.21.0...');
1002+
expect(logSpy).toHaveBeenCalledWith('matching 1.21.0...');
1003+
});
1004+
1005+
it('reads version from toolchain directive in go.work', async () => {
1006+
inputs['go-version-file'] = 'go.work';
1007+
existsSpy.mockImplementation(() => true);
1008+
readFileSpy.mockImplementation(() => Buffer.from(goWorkContents));
1009+
1010+
await main.run();
1011+
1012+
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.21.0');
1013+
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.21.0...');
1014+
expect(logSpy).toHaveBeenCalledWith('matching 1.21.0...');
1015+
});
1016+
});
1017+
9681018
it('exports GOTOOLCHAIN and sets it in current process env', async () => {
9691019
inputs['go-version'] = '1.21.0';
9701020
inSpy.mockImplementation(name => inputs[name]);

dist/setup/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -88545,8 +88545,14 @@ function parseGoVersionFile(versionFilePath) {
8854588545
const contents = fs_1.default.readFileSync(versionFilePath).toString();
8854688546
if (path.basename(versionFilePath) === 'go.mod' ||
8854788547
path.basename(versionFilePath) === 'go.work') {
88548-
const match = contents.match(/^go (\d+(\.\d+)*)/m);
88549-
return match ? match[1] : '';
88548+
// toolchain directive: https://go.dev/ref/mod#go-mod-file-toolchain
88549+
const matchToolchain = contents.match(/^toolchain go(\d+(\.\d+)*)/m);
88550+
if (matchToolchain) {
88551+
return matchToolchain[1];
88552+
}
88553+
// go directive: https://go.dev/ref/mod#go-mod-file-go
88554+
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
88555+
return matchGo ? matchGo[1] : '';
8855088556
}
8855188557
return contents.trim();
8855288558
}

src/installer.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,15 @@ export function parseGoVersionFile(versionFilePath: string): string {
424424
path.basename(versionFilePath) === 'go.mod' ||
425425
path.basename(versionFilePath) === 'go.work'
426426
) {
427-
const match = contents.match(/^go (\d+(\.\d+)*)/m);
428-
return match ? match[1] : '';
427+
// toolchain directive: https://go.dev/ref/mod#go-mod-file-toolchain
428+
const matchToolchain = contents.match(/^toolchain go(\d+(\.\d+)*)/m);
429+
if (matchToolchain) {
430+
return matchToolchain[1];
431+
}
432+
433+
// go directive: https://go.dev/ref/mod#go-mod-file-go
434+
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
435+
return matchGo ? matchGo[1] : '';
429436
}
430437

431438
return contents.trim();

0 commit comments

Comments
 (0)