Skip to content

Commit faf5242

Browse files
Add support for .tool-versions file in setup-go, update workflow (#673)
* setup go in local * add .tool-versions file support
1 parent 7bc60db commit faf5242

File tree

8 files changed

+63
-14
lines changed

8 files changed

+63
-14
lines changed

.github/workflows/versions.yml

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
21+
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-large]
2222
steps:
2323
- uses: actions/checkout@v5
2424
- name: Setup Go Stable
@@ -33,7 +33,7 @@ jobs:
3333
strategy:
3434
fail-fast: false
3535
matrix:
36-
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
36+
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-large]
3737
steps:
3838
- uses: actions/checkout@v5
3939
- name: Setup Go oldStable
@@ -48,13 +48,13 @@ jobs:
4848
strategy:
4949
fail-fast: false
5050
matrix:
51-
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
51+
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-large]
5252
version: [stable, oldstable]
5353
architecture: [x64, x32]
5454
exclude:
5555
- os: macos-latest
5656
architecture: x32
57-
- os: macos-13
57+
- os: macos-latest-large
5858
architecture: x32
5959
steps:
6060
- uses: actions/checkout@v5
@@ -72,7 +72,7 @@ jobs:
7272
strategy:
7373
fail-fast: false
7474
matrix:
75-
os: [macos-latest, windows-latest, ubuntu-latest, macos-13]
75+
os: [macos-latest, windows-latest, ubuntu-latest, macos-latest-large]
7676
go: [1.21.13, 1.22.8, 1.23.2]
7777
include:
7878
- os: windows-latest
@@ -98,7 +98,7 @@ jobs:
9898
strategy:
9999
fail-fast: false
100100
matrix:
101-
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
101+
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-large]
102102
go-version: ['1.20', '1.21', '1.22', '1.23']
103103
steps:
104104
- uses: actions/checkout@v5
@@ -115,7 +115,7 @@ jobs:
115115
strategy:
116116
fail-fast: false
117117
matrix:
118-
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
118+
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-large]
119119
steps:
120120
- uses: actions/checkout@v5
121121
- name: Setup Go and check latest
@@ -131,7 +131,7 @@ jobs:
131131
strategy:
132132
fail-fast: false
133133
matrix:
134-
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
134+
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-large]
135135
steps:
136136
- uses: actions/checkout@v5
137137
- name: Setup Go and check latest
@@ -142,12 +142,28 @@ jobs:
142142
run: __tests__/verify-go.sh 1.21
143143
shell: bash
144144

145+
go-version-file-with-tool-versions:
146+
runs-on: ${{ matrix.os }}
147+
strategy:
148+
fail-fast: false
149+
matrix:
150+
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-large]
151+
steps:
152+
- uses: actions/checkout@v5
153+
- name: Setup Go and check latest
154+
uses: ./
155+
with:
156+
go-version-file: __tests__/data/.tool-versions
157+
- name: verify go
158+
run: __tests__/verify-go.sh 1.23.2
159+
shell: bash
160+
145161
setup-versions-from-manifest:
146162
runs-on: ${{ matrix.os }}
147163
strategy:
148164
fail-fast: false
149165
matrix:
150-
os: [macos-latest, windows-latest, ubuntu-latest, macos-13]
166+
os: [macos-latest, windows-latest, ubuntu-latest, macos-latest-large]
151167
go: [1.20.14, 1.21.10, 1.22.8, 1.23.2]
152168
steps:
153169
- name: Checkout
@@ -167,7 +183,7 @@ jobs:
167183
strategy:
168184
fail-fast: false
169185
matrix:
170-
os: [windows-latest, ubuntu-latest, macos-13]
186+
os: [windows-latest, ubuntu-latest, macos-latest-large]
171187
go: [1.11.12]
172188
steps:
173189
- name: Checkout
@@ -187,7 +203,7 @@ jobs:
187203
strategy:
188204
fail-fast: false
189205
matrix:
190-
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
206+
os: [ubuntu-latest, windows-latest, macos-latest, macos-latest-large]
191207
go-version: [1.20.14, 1.21, 1.22, 1.23]
192208
include:
193209
- os: macos-latest
@@ -196,7 +212,7 @@ jobs:
196212
architecture: x64
197213
- os: windows-latest
198214
architecture: x64
199-
- os: macos-13
215+
- os: macos-latest-large
200216
architecture: x64
201217
steps:
202218
- uses: actions/checkout@v5

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ steps:
178178

179179
## Getting go version from the go.mod file
180180

181-
The `go-version-file` input accepts a path to a `go.mod` file or a `go.work`
181+
The `go-version-file` input accepts a path to a `go.mod` file, `.tool-versions` file or a `go.work`
182182
file that contains the version of Go to be used by a project. The version taken
183183
from thils file will be:
184184

@@ -203,6 +203,15 @@ steps:
203203
go-version-file: 'path/to/go.mod'
204204
- run: go version
205205
```
206+
```yaml
207+
steps:
208+
- uses: actions/checkout@v5
209+
- uses: actions/setup-go@v6
210+
with:
211+
go-version-file: '.tool-versions'
212+
- run: go version
213+
```
214+
> The [.tool-versions file](https://asdf-vm.com/manage/configuration.html#tool-versions) supports version specifications in accordance with asdf standards, adhering to Semantic Versioning ([semver](https://semver.org)).
206215

207216
## Matrix testing
208217

__tests__/data/.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
golang 1.23.2

__tests__/setup-go.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,9 @@ exclude example.com/thismodule v1.3.0
866866
867867
use .
868868
869+
`;
870+
871+
const toolVersionsContents = `golang 1.23
869872
`;
870873

871874
it('reads version from go.mod', async () => {
@@ -892,6 +895,18 @@ use .
892895
expect(logSpy).toHaveBeenCalledWith('matching 1.19...');
893896
});
894897

898+
it('reads version from .tool-versions', async () => {
899+
inputs['go-version-file'] = '.tool-versions';
900+
existsSpy.mockImplementation(() => true);
901+
readFileSpy.mockImplementation(() => Buffer.from(toolVersionsContents));
902+
903+
await main.run();
904+
905+
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.23');
906+
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.23...');
907+
expect(logSpy).toHaveBeenCalledWith('matching 1.23...');
908+
});
909+
895910
it('reads version from .go-version', async () => {
896911
inputs['go-version-file'] = '.go-version';
897912
existsSpy.mockImplementation(() => true);

action.yml

Lines changed: 1 addition & 1 deletion
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. Be sure to enclose this option in single quotation marks.'
77
go-version-file:
8-
description: 'Path to the go.mod or go.work file.'
8+
description: 'Path to the go.mod, .tool-versions, 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

dist/setup/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94679,6 +94679,10 @@ function parseGoVersionFile(versionFilePath) {
9467994679
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
9468094680
return matchGo ? matchGo[1] : '';
9468194681
}
94682+
else if (path.basename(versionFilePath) === '.tool-versions') {
94683+
const match = contents.match(/^golang\s+([^\n#]+)/m);
94684+
return match ? match[1].trim() : '';
94685+
}
9468294686
return contents.trim();
9468394687
}
9468494688
function resolveStableVersionDist(versionSpec, arch) {

src/cache-save.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ process.on('uncaughtException', e => {
1515
// Added early exit to resolve issue with slow post action step:
1616
// - https://github.com/actions/setup-node/issues/878
1717
// https://github.com/actions/cache/pull/1217
18+
1819
export async function run(earlyExit?: boolean) {
1920
try {
2021
const cacheInput = core.getBooleanInput('cache');

src/installer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,9 @@ export function parseGoVersionFile(versionFilePath: string): string {
513513
// go directive: https://go.dev/ref/mod#go-mod-file-go
514514
const matchGo = contents.match(/^go (\d+(\.\d+)*)/m);
515515
return matchGo ? matchGo[1] : '';
516+
} else if (path.basename(versionFilePath) === '.tool-versions') {
517+
const match = contents.match(/^golang\s+([^\n#]+)/m);
518+
return match ? match[1].trim() : '';
516519
}
517520

518521
return contents.trim();

0 commit comments

Comments
 (0)