Skip to content

Commit c33bc5a

Browse files
authored
new: Clean target directory before saving. (#3)
1 parent 8e43d37 commit c33bc5a

File tree

4 files changed

+69
-28
lines changed

4 files changed

+69
-28
lines changed

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,22 @@ CI times. To disable caching, set the `cache` input to `false`.
8686

8787
The following optimizations and considerations are taken into account when caching:
8888

89-
- The `~/.cargo/bin` directory is not cached as we manage binary installation in this action via the
90-
`bins` input.
91-
- The `~/.cargo/git` directory is not cached as it's not necessary for CI. When required by Cargo or
92-
a crate, a checkout will be performed on-demand.
93-
- The `~/.cargo/registry` directory is _cleaned_ before saving the cache. This includes removing
94-
`src`, `.cache`, and any other unnecessary files.
95-
- Only the `/target/debug` profile is cached, as this profile is typically used for formatting,
96-
linting, and testing.
97-
- The following sources are hashed for the generated cache key: `$GITHUB_JOB`, `Cargo.lock`, Rust
98-
version, Rust commit hash, and operating system.
89+
- `~/.cargo`
90+
- The `/bin` directory is not cached as we manage binary installation in this action via the
91+
`bins` input.
92+
- The `/git` directory is not cached as it's not necessary for CI. When required by Cargo or a
93+
crate, a checkout will be performed on-demand.
94+
- The `/registry` directory is _cleaned_ before saving the cache. This includes removing `src`,
95+
`.cache`, and any other unnecessary files.
96+
- `/target/debug`
97+
- Only the `debug` profile is cached, as this profile is typically used for formatting, linting,
98+
and testing.
99+
- The `/examples` and `/incremental` directories are not cached as they are not necessary for CI.
100+
- All dep-info (`*.d`) files are removed, as they're meant for build systems and signaling
101+
re-executions.
102+
103+
The following sources are hashed for the generated cache key: `$GITHUB_JOB`, `Cargo.lock`, Rust
104+
version, Rust commit hash, and OS.
99105

100106
## Compared to
101107

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@moonrepo/setup-rust",
3-
"version": "0.3.6",
3+
"version": "0.4.0",
44
"description": "A GitHub action for setting up Rust and Cargo.",
55
"main": "dist/index.js",
66
"scripts": {

src/cargo.ts

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import * as cache from '@actions/cache';
66
import * as core from '@actions/core';
77
import * as exec from '@actions/exec';
88
import * as glob from '@actions/glob';
9-
import * as io from '@actions/io';
109
import * as tc from '@actions/tool-cache';
10+
import { rmrf } from './fs';
1111
import { RUST_HASH, RUST_VERSION } from './rust';
1212

1313
export const CARGO_HOME = process.env.CARGO_HOME ?? path.join(os.homedir(), '.cargo');
1414

15+
export const WORKSPACE_ROOT = process.env.GITHUB_WORKSPACE ?? process.cwd();
16+
1517
export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable();
1618

1719
export async function downloadAndInstallBinstall(binDir: string) {
@@ -87,12 +89,12 @@ export function getCachePaths(): string[] {
8789
// ~/.cargo/registry
8890
path.join(CARGO_HOME, 'registry'),
8991
// /workspace/target/debug
90-
path.join(process.env.GITHUB_WORKSPACE ?? process.cwd(), 'target/debug'),
92+
path.join(WORKSPACE_ROOT, 'target/debug'),
9193
];
9294
}
9395

9496
export function getCachePrefixes(): string[] {
95-
return [`setup-rustcargo-v0-${process.platform}`, 'setup-rustcargo-v0'];
97+
return [`setup-rustcargo-v1-${process.platform}`, 'setup-rustcargo-v1'];
9698
}
9799

98100
export async function getPrimaryCacheKey() {
@@ -122,7 +124,7 @@ export async function getPrimaryCacheKey() {
122124
}
123125

124126
export async function cleanCargoRegistry() {
125-
core.info('Cleaning cache before saving');
127+
core.info('Cleaning ~/.cargo before saving');
126128

127129
const registryDir = path.join(CARGO_HOME, 'registry');
128130

@@ -133,25 +135,46 @@ export async function cleanCargoRegistry() {
133135
const indexDir = path.join(registryDir, 'index');
134136

135137
if (fs.existsSync(indexDir)) {
136-
for (const index of fs.readdirSync(indexDir)) {
137-
if (fs.existsSync(path.join(indexDir, index, '.git'))) {
138-
const dir = path.join(indexDir, index, '.cache');
139-
140-
core.debug(`Deleting ${dir}`);
141-
142-
try {
143-
// eslint-disable-next-line no-await-in-loop
144-
await io.rmRF(dir);
145-
} catch (error: unknown) {
146-
core.warning(`Failed to delete ${dir}: ${error}`);
138+
await Promise.all(
139+
fs.readdirSync(indexDir).map(async (index) => {
140+
if (fs.existsSync(path.join(indexDir, index, '.git'))) {
141+
await rmrf(path.join(indexDir, index, '.cache'));
147142
}
148-
}
149-
}
143+
}),
144+
);
150145
}
151146

152147
// .cargo/registry/cache - Do nothing?
153148
}
154149

150+
// https://doc.rust-lang.org/cargo/guide/build-cache.html
151+
export async function cleanTargetProfile() {
152+
core.info('Cleaning target/debug before saving');
153+
154+
const targetDir = path.join(WORKSPACE_ROOT, 'target/debug');
155+
156+
// target/debug/{examples,incremental} - Not required in CI
157+
core.info('Removing examples and incremental directories');
158+
159+
await Promise.all(
160+
['examples', 'incremental'].map(async (dirName) => {
161+
const dir = path.join(targetDir, dirName);
162+
163+
if (fs.existsSync(dir)) {
164+
await rmrf(dir);
165+
}
166+
}),
167+
);
168+
169+
// target/debug/**/*.d - Not required in CI?
170+
core.info('Removing dep-info files (*.d)');
171+
172+
const globber = await glob.create(path.join(targetDir, '**/*.d'));
173+
const files = await globber.glob();
174+
175+
await Promise.all(files.map(rmrf));
176+
}
177+
155178
export async function saveCache() {
156179
if (!CACHE_ENABLED) {
157180
return;

src/fs.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as core from '@actions/core';
2+
import * as io from '@actions/io';
3+
4+
export async function rmrf(dir: string) {
5+
core.debug(`Deleting ${dir}`);
6+
7+
try {
8+
await io.rmRF(dir);
9+
} catch (error: unknown) {
10+
core.warning(`Failed to delete ${dir}: ${error}`);
11+
}
12+
}

0 commit comments

Comments
 (0)