Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support go.work file for go-version-file #283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion .github/workflows/versions.yml
Expand Up @@ -66,6 +66,22 @@ jobs:
run: __tests__/verify-go.sh 1.14
shell: bash

go-version-file-with-gowork:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v3
- name: Setup Go and check latest
uses: ./
with:
go-version-file: __tests__/data/go.work
- name: verify go
run: __tests__/verify-go.sh 1.19
shell: bash

setup-versions-from-manifest:
name: Setup ${{ matrix.go }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -123,4 +139,4 @@ jobs:
go-version: ${{ matrix.go-version }}
architecture: x64
- name: Verify Go
run: go version
run: go version
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -127,7 +127,7 @@ steps:
```
## Getting go version from the go.mod file

The `go-version-file` input accepts a path to a `go.mod` file containing 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 [version-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. 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 [version-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go servers.

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
Expand Down
3 changes: 3 additions & 0 deletions __tests__/data/go.work
@@ -0,0 +1,3 @@
go 1.19

use .
18 changes: 18 additions & 0 deletions __tests__/setup-go.test.ts
Expand Up @@ -824,6 +824,12 @@ require (
replace example.com/thatmodule => ../thatmodule
exclude example.com/thismodule v1.3.0
`;

const goWorkContents = `go 1.19
use .
`;

it('reads version from go.mod', async () => {
Expand All @@ -838,6 +844,18 @@ exclude example.com/thismodule v1.3.0
expect(logSpy).toHaveBeenCalledWith('matching 1.14...');
});

it('reads version from 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.19');
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.19...');
expect(logSpy).toHaveBeenCalledWith('matching 1.19...');
});

it('reads version from .go-version', async () => {
inputs['go-version-file'] = '.go-version';
existsSpy.mockImplementation(() => true);
Expand Down
6 changes: 3 additions & 3 deletions action.yml
Expand Up @@ -5,7 +5,7 @@ inputs:
go-version:
description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges.'
go-version-file:
description: 'Path to the go.mod file.'
description: 'Path to the go.mod or go.work file.'
check-latest:
description: 'Set this option to true if you want the action to always check for the latest available version that satisfies the version spec'
default: false
Expand All @@ -21,8 +21,8 @@ inputs:
description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.'
outputs:
go-version:
description: 'The installed Go version. Useful when given a version range as input.'
cache-hit:
description: 'The installed Go version. Useful when given a version range as input.'
cache-hit:
description: 'A boolean value to indicate if a cache was hit'
runs:
using: 'node16'
Expand Down
3 changes: 2 additions & 1 deletion dist/setup/index.js
Expand Up @@ -63444,7 +63444,8 @@ function makeSemver(version) {
exports.makeSemver = makeSemver;
function parseGoVersionFile(versionFilePath) {
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') {
const match = contents.match(/^go (\d+(\.\d+)*)/m);
return match ? match[1] : '';
}
Expand Down
5 changes: 4 additions & 1 deletion src/installer.ts
Expand Up @@ -316,7 +316,10 @@ export function makeSemver(version: string): string {
export function parseGoVersionFile(versionFilePath: string): string {
const contents = fs.readFileSync(versionFilePath).toString();

if (path.basename(versionFilePath) === 'go.mod') {
if (
path.basename(versionFilePath) === 'go.mod' ||
path.basename(versionFilePath) === 'go.work'
) {
const match = contents.match(/^go (\d+(\.\d+)*)/m);
return match ? match[1] : '';
}
Expand Down