Merge 0348eaa37c95bbfed367b891deb3011c8b1decfd into e75c3e80bcde46e322cd5845ae46582947c9becd

This commit is contained in:
Matthew Hughes 2025-08-17 10:14:26 +00:00 committed by GitHub
commit 168454f13b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 126 additions and 11 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

@ -285,7 +285,7 @@ describe('setup-go', () => {
expect(logSpy).toHaveBeenCalledWith(`Setup go version spec 1.13.0`); expect(logSpy).toHaveBeenCalledWith(`Setup go version spec 1.13.0`);
}); });
it('does not export any variables for Go versions >=1.9', async () => { it('does not export GOROOT for Go versions >=1.9', async () => {
inputs['go-version'] = '1.13.0'; inputs['go-version'] = '1.13.0';
inSpy.mockImplementation(name => inputs[name]); inSpy.mockImplementation(name => inputs[name]);
@ -298,7 +298,7 @@ describe('setup-go', () => {
}); });
await main.run(); await main.run();
expect(vars).toStrictEqual({}); expect(vars).not.toHaveProperty('GOROOT');
}); });
it('exports GOROOT for Go versions <1.9', async () => { it('exports GOROOT for Go versions <1.9', async () => {
@ -314,9 +314,7 @@ describe('setup-go', () => {
}); });
await main.run(); await main.run();
expect(vars).toStrictEqual({ expect(vars).toHaveProperty('GOROOT', toolPath);
GOROOT: toolPath
});
}); });
it('finds a version of go already in the cache', async () => { it('finds a version of go already in the cache', async () => {
@ -989,4 +987,68 @@ 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]);
const vars: {[key: string]: string} = {};
exportVarSpy.mockImplementation((name: string, val: string) => {
vars[name] = val;
});
await main.run();
expect(vars).toStrictEqual({GOTOOLCHAIN: 'local'});
expect(process.env).toHaveProperty('GOTOOLCHAIN', 'local');
});
}); });

26
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();
} }
@ -94777,6 +94783,7 @@ const os_1 = __importDefault(__nccwpck_require__(2037));
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
setToolchain();
// //
// versionSpec is optional. If supplied, install / use from the tool cache // versionSpec is optional. If supplied, install / use from the tool cache
// If not supplied then problem matchers will still be setup. Useful for self-hosted. // If not supplied then problem matchers will still be setup. Useful for self-hosted.
@ -94890,6 +94897,21 @@ function resolveVersionInput() {
} }
return version; return version;
} }
function setToolchain() {
// docs: https://go.dev/doc/toolchain
// "local indicates the bundled Go toolchain (the one that shipped with the go command being run)"
// this is so any 'go' command is run with the selected Go version
// and doesn't trigger a toolchain download and run commands with that
// see e.g. issue #424
// and a similar discussion: https://github.com/docker-library/golang/issues/472
const toolchain = 'local';
const toolchainVar = 'GOTOOLCHAIN';
// set the value in process env so any `go` commands run as child-process
// don't cause toolchain downloads
process.env[toolchainVar] = toolchain;
// and in the runner env so e.g. a user running `go mod tidy` won't cause it
core.exportVariable(toolchainVar, toolchain);
}
/***/ }), /***/ }),

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();

View File

@ -11,6 +11,7 @@ import os from 'os';
export async function run() { export async function run() {
try { try {
setToolchain();
// //
// versionSpec is optional. If supplied, install / use from the tool cache // versionSpec is optional. If supplied, install / use from the tool cache
// If not supplied then problem matchers will still be setup. Useful for self-hosted. // If not supplied then problem matchers will still be setup. Useful for self-hosted.
@ -160,3 +161,20 @@ function resolveVersionInput(): string {
return version; return version;
} }
function setToolchain() {
// docs: https://go.dev/doc/toolchain
// "local indicates the bundled Go toolchain (the one that shipped with the go command being run)"
// this is so any 'go' command is run with the selected Go version
// and doesn't trigger a toolchain download and run commands with that
// see e.g. issue #424
// and a similar discussion: https://github.com/docker-library/golang/issues/472
const toolchain = 'local';
const toolchainVar = 'GOTOOLCHAIN';
// set the value in process env so any `go` commands run as child-process
// don't cause toolchain downloads
process.env[toolchainVar] = toolchain;
// and in the runner env so e.g. a user running `go mod tidy` won't cause it
core.exportVariable(toolchainVar, toolchain);
}