Skip to content

Commit 6a2f4c2

Browse files
committed
fix: cache errors
1 parent b15cb59 commit 6a2f4c2

File tree

6 files changed

+45
-33
lines changed

6 files changed

+45
-33
lines changed

dist/index.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44225,10 +44225,12 @@ const crypto_1 = __nccwpck_require__(76982);
4422544225
const fs_1 = __nccwpck_require__(79896);
4422644226
const cargo_1 = __nccwpck_require__(38472);
4422744227
const cache_impl_1 = __nccwpck_require__(36609);
44228-
function buildCacheStrategy(projectDir, strategy, toolchains, fallbackBranch) {
44228+
function buildCacheStrategy(projectDir, strategy, toolchains, fallbackBranch, cachePrefix) {
44229+
if (!cachePrefix)
44230+
return;
4422944231
switch (strategy) {
4423044232
case 'github':
44231-
return new GithubBuildCacheStrategy(projectDir, toolchains, fallbackBranch);
44233+
return new GithubBuildCacheStrategy(cachePrefix, projectDir, toolchains, fallbackBranch);
4423244234
case 'none':
4423344235
return undefined;
4423444236
default:
@@ -44242,11 +44244,11 @@ class GithubBuildCacheStrategy {
4424244244
projectDir;
4424344245
fallbackBranch;
4424444246
restoredFrom;
44245-
constructor(projectDir, toolchains, fallbackBranch) {
44247+
constructor(cachePrefix, projectDir, toolchains, fallbackBranch) {
4424644248
this.projectDir = projectDir;
4424744249
this.fallbackBranch = fallbackBranch;
44248-
this.cacheKey = GithubBuildCacheStrategy.buildCacheKey(projectDir, toolchains, fallbackBranch);
44249-
this.fallbackKey = GithubBuildCacheStrategy.buildFallbackCacheKey(projectDir, toolchains, fallbackBranch);
44250+
this.cacheKey = GithubBuildCacheStrategy.buildCacheKey(cachePrefix, projectDir, toolchains, fallbackBranch);
44251+
this.fallbackKey = GithubBuildCacheStrategy.buildFallbackCacheKey(cachePrefix, projectDir, toolchains, fallbackBranch);
4425044252
}
4425144253
async restore() {
4425244254
this.restoredFrom = await GithubBuildCacheStrategy.restoreBuildCache(this.projectDir, this.cacheKey, this.fallbackKey);
@@ -44284,7 +44286,7 @@ class GithubBuildCacheStrategy {
4428444286
await (0, cache_impl_1.saveToCache)([targetDir], cacheKey);
4428544287
(0, core_1.info)(`Saved build cache with key: ${cacheKey}`);
4428644288
}
44287-
static buildCacheKey(projectDir, toolchains, fallbackBranch) {
44289+
static buildCacheKey(cachePrefix, projectDir, toolchains, fallbackBranch) {
4428844290
const lockFile = cargo_1.Cargo.cargoLock(projectDir);
4428944291
let lockHashOrBranch = process.env.GITHUB_REF_NAME || 'not-in-gh-action';
4429044292
if (fallbackBranch === lockHashOrBranch) {
@@ -44309,16 +44311,16 @@ class GithubBuildCacheStrategy {
4430944311
normalizedProjectDir = normalizedProjectDir.replace(/[\\/]+/g, '-');
4431044312
const platform = process.platform;
4431144313
const arch = process.arch;
44312-
return `rax-cache-build-${platform}-${arch}-${toolchainHash}-${normalizedProjectDir}-${lockHashOrBranch}`;
44314+
return `${cachePrefix}-build-${platform}-${arch}-${toolchainHash}-${normalizedProjectDir}-${lockHashOrBranch}`;
4431344315
}
44314-
static buildFallbackCacheKey(projectDir, toolchains, fallbackBranch) {
44316+
static buildFallbackCacheKey(cachePrefix, projectDir, toolchains, fallbackBranch) {
4431544317
const platform = process.platform;
4431644318
const arch = process.arch;
4431744319
const toolchainHash = (0, crypto_1.createHash)('sha256')
4431844320
.update(toolchains.sort().join(','))
4431944321
.digest('hex')
4432044322
.slice(0, 8);
44321-
return `rax-cache-build-${platform}-${arch}-${toolchainHash}-${projectDir}-${fallbackBranch}`;
44323+
return `${cachePrefix}-build-${platform}-${arch}-${toolchainHash}-${projectDir}-${fallbackBranch}`;
4432244324
}
4432344325
}
4432444326
exports.GithubBuildCacheStrategy = GithubBuildCacheStrategy;
@@ -44359,18 +44361,17 @@ async function saveToCache(cachePath, key) {
4435944361
}
4436044362
if (!anyExists) {
4436144363
core.warning(`No paths from ${cachePath} exist, skip caching for key ${key}`);
44362-
return;
44364+
return false;
4436344365
}
4436444366
try {
4436544367
await cache.saveCache(cachePath, key);
4436644368
core.info(`Cached key ${key}`);
4436744369
}
4436844370
catch (err) {
44369-
if (err.name === cache.ValidationError.name)
44370-
throw err;
44371-
if (err.name === cache.ReserveCacheError.name)
44372-
core.warning(`Caching failed for ${key}: ${err.message}`);
44371+
core.error(`Caching failed for ${key}: ${err.message}`);
44372+
return false;
4437344373
}
44374+
return true;
4437444375
}
4437544376
/**
4437644377
* Generates a cache key
@@ -44717,7 +44718,7 @@ async function run(cfg) {
4471744718
}
4471844719
cfg.toolchain = tomlChannel;
4471944720
}
44720-
const cacheKey = cfg.cacheKey === 'no-cache' ? undefined : 'rax-cache';
44721+
const cachePrefix = cfg.cacheKey === 'no-cache' ? undefined : 'rax-cache';
4472144722
// Ensure default toolchain is set to allow rustc -vV calls
4472244723
if ((await (0, rustup_js_1.getGlobalDefaultToolchain)()) === undefined) {
4472344724
await (0, rustup_js_1.setDefaultToolchain)(cfg.toolchain || 'stable');
@@ -44731,8 +44732,8 @@ async function run(cfg) {
4473144732
new workflows_js_1.DenyWorkflow((0, workflows_js_1.workflowConfig)(cfg, 'deny')),
4473244733
].filter((wf) => workflowFilter(cfg, wf.name));
4473344734
// Prepare toolchains
44734-
const installedToolchains = await installToolchains(cfg, enabledWorkflows, start, cacheKey);
44735-
const installedTools = await installTools(start, enabledWorkflows, cacheKey, cfg);
44735+
const installedToolchains = await installToolchains(cfg, enabledWorkflows, start, cachePrefix);
44736+
const installedTools = await installTools(start, enabledWorkflows, cachePrefix, cfg);
4473644737
// If installOnly is set, skip workflow execution
4473744738
if (cfg.installOnly) {
4473844739
(0, core_1.info)('Install-only mode enabled, skipping workflow execution.');
@@ -44743,7 +44744,7 @@ async function run(cfg) {
4474344744
succeeded: true,
4474444745
};
4474544746
}
44746-
const buildCache = (0, build_cache_js_1.buildCacheStrategy)(cfg.project, cfg.buildCacheStrategy, installedToolchains, cfg.buildCacheFallbackBranch);
44747+
const buildCache = (0, build_cache_js_1.buildCacheStrategy)(cfg.project, cfg.buildCacheStrategy, installedToolchains, cfg.buildCacheFallbackBranch, cachePrefix);
4474744748
if (buildCache) {
4474844749
await (0, core_1.group)(`Restoring build cache: ${timeSinceStart(start)}`, async () => {
4474944750
await buildCache.restore();

dist/src/cache/build-cache.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ export interface BuildCacheStrategy {
22
restore(): Promise<void>;
33
save(): Promise<void>;
44
}
5-
export declare function buildCacheStrategy(projectDir: string, strategy: string, toolchains: string[], fallbackBranch: string): BuildCacheStrategy | undefined;
5+
export declare function buildCacheStrategy(projectDir: string, strategy: string, toolchains: string[], fallbackBranch: string, cachePrefix?: string): BuildCacheStrategy | undefined;
66
export declare class GithubBuildCacheStrategy implements BuildCacheStrategy {
77
private cacheKey;
88
private fallbackKey;
99
private projectDir;
1010
private fallbackBranch;
1111
private restoredFrom;
12-
constructor(projectDir: string, toolchains: string[], fallbackBranch: string);
12+
constructor(cachePrefix: string, projectDir: string, toolchains: string[], fallbackBranch: string);
1313
restore(): Promise<void>;
1414
save(): Promise<void>;
1515
static restoreBuildCache(projectDir: string, cacheKey: string, fallbackKey: string): Promise<string | undefined>;
1616
static saveBuildCache(projectDir: string, cacheKey: string): Promise<void>;
17-
static buildCacheKey(projectDir: string, toolchains: string[], fallbackBranch: string): string;
18-
static buildFallbackCacheKey(projectDir: string, toolchains: string[], fallbackBranch: string): string;
17+
static buildCacheKey(cachePrefix: string, projectDir: string, toolchains: string[], fallbackBranch: string): string;
18+
static buildFallbackCacheKey(cachePrefix: string, projectDir: string, toolchains: string[], fallbackBranch: string): string;
1919
}

dist/src/cache/cache-impl.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export declare function restoreFromCache(cachePath: string[], key: string, restoreKeys?: string[]): Promise<string | undefined>;
2-
export declare function saveToCache(cachePath: string[], key: string): Promise<void>;
2+
export declare function saveToCache(cachePath: string[], key: string): Promise<boolean>;
33
/**
44
* Generates a cache key
55
*

src/cache/build-cache.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ export function buildCacheStrategy(
1414
strategy: string,
1515
toolchains: string[],
1616
fallbackBranch: string,
17+
cachePrefix?: string,
1718
): BuildCacheStrategy | undefined {
19+
if (!cachePrefix) return;
20+
1821
switch (strategy) {
1922
case 'github':
2023
return new GithubBuildCacheStrategy(
24+
cachePrefix,
2125
projectDir,
2226
toolchains,
2327
fallbackBranch,
@@ -39,18 +43,21 @@ export class GithubBuildCacheStrategy implements BuildCacheStrategy {
3943
private restoredFrom: string | undefined;
4044

4145
constructor(
46+
cachePrefix: string,
4247
projectDir: string,
4348
toolchains: string[],
4449
fallbackBranch: string,
4550
) {
4651
this.projectDir = projectDir;
4752
this.fallbackBranch = fallbackBranch;
4853
this.cacheKey = GithubBuildCacheStrategy.buildCacheKey(
54+
cachePrefix,
4955
projectDir,
5056
toolchains,
5157
fallbackBranch,
5258
);
5359
this.fallbackKey = GithubBuildCacheStrategy.buildFallbackCacheKey(
60+
cachePrefix,
5461
projectDir,
5562
toolchains,
5663
fallbackBranch,
@@ -121,6 +128,7 @@ export class GithubBuildCacheStrategy implements BuildCacheStrategy {
121128
}
122129

123130
static buildCacheKey(
131+
cachePrefix: string,
124132
projectDir: string,
125133
toolchains: string[],
126134
fallbackBranch: string,
@@ -153,10 +161,11 @@ export class GithubBuildCacheStrategy implements BuildCacheStrategy {
153161
const platform = process.platform;
154162
const arch = process.arch;
155163

156-
return `rax-cache-build-${platform}-${arch}-${toolchainHash}-${normalizedProjectDir}-${lockHashOrBranch}`;
164+
return `${cachePrefix}-build-${platform}-${arch}-${toolchainHash}-${normalizedProjectDir}-${lockHashOrBranch}`;
157165
}
158166

159167
static buildFallbackCacheKey(
168+
cachePrefix: string,
160169
projectDir: string,
161170
toolchains: string[],
162171
fallbackBranch: string,
@@ -168,6 +177,6 @@ export class GithubBuildCacheStrategy implements BuildCacheStrategy {
168177
.digest('hex')
169178
.slice(0, 8);
170179

171-
return `rax-cache-build-${platform}-${arch}-${toolchainHash}-${projectDir}-${fallbackBranch}`;
180+
return `${cachePrefix}-build-${platform}-${arch}-${toolchainHash}-${projectDir}-${fallbackBranch}`;
172181
}
173182
}

src/cache/cache-impl.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function restoreFromCache(
1818
export async function saveToCache(
1919
cachePath: string[],
2020
key: string,
21-
): Promise<void> {
21+
): Promise<boolean> {
2222
let anyExists = false;
2323
for (const path of cachePath) {
2424
if (!existsSync(path)) {
@@ -32,17 +32,18 @@ export async function saveToCache(
3232
core.warning(
3333
`No paths from ${cachePath} exist, skip caching for key ${key}`,
3434
);
35-
return;
35+
return false;
3636
}
3737

3838
try {
3939
await cache.saveCache(cachePath, key);
4040
core.info(`Cached key ${key}`);
4141
} catch (err: any) {
42-
if (err.name === cache.ValidationError.name) throw err;
43-
if (err.name === cache.ReserveCacheError.name)
44-
core.warning(`Caching failed for ${key}: ${err.message}`);
42+
core.error(`Caching failed for ${key}: ${err.message}`);
43+
return false;
4544
}
45+
46+
return true;
4647
}
4748

4849
/**

src/lib.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function run(cfg: Input): Promise<RunResult> {
5959
cfg.toolchain = tomlChannel;
6060
}
6161

62-
const cacheKey = cfg.cacheKey === 'no-cache' ? undefined : 'rax-cache';
62+
const cachePrefix = cfg.cacheKey === 'no-cache' ? undefined : 'rax-cache';
6363

6464
// Ensure default toolchain is set to allow rustc -vV calls
6565
if ((await getGlobalDefaultToolchain()) === undefined) {
@@ -80,13 +80,13 @@ export async function run(cfg: Input): Promise<RunResult> {
8080
cfg,
8181
enabledWorkflows,
8282
start,
83-
cacheKey,
83+
cachePrefix,
8484
);
8585

8686
const installedTools: [string, string][] = await installTools(
8787
start,
8888
enabledWorkflows,
89-
cacheKey,
89+
cachePrefix,
9090
cfg,
9191
);
9292

@@ -106,6 +106,7 @@ export async function run(cfg: Input): Promise<RunResult> {
106106
cfg.buildCacheStrategy,
107107
installedToolchains,
108108
cfg.buildCacheFallbackBranch,
109+
cachePrefix,
109110
);
110111

111112
if (buildCache) {

0 commit comments

Comments
 (0)