Skip to content

Commit a5f9b05

Browse files
Update default Go module caching to use go.mod (#705)
* Update module cache to use go.mod as key * Fix typo * Revise breaking changes in README for V6 Updated breaking changes section with enhanced formatting and clarified toolchain management details.
1 parent 7a3fe6c commit a5f9b05

File tree

8 files changed

+77
-80
lines changed

8 files changed

+77
-80
lines changed

README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,18 @@ steps:
2424
2525
### V6 Changes
2626
27-
#### Node Runtime Upgrade
27+
**Node Runtime Upgrade**
2828
- **Upgraded from Node 20 to Node 24**
2929
- ⚠️ **Action Required**: Ensure your runner is on version v2.327.1 or later for compatibility
3030
- See [Release Notes](https://github.com/actions/runner/releases/tag/v2.327.1) for more details
3131
32-
#### Enhanced Go Toolchain Management
32+
**Enhanced Go Toolchain Management**
3333
34-
V6 introduces significant improvements for reliable and consistent Go version selection:
34+
V6 introduces significant improvements for reliable and consistent Go version selection. Supports both `go` and `toolchain` directives in `go.mod`. If the `toolchain` directive is present, its version is used; otherwise, the action falls back to the go directive.
35+
36+
**Cache Key Update**
3537

36-
**Toolchain Directive Support**
37-
Now correctly interprets both `go` and `toolchain` directives from `go.mod`:
38-
```go
39-
go 1.23.0 // Minimum required version
40-
toolchain go1.23.2 // V6 uses this exact version
41-
```
42-
43-
**Intelligent Caching**
44-
Cache keys now incorporate the `toolchain` directive version from `go.mod`, eliminating cache conflicts when switching between different toolchain versions within the same Go minor release.
38+
By default, caching for Go modules now relies on `go.mod`. To use `go.sum`, configure the `cache-dependency-path` input.
4539

4640
For more details, see the [full release notes](https://github.com/actions/setup-go/releases/tag/v6.0.0).
4741

@@ -257,7 +251,7 @@ The action features integrated caching for Go modules and build outputs. Built o
257251

258252
#### Automatic Caching
259253

260-
Default behavior: Searches for `go.sum` in the repository root and uses its hash for the cache key.
254+
Default behavior: Searches for `go.mod` in the repository root and uses its hash for the cache key.
261255

262256
```yaml
263257
steps:
@@ -368,7 +362,7 @@ jobs:
368362
path: |
369363
${{ env.GO_MOD_CACHE }}
370364
${{ env.GO_BUILD_CACHE }}
371-
key: setup-go-${{ runner.os }}-${{ env.ARCH }}-${{ env.CACHE_OS_SUFFIX }}go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('**/go.sum') }}
365+
key: setup-go-${{ runner.os }}-${{ env.ARCH }}-${{ env.CACHE_OS_SUFFIX }}go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('**/go.mod') }}
372366
- name: Download modules
373367
run: go mod download
374368
- name: Build
@@ -483,4 +477,4 @@ Contributions are welcome! See our [Contributor's Guide](docs/contributors.md) f
483477

484478
## Code of Conduct
485479

486-
👋 Be nice. See our [Code of Conduct](CODE_OF_CONDUCT.md).
480+
👋 Be nice. See our [Code of Conduct](CODE_OF_CONDUCT.md).

__tests__/cache-restore.test.ts

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,102 @@
11
import * as cache from '@actions/cache';
22
import * as core from '@actions/core';
33
import * as glob from '@actions/glob';
4+
import fs from 'fs';
45

56
import * as cacheRestore from '../src/cache-restore';
67
import * as cacheUtils from '../src/cache-utils';
78
import {PackageManagerInfo} from '../src/package-managers';
89

910
describe('restoreCache', () => {
10-
//Arrange
11-
const hashFilesSpy = jest.spyOn(glob, 'hashFiles');
12-
const getCacheDirectoryPathSpy = jest.spyOn(
13-
cacheUtils,
14-
'getCacheDirectoryPath'
15-
);
16-
const restoreCacheSpy = jest.spyOn(cache, 'restoreCache');
17-
const infoSpy = jest.spyOn(core, 'info');
18-
const setOutputSpy = jest.spyOn(core, 'setOutput');
11+
let hashFilesSpy: jest.SpyInstance;
12+
let getCacheDirectoryPathSpy: jest.SpyInstance;
13+
let restoreCacheSpy: jest.SpyInstance;
14+
let infoSpy: jest.SpyInstance;
15+
let setOutputSpy: jest.SpyInstance;
1916

2017
const versionSpec = '1.13.1';
2118
const packageManager = 'default';
2219
const cacheDependencyPath = 'path';
2320

21+
let originalWorkspace: string | undefined;
22+
2423
beforeEach(() => {
24+
originalWorkspace = process.env.GITHUB_WORKSPACE;
25+
process.env.GITHUB_WORKSPACE = '/test/workspace';
26+
//Arrange
27+
hashFilesSpy = jest.spyOn(glob, 'hashFiles');
28+
getCacheDirectoryPathSpy = jest.spyOn(cacheUtils, 'getCacheDirectoryPath');
29+
restoreCacheSpy = jest.spyOn(cache, 'restoreCache');
30+
infoSpy = jest.spyOn(core, 'info');
31+
setOutputSpy = jest.spyOn(core, 'setOutput');
32+
2533
getCacheDirectoryPathSpy.mockImplementation(
2634
(PackageManager: PackageManagerInfo) => {
27-
return new Promise<string[]>(resolve => {
28-
resolve(['cache_directory_path', 'cache_directory_path']);
29-
});
35+
return Promise.resolve([
36+
'cache_directory_path',
37+
'cache_directory_path'
38+
]);
3039
}
3140
);
3241
});
3342

34-
it('should throw if dependency file path is not valid', async () => {
35-
//Arrange
36-
hashFilesSpy.mockImplementation((somePath: string) => {
37-
return new Promise<string>(resolve => {
38-
resolve('');
39-
});
40-
});
43+
afterEach(() => {
44+
process.env.GITHUB_WORKSPACE = originalWorkspace;
45+
jest.restoreAllMocks();
46+
});
4147

42-
//Act + Assert
43-
await expect(async () => {
44-
await cacheRestore.restoreCache(
48+
it('should throw if dependency file path is not valid', async () => {
49+
// Arrange
50+
hashFilesSpy.mockImplementation(() => Promise.resolve(''));
51+
// Act + Assert
52+
await expect(
53+
cacheRestore.restoreCache(
4554
versionSpec,
4655
packageManager,
4756
cacheDependencyPath
48-
);
49-
}).rejects.toThrow(
57+
)
58+
).rejects.toThrow(
5059
'Some specified paths were not resolved, unable to cache dependencies.'
5160
);
5261
});
5362

54-
it('should inform if cache hit is not occured', async () => {
55-
//Arrange
56-
hashFilesSpy.mockImplementation((somePath: string) => {
57-
return new Promise<string>(resolve => {
58-
resolve('file_hash');
59-
});
60-
});
61-
62-
restoreCacheSpy.mockImplementation(() => {
63-
return new Promise<string>(resolve => {
64-
resolve('');
65-
});
66-
});
67-
68-
//Act + Assert
63+
it('should inform if cache hit is not occurred', async () => {
64+
// Arrange
65+
hashFilesSpy.mockImplementation(() => Promise.resolve('file_hash'));
66+
restoreCacheSpy.mockImplementation(() => Promise.resolve(''));
67+
// Act + Assert
6968
await cacheRestore.restoreCache(
7069
versionSpec,
7170
packageManager,
7271
cacheDependencyPath
7372
);
74-
expect(infoSpy).toHaveBeenCalledWith(`Cache is not found`);
73+
expect(infoSpy).toHaveBeenCalledWith('Cache is not found');
7574
});
7675

77-
it('should set output if cache hit is occured', async () => {
78-
//Arrange
79-
hashFilesSpy.mockImplementation((somePath: string) => {
80-
return new Promise<string>(resolve => {
81-
resolve('file_hash');
82-
});
83-
});
84-
85-
restoreCacheSpy.mockImplementation(() => {
86-
return new Promise<string>(resolve => {
87-
resolve('cache_key');
88-
});
89-
});
90-
91-
//Act + Assert
76+
it('should set output if cache hit is occurred', async () => {
77+
// Arrange
78+
hashFilesSpy.mockImplementation(() => Promise.resolve('file_hash'));
79+
restoreCacheSpy.mockImplementation(() => Promise.resolve('cache_key'));
80+
// Act + Assert
9281
await cacheRestore.restoreCache(
9382
versionSpec,
9483
packageManager,
9584
cacheDependencyPath
9685
);
9786
expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
9887
});
88+
89+
it('should throw if dependency file is not found in workspace', async () => {
90+
jest.spyOn(fs, 'readdirSync').mockReturnValue(['main.go'] as any);
91+
92+
await expect(
93+
cacheRestore.restoreCache(
94+
versionSpec,
95+
packageManager
96+
// No cacheDependencyPath
97+
)
98+
).rejects.toThrow(
99+
'Dependencies file is not found in /test/workspace. Supported file pattern: go.mod'
100+
);
101+
});
99102
});

__tests__/cache-utils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('getPackageManagerInfo', () => {
4747
//Arrange
4848
const packageManagerName = 'default';
4949
const expectedResult = {
50-
dependencyFilePattern: 'go.sum',
50+
dependencyFilePattern: 'go.mod',
5151
cacheFolderCommandList: ['go env GOMODCACHE', 'go env GOCACHE']
5252
};
5353

@@ -73,7 +73,7 @@ describe('getCacheDirectoryPath', () => {
7373
const getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
7474

7575
const validPackageManager: PackageManagerInfo = {
76-
dependencyFilePattern: 'go.sum',
76+
dependencyFilePattern: 'go.mod',
7777
cacheFolderCommandList: ['go env GOMODCACHE', 'go env GOCACHE']
7878
};
7979

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ inputs:
1616
description: Used to specify whether caching is needed. Set to true, if you'd like to enable caching.
1717
default: true
1818
cache-dependency-path:
19-
description: 'Used to specify the path to a dependency file - go.sum'
19+
description: 'Used to specify the path to a dependency file (e.g., go.mod, go.sum)'
2020
architecture:
2121
description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.'
2222
outputs:

dist/cache-save/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44277,7 +44277,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
4427744277
exports.supportedPackageManagers = void 0;
4427844278
exports.supportedPackageManagers = {
4427944279
default: {
44280-
dependencyFilePattern: 'go.sum',
44280+
dependencyFilePattern: 'go.mod',
4428144281
cacheFolderCommandList: ['go env GOMODCACHE', 'go env GOCACHE']
4428244282
}
4428344283
};

dist/setup/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49364,8 +49364,8 @@ const findDependencyFile = (packageManager) => {
4936449364
const dependencyFile = packageManager.dependencyFilePattern;
4936549365
const workspace = process.env.GITHUB_WORKSPACE;
4936649366
const rootContent = fs_1.default.readdirSync(workspace);
49367-
const goSumFileExists = rootContent.includes(dependencyFile);
49368-
if (!goSumFileExists) {
49367+
const goModFileExists = rootContent.includes(dependencyFile);
49368+
if (!goModFileExists) {
4936949369
throw new Error(`Dependencies file is not found in ${workspace}. Supported file pattern: ${dependencyFile}`);
4937049370
}
4937149371
return path_1.default.join(workspace, dependencyFile);
@@ -50182,7 +50182,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
5018250182
exports.supportedPackageManagers = void 0;
5018350183
exports.supportedPackageManagers = {
5018450184
default: {
50185-
dependencyFilePattern: 'go.sum',
50185+
dependencyFilePattern: 'go.mod',
5018650186
cacheFolderCommandList: ['go env GOMODCACHE', 'go env GOCACHE']
5018750187
}
5018850188
};

src/cache-restore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ const findDependencyFile = (packageManager: PackageManagerInfo) => {
5555
const workspace = process.env.GITHUB_WORKSPACE!;
5656
const rootContent = fs.readdirSync(workspace);
5757

58-
const goSumFileExists = rootContent.includes(dependencyFile);
59-
if (!goSumFileExists) {
58+
const goModFileExists = rootContent.includes(dependencyFile);
59+
if (!goModFileExists) {
6060
throw new Error(
6161
`Dependencies file is not found in ${workspace}. Supported file pattern: ${dependencyFile}`
6262
);

src/package-managers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface PackageManagerInfo {
99

1010
export const supportedPackageManagers: SupportedPackageManagers = {
1111
default: {
12-
dependencyFilePattern: 'go.sum',
12+
dependencyFilePattern: 'go.mod',
1313
cacheFolderCommandList: ['go env GOMODCACHE', 'go env GOCACHE']
1414
}
1515
};

0 commit comments

Comments
 (0)