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_.

Link: https://go.dev/doc/toolchain#config [1]
Issue: https://github.com/actions/setup-go/issues/457
This commit is contained in:
Matthew Hughes 2024-03-03 09:48:10 +00:00
parent cae311c529
commit 0348eaa37c
4 changed files with 75 additions and 6 deletions

View File

@ -191,9 +191,15 @@ steps:
## Getting go version from the go.mod file ## Getting go version from the go.mod file
The `go-version-file` input accepts a path to a `go.mod` file or a `go.work` file that contains the version of Go to be used by a project. The `go-version-file` input accepts a path to a `go.mod` file or a `go.work`
file that contains the version of Go to be used by a project. The version taken
from thils file will be:
- The version from the `toolchain` directive, if there is one, otherwise
- The version from the `go` directive
The version can specify a patch version or omit it altogether (e.g., `go 1.22.0` or `go 1.22`).
The `go` directive in `go.mod` can specify a patch version or omit it altogether (e.g., `go 1.22.0` or `go 1.22`).
If a patch version is specified, that specific patch version will be used. If a patch version is specified, that specific patch version will be used.
If no patch version is specified, it will search for the latest available patch version in the cache, If no patch version is specified, it will search for the latest available patch version in the cache,
[versions-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json), and the [versions-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json), and the

View File

@ -988,6 +988,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 () => { it('exports GOTOOLCHAIN and sets it in current process env', async () => {
inputs['go-version'] = '1.21.0'; inputs['go-version'] = '1.21.0';
inSpy.mockImplementation(name => inputs[name]); inSpy.mockImplementation(name => inputs[name]);

10
dist/setup/index.js vendored
View File

@ -94663,8 +94663,14 @@ function parseGoVersionFile(versionFilePath) {
const contents = fs_1.default.readFileSync(versionFilePath).toString(); const contents = fs_1.default.readFileSync(versionFilePath).toString();
if (path.basename(versionFilePath) === 'go.mod' || if (path.basename(versionFilePath) === 'go.mod' ||
path.basename(versionFilePath) === 'go.work') { path.basename(versionFilePath) === 'go.work') {
const match = contents.match(/^go (\d+(\.\d+)*)/m); // toolchain directive: https://go.dev/ref/mod#go-mod-file-toolchain
return match ? match[1] : ''; 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(); return contents.trim();
} }

View File

@ -495,8 +495,15 @@ export function parseGoVersionFile(versionFilePath: string): string {
path.basename(versionFilePath) === 'go.mod' || path.basename(versionFilePath) === 'go.mod' ||
path.basename(versionFilePath) === 'go.work' path.basename(versionFilePath) === 'go.work'
) { ) {
const match = contents.match(/^go (\d+(\.\d+)*)/m); // toolchain directive: https://go.dev/ref/mod#go-mod-file-toolchain
return match ? match[1] : ''; 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(); return contents.trim();