Skip to content

Commit def0926

Browse files
authored
feat: add option to cache all crates (#137)
Add cache-all-crates option which allows all crates to be cached instead of just the dependency crates. This is useful when additional crates are required for CI tooling.
1 parent 827c240 commit def0926

File tree

6 files changed

+42
-17
lines changed

6 files changed

+42
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ sensible defaults.
5454
# default: "false"
5555
cache-on-failure: ""
5656

57+
# Determines which crates are cached.
58+
# If `true` all crates will be cached, otherwise only dependent crates will be cached.
59+
# Useful if additional crates are used for CI tooling.
60+
# default: "false"
61+
cache-all-crates: ""
62+
5763
# Determiners whether the cache should be saved.
5864
# If `false`, the cache is only restored.
5965
# Useful for jobs where the matrix is additive e.g. additional Cargo features.

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ inputs:
2828
cache-on-failure:
2929
description: "Cache even if the build fails. Defaults to false."
3030
required: false
31+
cache-all-crates:
32+
description: "Determines which crates are cached. If `true` all crates will be cached, otherwise only dependent crates will be cached."
33+
required: false
34+
default: "false"
3135
save-if:
3236
description: "Determiners whether the cache should be saved. If `false`, the cache is only restored."
3337
required: false

dist/restore/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60234,7 +60234,7 @@ async function cleanBin() {
6023460234
}
6023560235
}
6023660236
}
60237-
async function cleanRegistry(packages) {
60237+
async function cleanRegistry(packages, crates = true) {
6023860238
// `.cargo/registry/src`
6023960239
// we can remove this completely, as cargo will recreate this from `cache`
6024060240
await rmRF(path.join(CARGO_HOME, "registry", "src"));
@@ -60252,6 +60252,10 @@ async function cleanRegistry(packages) {
6025260252
// TODO: else, clean `.cache` based on the `packages`
6025360253
}
6025460254
}
60255+
if (!crates) {
60256+
core.debug(`skipping crate cleanup`);
60257+
return;
60258+
}
6025560259
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
6025660260
// `.cargo/registry/cache`
6025760261
const cacheDir = await fs.promises.opendir(path.join(CARGO_HOME, "registry", "cache"));

dist/save/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60234,7 +60234,7 @@ async function cleanBin() {
6023460234
}
6023560235
}
6023660236
}
60237-
async function cleanRegistry(packages) {
60237+
async function cleanRegistry(packages, crates = true) {
6023860238
// `.cargo/registry/src`
6023960239
// we can remove this completely, as cargo will recreate this from `cache`
6024060240
await rmRF(external_path_default().join(CARGO_HOME, "registry", "src"));
@@ -60252,6 +60252,10 @@ async function cleanRegistry(packages) {
6025260252
// TODO: else, clean `.cache` based on the `packages`
6025360253
}
6025460254
}
60255+
if (!crates) {
60256+
core.debug(`skipping crate cleanup`);
60257+
return;
60258+
}
6025560259
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
6025660260
// `.cargo/registry/cache`
6025760261
const cacheDir = await external_fs_default().promises.opendir(external_path_default().join(CARGO_HOME, "registry", "cache"));
@@ -60417,29 +60421,30 @@ async function run() {
6041760421
await cleanTargetDir(workspace.target, packages);
6041860422
}
6041960423
catch (e) {
60420-
core.info(`[warning] ${e.stack}`);
60424+
core.error(`${e.stack}`);
6042160425
}
6042260426
}
6042360427
try {
60424-
core.info(`... Cleaning cargo registry ...`);
60425-
await cleanRegistry(allPackages);
60428+
const creates = core.getInput("cache-all-crates").toLowerCase() || "false";
60429+
core.info(`... Cleaning cargo registry cache-all-crates: ${creates} ...`);
60430+
await cleanRegistry(allPackages, creates === "true");
6042660431
}
6042760432
catch (e) {
60428-
core.info(`[warning] ${e.stack}`);
60433+
core.error(`${e.stack}`);
6042960434
}
6043060435
try {
6043160436
core.info(`... Cleaning cargo/bin ...`);
6043260437
await cleanBin();
6043360438
}
6043460439
catch (e) {
60435-
core.info(`[warning] ${e.stack}`);
60440+
core.error(`${e.stack}`);
6043660441
}
6043760442
try {
6043860443
core.info(`... Cleaning cargo git cache ...`);
6043960444
await cleanGit(allPackages);
6044060445
}
6044160446
catch (e) {
60442-
core.info(`[warning] ${e.stack}`);
60447+
core.error(`${e.stack}`);
6044360448
}
6044460449
core.info(`... Saving cache ...`);
6044560450
// Pass a copy of cachePaths to avoid mutating the original array as reported by:
@@ -60448,7 +60453,7 @@ async function run() {
6044860453
await cache.saveCache(config.cachePaths.slice(), config.cacheKey);
6044960454
}
6045060455
catch (e) {
60451-
core.info(`[warning] ${e.stack}`);
60456+
core.error(`${e.stack}`);
6045260457
}
6045360458
}
6045460459
run();

src/cleanup.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export async function cleanBin() {
8585
}
8686
}
8787

88-
export async function cleanRegistry(packages: Packages) {
88+
export async function cleanRegistry(packages: Packages, crates = true) {
8989
// `.cargo/registry/src`
9090
// we can remove this completely, as cargo will recreate this from `cache`
9191
await rmRF(path.join(CARGO_HOME, "registry", "src"));
@@ -106,6 +106,11 @@ export async function cleanRegistry(packages: Packages) {
106106
}
107107
}
108108

109+
if (!crates) {
110+
core.debug(`skipping crate cleanup`);
111+
return;
112+
}
113+
109114
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
110115

111116
// `.cargo/registry/cache`

src/save.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,30 @@ async function run() {
4040
core.info(`... Cleaning ${workspace.target} ...`);
4141
await cleanTargetDir(workspace.target, packages);
4242
} catch (e) {
43-
core.info(`[warning] ${(e as any).stack}`);
43+
core.error(`${(e as any).stack}`);
4444
}
4545
}
4646

4747
try {
48-
core.info(`... Cleaning cargo registry ...`);
49-
await cleanRegistry(allPackages);
48+
const creates = core.getInput("cache-all-crates").toLowerCase() || "false";
49+
core.info(`... Cleaning cargo registry cache-all-crates: ${creates} ...`);
50+
await cleanRegistry(allPackages, creates === "true");
5051
} catch (e) {
51-
core.info(`[warning] ${(e as any).stack}`);
52+
core.error(`${(e as any).stack}`);
5253
}
5354

5455
try {
5556
core.info(`... Cleaning cargo/bin ...`);
5657
await cleanBin();
5758
} catch (e) {
58-
core.info(`[warning] ${(e as any).stack}`);
59+
core.error(`${(e as any).stack}`);
5960
}
6061

6162
try {
6263
core.info(`... Cleaning cargo git cache ...`);
6364
await cleanGit(allPackages);
6465
} catch (e) {
65-
core.info(`[warning] ${(e as any).stack}`);
66+
core.error(`${(e as any).stack}`);
6667
}
6768

6869
core.info(`... Saving cache ...`);
@@ -71,7 +72,7 @@ async function run() {
7172
// TODO: remove this once the underlying bug is fixed.
7273
await cache.saveCache(config.cachePaths.slice(), config.cacheKey);
7374
} catch (e) {
74-
core.info(`[warning] ${(e as any).stack}`);
75+
core.error(`${(e as any).stack}`);
7576
}
7677
}
7778

0 commit comments

Comments
 (0)