From be5f1f14e13428058f4b1038857ca3804a607cd2 Mon Sep 17 00:00:00 2001 From: Matthew Hughes Date: Sun, 3 Mar 2024 09:48:10 +0000 Subject: [PATCH] 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) --- __tests__/setup-go.test.ts | 50 ++++++++++++++++++++++++++++++++++++++ dist/setup/index.js | 10 ++++++-- src/installer.ts | 11 +++++++-- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 415c958..ee2df59 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -965,6 +965,56 @@ use . ); }); + describe('go-version-file-toolchain', () => { + const goModContents = `module example.com/mymodule + +go 1.14 + +toolchain go1.21.0 + +require ( + example.com/othermodule v1.2.3 + example.com/thismodule v1.2.3 + example.com/thatmodule v1.2.3 +) + +replace example.com/thatmodule => ../thatmodule +exclude example.com/thismodule v1.3.0 +`; + + const goWorkContents = `go 1.19 + +toolchain go1.21.0 + +use . + +`; + + it('reads version from toolchain directive in go.mod', async () => { + inputs['go-version-file'] = 'go.mod'; + existsSpy.mockImplementation(() => true); + readFileSpy.mockImplementation(() => Buffer.from(goModContents)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.21.0'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.21.0...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.21.0...'); + }); + + it('reads version from toolchain directive in go.work', async () => { + inputs['go-version-file'] = 'go.work'; + existsSpy.mockImplementation(() => true); + readFileSpy.mockImplementation(() => Buffer.from(goWorkContents)); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.21.0'); + expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.21.0...'); + expect(logSpy).toHaveBeenCalledWith('matching 1.21.0...'); + }); + }); + it('exports GOTOOLCHAIN and sets it in current process env', async () => { inputs['go-version'] = '1.21.0'; inSpy.mockImplementation(name => inputs[name]); diff --git a/dist/setup/index.js b/dist/setup/index.js index 4cbdc47..bde7044 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -88318,8 +88318,14 @@ function parseGoVersionFile(versionFilePath) { const contents = fs_1.default.readFileSync(versionFilePath).toString(); if (path.basename(versionFilePath) === 'go.mod' || path.basename(versionFilePath) === 'go.work') { - const match = contents.match(/^go (\d+(\.\d+)*)/m); - return match ? match[1] : ''; + // toolchain directive: https://go.dev/ref/mod#go-mod-file-toolchain + const matchToolchain = contents.match(/^toolchain go(\d+(\.\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); + return matchGo ? matchGo[1] : ''; } return contents.trim(); } diff --git a/src/installer.ts b/src/installer.ts index 817c334..96be79b 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -424,8 +424,15 @@ export function parseGoVersionFile(versionFilePath: string): string { path.basename(versionFilePath) === 'go.mod' || path.basename(versionFilePath) === 'go.work' ) { - const match = contents.match(/^go (\d+(\.\d+)*)/m); - return match ? match[1] : ''; + // toolchain directive: https://go.dev/ref/mod#go-mod-file-toolchain + const matchToolchain = contents.match(/^toolchain go(\d+(\.\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); + return matchGo ? matchGo[1] : ''; } return contents.trim();