Skip to content

Commit e8e63cd

Browse files
authored
Allow opting out of caching $CARGO_HOME/bin. (#216)
Prevents wiping the bin directory, which is harmful for self-hosted runners.
1 parent 9a2e0d3 commit e8e63cd

File tree

6 files changed

+59
-15
lines changed

6 files changed

+59
-15
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ sensible defaults.
7979
# Can be set to either "github" or "buildjet"
8080
# default: "github"
8181
cache-provider: ""
82+
83+
# Determines whether to cache the ~/.cargo/bin directory.
84+
# default: "true"
85+
cache-bin: ""
8286
```
8387
8488
Further examples are available in the [.github/workflows](./.github/workflows/) directory.
@@ -175,4 +179,7 @@ to see those details as well as further details related to caching operations.
175179
## Known issues
176180

177181
- The cache cleaning process currently removes all the files from `~/.cargo/bin`
178-
that were present before the action ran (for example `rustc`).
182+
that were present before the action ran (for example `rustc`), by default.
183+
This can be an issue on long-running self-hosted runners, where such state
184+
is expected to be preserved across runs. You can work around this by setting
185+
`cache-bin: "false"`.

action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ inputs:
4040
description: "Determines which provider to use for caching. Options are github or buildjet, defaults to github."
4141
required: false
4242
default: "github"
43+
cache-bin:
44+
description: "Determines whether to cache ${CARGO_HOME}/bin."
45+
required: false
46+
default: "true"
4347
lookup-only:
4448
description: "Check if a cache entry exists without downloading the cache"
4549
required: false

dist/restore/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -77276,6 +77276,8 @@ class CacheConfig {
7727677276
this.cacheKey = "";
7727777277
/** The secondary (restore) key that only contains the prefix and environment */
7727877278
this.restoreKey = "";
77279+
/** Whether to cache CARGO_HOME/.bin */
77280+
this.cacheBin = true;
7727977281
/** The workspace configurations */
7728077282
this.workspaces = [];
7728177283
/** The cargo binaries present during main step */
@@ -77351,6 +77353,7 @@ class CacheConfig {
7735177353
// Construct the lockfiles portion of the key:
7735277354
// This considers all the files found via globbing for various manifests
7735377355
// and lockfiles.
77356+
self.cacheBin = lib_core.getInput("cache-bin").toLowerCase() == "true";
7735477357
// Constructs the workspace config and paths to restore:
7735577358
// The workspaces are given using a `$workspace -> $target` syntax.
7735677359
const workspaces = [];
@@ -77445,7 +77448,13 @@ class CacheConfig {
7744577448
self.keyFiles = sort_and_uniq(keyFiles);
7744677449
key += `-${lockHash}`;
7744777450
self.cacheKey = key;
77448-
self.cachePaths = [config_CARGO_HOME];
77451+
self.cachePaths = [
77452+
external_path_default().join(config_CARGO_HOME, "registry"),
77453+
external_path_default().join(config_CARGO_HOME, "git"),
77454+
];
77455+
if (self.cacheBin) {
77456+
self.cachePaths = [external_path_default().join(config_CARGO_HOME, "bin"), ...self.cachePaths];
77457+
}
7744977458
const cacheTargets = lib_core.getInput("cache-targets").toLowerCase() || "true";
7745077459
if (cacheTargets === "true") {
7745177460
self.cachePaths.push(...workspaces.map((ws) => ws.target));

dist/save/index.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -77276,6 +77276,8 @@ class CacheConfig {
7727677276
this.cacheKey = "";
7727777277
/** The secondary (restore) key that only contains the prefix and environment */
7727877278
this.restoreKey = "";
77279+
/** Whether to cache CARGO_HOME/.bin */
77280+
this.cacheBin = true;
7727977281
/** The workspace configurations */
7728077282
this.workspaces = [];
7728177283
/** The cargo binaries present during main step */
@@ -77351,6 +77353,7 @@ class CacheConfig {
7735177353
// Construct the lockfiles portion of the key:
7735277354
// This considers all the files found via globbing for various manifests
7735377355
// and lockfiles.
77356+
self.cacheBin = core.getInput("cache-bin").toLowerCase() == "true";
7735477357
// Constructs the workspace config and paths to restore:
7735577358
// The workspaces are given using a `$workspace -> $target` syntax.
7735677359
const workspaces = [];
@@ -77445,7 +77448,13 @@ class CacheConfig {
7744577448
self.keyFiles = sort_and_uniq(keyFiles);
7744677449
key += `-${lockHash}`;
7744777450
self.cacheKey = key;
77448-
self.cachePaths = [CARGO_HOME];
77451+
self.cachePaths = [
77452+
external_path_default().join(CARGO_HOME, "registry"),
77453+
external_path_default().join(CARGO_HOME, "git"),
77454+
];
77455+
if (self.cacheBin) {
77456+
self.cachePaths = [external_path_default().join(CARGO_HOME, "bin"), ...self.cachePaths];
77457+
}
7744977458
const cacheTargets = core.getInput("cache-targets").toLowerCase() || "true";
7745077459
if (cacheTargets === "true") {
7745177460
self.cachePaths.push(...workspaces.map((ws) => ws.target));
@@ -77907,12 +77916,14 @@ async function run() {
7790777916
catch (e) {
7790877917
core.debug(`${e.stack}`);
7790977918
}
77910-
try {
77911-
core.info(`... Cleaning cargo/bin ...`);
77912-
await cleanBin(config.cargoBins);
77913-
}
77914-
catch (e) {
77915-
core.debug(`${e.stack}`);
77919+
if (config.cacheBin) {
77920+
try {
77921+
core.info(`... Cleaning cargo/bin ...`);
77922+
await cleanBin(config.cargoBins);
77923+
}
77924+
catch (e) {
77925+
core.debug(`${e.stack}`);
77926+
}
7791677927
}
7791777928
try {
7791877929
core.info(`... Cleaning cargo git cache ...`);

src/config.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export class CacheConfig {
2525
/** The secondary (restore) key that only contains the prefix and environment */
2626
public restoreKey = "";
2727

28+
/** Whether to cache CARGO_HOME/.bin */
29+
public cacheBin: boolean = true;
30+
2831
/** The workspace configurations */
2932
public workspaces: Array<Workspace> = [];
3033

@@ -120,6 +123,8 @@ export class CacheConfig {
120123
// This considers all the files found via globbing for various manifests
121124
// and lockfiles.
122125

126+
self.cacheBin = core.getInput("cache-bin").toLowerCase() == "true";
127+
123128
// Constructs the workspace config and paths to restore:
124129
// The workspaces are given using a `$workspace -> $target` syntax.
125130

@@ -238,7 +243,13 @@ export class CacheConfig {
238243
key += `-${lockHash}`;
239244
self.cacheKey = key;
240245

241-
self.cachePaths = [CARGO_HOME];
246+
self.cachePaths = [
247+
path.join(CARGO_HOME, "registry"),
248+
path.join(CARGO_HOME, "git"),
249+
];
250+
if (self.cacheBin) {
251+
self.cachePaths = [path.join(CARGO_HOME, "bin"), ...self.cachePaths];
252+
}
242253
const cacheTargets = core.getInput("cache-targets").toLowerCase() || "true";
243254
if (cacheTargets === "true") {
244255
self.cachePaths.push(...workspaces.map((ws) => ws.target));

src/save.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ async function run() {
5656
core.debug(`${(e as any).stack}`);
5757
}
5858

59-
try {
60-
core.info(`... Cleaning cargo/bin ...`);
61-
await cleanBin(config.cargoBins);
62-
} catch (e) {
63-
core.debug(`${(e as any).stack}`);
59+
if (config.cacheBin) {
60+
try {
61+
core.info(`... Cleaning cargo/bin ...`);
62+
await cleanBin(config.cargoBins);
63+
} catch (e) {
64+
core.debug(`${(e as any).stack}`);
65+
}
6466
}
6567

6668
try {

0 commit comments

Comments
 (0)