@@ -6,12 +6,14 @@ import * as cache from '@actions/cache';
66import * as core from '@actions/core' ;
77import * as exec from '@actions/exec' ;
88import * as glob from '@actions/glob' ;
9- import * as io from '@actions/io' ;
109import * as tc from '@actions/tool-cache' ;
10+ import { rmrf } from './fs' ;
1111import { RUST_HASH , RUST_VERSION } from './rust' ;
1212
1313export 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+
1517export const CACHE_ENABLED = core . getBooleanInput ( 'cache' ) || cache . isFeatureAvailable ( ) ;
1618
1719export 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
9496export 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
98100export async function getPrimaryCacheKey ( ) {
@@ -122,7 +124,7 @@ export async function getPrimaryCacheKey() {
122124}
123125
124126export 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+
155178export async function saveCache ( ) {
156179 if ( ! CACHE_ENABLED ) {
157180 return ;
0 commit comments