From c8e2a54937e3d176334d8565d084fa913391adbd Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Mon, 17 Apr 2023 18:50:22 -0700 Subject: [PATCH] Clean target dir. --- src/cargo.ts | 55 +++++++++++++++++++++++++++++++++++++--------------- src/fs.ts | 12 ++++++++++++ 2 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 src/fs.ts diff --git a/src/cargo.ts b/src/cargo.ts index 27406a8..b8bf5ab 100644 --- a/src/cargo.ts +++ b/src/cargo.ts @@ -6,12 +6,14 @@ import * as cache from '@actions/cache'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; import * as glob from '@actions/glob'; -import * as io from '@actions/io'; import * as tc from '@actions/tool-cache'; +import { rmrf } from './fs'; import { RUST_HASH, RUST_VERSION } from './rust'; export const CARGO_HOME = process.env.CARGO_HOME ?? path.join(os.homedir(), '.cargo'); +export const WORKSPACE_ROOT = process.env.GITHUB_WORKSPACE ?? process.cwd(); + export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable(); export async function downloadAndInstallBinstall(binDir: string) { @@ -87,7 +89,7 @@ export function getCachePaths(): string[] { // ~/.cargo/registry path.join(CARGO_HOME, 'registry'), // /workspace/target/debug - path.join(process.env.GITHUB_WORKSPACE ?? process.cwd(), 'target/debug'), + path.join(WORKSPACE_ROOT, 'target/debug'), ]; } @@ -122,7 +124,7 @@ export async function getPrimaryCacheKey() { } export async function cleanCargoRegistry() { - core.info('Cleaning cache before saving'); + core.info('Cleaning ~/.cargo before saving'); const registryDir = path.join(CARGO_HOME, 'registry'); @@ -133,25 +135,46 @@ export async function cleanCargoRegistry() { const indexDir = path.join(registryDir, 'index'); if (fs.existsSync(indexDir)) { - for (const index of fs.readdirSync(indexDir)) { - if (fs.existsSync(path.join(indexDir, index, '.git'))) { - const dir = path.join(indexDir, index, '.cache'); - - core.debug(`Deleting ${dir}`); - - try { - // eslint-disable-next-line no-await-in-loop - await io.rmRF(dir); - } catch (error: unknown) { - core.warning(`Failed to delete ${dir}: ${error}`); + await Promise.all( + fs.readdirSync(indexDir).map(async (index) => { + if (fs.existsSync(path.join(indexDir, index, '.git'))) { + await rmrf(path.join(indexDir, index, '.cache')); } - } - } + }), + ); } // .cargo/registry/cache - Do nothing? } +// https://doc.rust-lang.org/cargo/guide/build-cache.html +export async function cleanTargetProfile() { + core.info('Cleaning target/debug before saving'); + + const targetDir = path.join(WORKSPACE_ROOT, 'target/debug'); + + // target/debug/{examples,incremental} - Not required in CI + core.info('Removing examples and incremental directories'); + + await Promise.all( + ['examples', 'incremental'].map(async (dirName) => { + const dir = path.join(targetDir, dirName); + + if (fs.existsSync(dir)) { + await rmrf(dir); + } + }), + ); + + // target/debug/**/*.d - Dep info files for build systems, not required in CI + core.info('Removing depinfo files (*.d)'); + + const globber = await glob.create(path.join(targetDir, '**/*.d')); + const files = await globber.glob(); + + await Promise.all(files.map(rmrf)); +} + export async function saveCache() { if (!CACHE_ENABLED) { return; diff --git a/src/fs.ts b/src/fs.ts new file mode 100644 index 0000000..31454d2 --- /dev/null +++ b/src/fs.ts @@ -0,0 +1,12 @@ +import * as core from '@actions/core'; +import * as io from '@actions/io'; + +export async function rmrf(dir: string) { + core.debug(`Deleting ${dir}`); + + try { + await io.rmRF(dir); + } catch (error: unknown) { + core.warning(`Failed to delete ${dir}: ${error}`); + } +}