Skip to content

Commit 8fb6887

Browse files
committed
add support gowork for go-version-file
1 parent c4a742c commit 8fb6887

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

.github/workflows/versions.yml

+17-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ jobs:
6666
run: __tests__/verify-go.sh 1.14
6767
shell: bash
6868

69+
go-version-file-with-gowork:
70+
runs-on: ${{ matrix.os }}
71+
strategy:
72+
fail-fast: false
73+
matrix:
74+
os: [ubuntu-latest, windows-latest, macos-latest]
75+
steps:
76+
- uses: actions/checkout@v3
77+
- name: Setup Go and check latest
78+
uses: ./
79+
with:
80+
go-version-file: __tests__/data/go.work
81+
- name: verify go
82+
run: __tests__/verify-go.sh 1.19
83+
shell: bash
84+
6985
setup-versions-from-manifest:
7086
name: Setup ${{ matrix.go }} ${{ matrix.os }}
7187
runs-on: ${{ matrix.os }}
@@ -123,4 +139,4 @@ jobs:
123139
go-version: ${{ matrix.go-version }}
124140
architecture: x64
125141
- name: Verify Go
126-
run: go version
142+
run: go version

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ steps:
127127
```
128128
## Getting go version from the go.mod file
129129

130-
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.
130+
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.
131131

132132
If both the `go-version` and the `go-version-file` inputs are provided then the `go-version` input is used.
133133
> The action will search for the `go.mod` file relative to the repository root

__tests__/data/go.work

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go 1.19
2+
3+
use .

__tests__/setup-go.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,12 @@ require (
824824
825825
replace example.com/thatmodule => ../thatmodule
826826
exclude example.com/thismodule v1.3.0
827+
`;
828+
829+
const goWorkContents = `go 1.19
830+
831+
use .
832+
827833
`;
828834

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

847+
it('reads version from go.work', async () => {
848+
inputs['go-version-file'] = 'go.work';
849+
existsSpy.mockImplementation(() => true);
850+
readFileSpy.mockImplementation(() => Buffer.from(goWorkContents));
851+
852+
await main.run();
853+
854+
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.19');
855+
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.19...');
856+
expect(logSpy).toHaveBeenCalledWith('matching 1.19...');
857+
});
858+
841859
it('reads version from .go-version', async () => {
842860
inputs['go-version-file'] = '.go-version';
843861
existsSpy.mockImplementation(() => true);

action.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inputs:
55
go-version:
66
description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges.'
77
go-version-file:
8-
description: 'Path to the go.mod file.'
8+
description: 'Path to the go.mod or go.work file.'
99
check-latest:
1010
description: 'Set this option to true if you want the action to always check for the latest available version that satisfies the version spec'
1111
default: false
@@ -21,8 +21,8 @@ inputs:
2121
description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.'
2222
outputs:
2323
go-version:
24-
description: 'The installed Go version. Useful when given a version range as input.'
25-
cache-hit:
24+
description: 'The installed Go version. Useful when given a version range as input.'
25+
cache-hit:
2626
description: 'A boolean value to indicate if a cache was hit'
2727
runs:
2828
using: 'node16'

src/installer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export function makeSemver(version: string): string {
316316
export function parseGoVersionFile(versionFilePath: string): string {
317317
const contents = fs.readFileSync(versionFilePath).toString();
318318

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

0 commit comments

Comments
 (0)