diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 70f2166..415c958 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -265,7 +265,7 @@ describe('setup-go', () => { 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'; inSpy.mockImplementation(name => inputs[name]); @@ -278,7 +278,7 @@ describe('setup-go', () => { }); await main.run(); - expect(vars).toStrictEqual({}); + expect(vars).not.toHaveProperty('GOROOT'); }); it('exports GOROOT for Go versions <1.9', async () => { @@ -294,9 +294,7 @@ describe('setup-go', () => { }); await main.run(); - expect(vars).toStrictEqual({ - GOROOT: toolPath - }); + expect(vars).toHaveProperty('GOROOT', toolPath); }); it('finds a version of go already in the cache', async () => { @@ -966,4 +964,18 @@ use . } ); }); + + 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'); + }); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index f0a94cf..4cbdc47 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -88422,6 +88422,7 @@ const os_1 = __importDefault(__nccwpck_require__(2037)); function run() { return __awaiter(this, void 0, void 0, function* () { try { + setToolchain(); // // 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. @@ -88538,6 +88539,21 @@ function resolveVersionInput() { } 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); +} /***/ }), diff --git a/src/main.ts b/src/main.ts index 690d277..a52951c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,6 +11,7 @@ import os from 'os'; export async function run() { try { + setToolchain(); // // 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. @@ -160,3 +161,20 @@ function resolveVersionInput(): string { 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); +}