0
1
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Matthew Hughes bcf3c31df0
Merge be5f1f14e1 into cdcb360436 2024-04-19 05:53:29 +03:00
Masahiro Furudate cdcb360436
Remove the description of the old go.mod specification (#458)
* Fix emoji rendering

* Fix quoting

* Remove the description of the old go.mod specification

* Remove the single quotes from `go-version-file`

* Fix README

* Add description about patch versions to README

* Revert "Remove the single quotes from `go-version-file`"

This reverts commit ca4321abee.
2024-04-18 08:33:57 -05:00
Matthew Hughes be5f1f14e1 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)
2024-03-03 09:48:10 +00:00
Matthew Hughes f54e8eea6b Configure environment to avoid toolchain installs
Force `go` to always use the local toolchain (i.e. the one the one that
shipped with the go command being run) via setting the `GOTOOLCHAIN`
environment variable to `local`[1]:

> When GOTOOLCHAIN is set to local, the go command always runs the
bundled Go toolchain.

This is how things are setup in the official Docker images (e.g.[2], see
also the discussion around that change[3]). The motivation behind this
is to:

* Reduce duplicate work, the action will install a version of Go, a
  toolchain will be detected, the toolchain will be detected and then
  another version of Go installed
* Avoid Unexpected behaviour: if you specify this action runs with some Go
  version (e.g. `1.21.0`) but your go.mod contains a `toolchain` or `go`
  directive for a newer version (e.g. `1.22.0`) then, without any other
  configuration/environment setup, any go commands will be run using go
  `1.22.0`
* TODO: link image

This will be a **breaking change** for some workflows. Given a `go.mod`
like:

    module proj

    go 1.22.0

Then running any `go` command, e.g. `go mod tidy`, in an environment
where only go versions before `1.22.0` were installed would previously
trigger a toolchain download of Go `1.22.0` and that version being used
to execute the command. With this change the above would error out with
something like:

> go: go.mod requires go >= 1.22.0 (running go 1.21.7;
GOTOOLCHAIN=local)

[1] https://go.dev/doc/toolchain#select
[2] dae3405a32/Dockerfile-linux.template (L163)
[3] https://github.com/docker-library/golang/issues/472
2024-03-03 09:06:34 +00:00
6 changed files with 131 additions and 19 deletions

View File

@ -70,9 +70,10 @@ steps:
>
> ```yaml
> go-version: '1.20'
> ```
> ```
>
> The recommendation is based on the YAML parser's behavior, which interprets non-wrapped values as numbers and, in the case of version 1.20, trims it down to 1.2, which may not be very obvious.
Matching an unstable pre-release:
```yaml
@ -190,11 +191,13 @@ steps:
## 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. As the `go.mod` file contains only major and minor (e.g. 1.18) tags, the action will search for the
latest available patch version sequentially in the runner's directory with the cached tools, in
the [versions-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go
servers.
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` 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 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
[official Go language website](https://golang.org/dl/?mode=json&include=all), in that order.
If both the `go-version` and the `go-version-file` inputs are provided then the `go-version` input is used.
> The action will search for the `go.mod` file relative to the repository root

View File

@ -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,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

@ -88329,8 +88329,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();
}
@ -88433,6 +88439,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.
@ -88549,6 +88556,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);
}
/***/ }),

View File

@ -6,13 +6,13 @@ We have prepared a short guide so that the process of making your contribution i
## How can I contribute...
* [Contribute Documentation:green_book:](#contribute-documentation)
* [Contribute Documentation :green_book:](#contribute-documentation)
* [Contribute Code :computer:](#contribute-code)
* [Provide Support on Issues:pencil:](#provide-support-on-issues)
* [Provide Support on Issues :pencil:](#provide-support-on-issues)
* [Review Pull Requests:mag:](#review-pull-requests)
* [Review Pull Requests :mag:](#review-pull-requests)
## Contribute documentation
@ -113,4 +113,4 @@ Another great way to contribute is pull request reviews. Please, be extra kind:
- Make sure you're familiar with the code or documentation is updated, unless it's a minor change (spellchecking, minor formatting, etc.)
- Review changes using the GitHub functionality. You can ask a clarifying question, point out an error or suggest an alternative.
> Note: You may ask for minor changes - "nitpicks", but consider whether they are real blockers to merging or not
- Submit your review, which may include comments, an approval, or a changes request
- Submit your review, which may include comments, an approval, or a changes request

View File

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

View File

@ -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);
}