3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2025-06-19 14:53:42 +00:00

explicitly close dir handles, add more logging, cleanups

This commit is contained in:
Arpad Borsos 2022-09-04 13:10:36 +02:00
parent 213334cd98
commit be4be3720d
No known key found for this signature in database
GPG key ID: FC7BCA77824B3298
5 changed files with 217 additions and 148 deletions

82
dist/restore/index.js vendored
View file

@ -64346,7 +64346,7 @@ var cache = __nccwpck_require__(7799);
// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js // EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
var lib_core = __nccwpck_require__(2186); var lib_core = __nccwpck_require__(2186);
// EXTERNAL MODULE: ./node_modules/@actions/io/lib/io.js // EXTERNAL MODULE: ./node_modules/@actions/io/lib/io.js
var io = __nccwpck_require__(7436); var lib_io = __nccwpck_require__(7436);
// EXTERNAL MODULE: external "fs" // EXTERNAL MODULE: external "fs"
var external_fs_ = __nccwpck_require__(7147); var external_fs_ = __nccwpck_require__(7147);
var external_fs_default = /*#__PURE__*/__nccwpck_require__.n(external_fs_); var external_fs_default = /*#__PURE__*/__nccwpck_require__.n(external_fs_);
@ -64394,6 +64394,7 @@ async function getCmdOutput(cmd, args = [], options = {}) {
;// CONCATENATED MODULE: ./src/workspace.ts ;// CONCATENATED MODULE: ./src/workspace.ts
const SAVE_TARGETS = new Set(["lib", "proc-macro"]);
class Workspace { class Workspace {
constructor(root, target) { constructor(root, target) {
this.root = root; this.root = root;
@ -64409,7 +64410,7 @@ class Workspace {
if (pkg.manifest_path.startsWith(this.root)) { if (pkg.manifest_path.startsWith(this.root)) {
continue; continue;
} }
const targets = pkg.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name); const targets = pkg.targets.filter((t) => t.kind.some((kind) => SAVE_TARGETS.has(kind))).map((t) => t.name);
packages.push({ name: pkg.name, version: pkg.version, targets, path: external_path_default().dirname(pkg.manifest_path) }); packages.push({ name: pkg.name, version: pkg.version, targets, path: external_path_default().dirname(pkg.manifest_path) });
} }
} }
@ -64594,10 +64595,10 @@ async function getRustVersion() {
async function cleanTargetDir(targetDir, packages) { async function cleanTargetDir(targetDir, packages, checkTimestamp = false) {
let dir; lib_core.debug(`cleaning target directory "${targetDir}"`);
// remove all *files* from the profile directory // remove all *files* from the profile directory
dir = await external_fs_default().promises.opendir(targetDir); let dir = await external_fs_default().promises.opendir(targetDir);
for await (const dirent of dir) { for await (const dirent of dir) {
if (dirent.isDirectory()) { if (dirent.isDirectory()) {
let dirName = external_path_default().join(dir.path, dirent.name); let dirName = external_path_default().join(dir.path, dirent.name);
@ -64605,10 +64606,10 @@ async function cleanTargetDir(targetDir, packages) {
let isNestedTarget = (await exists(external_path_default().join(dirName, "CACHEDIR.TAG"))) || (await exists(external_path_default().join(dirName, ".rustc_info.json"))); let isNestedTarget = (await exists(external_path_default().join(dirName, "CACHEDIR.TAG"))) || (await exists(external_path_default().join(dirName, ".rustc_info.json")));
try { try {
if (isNestedTarget) { if (isNestedTarget) {
await cleanTargetDir(dirName, packages); await cleanTargetDir(dirName, packages, checkTimestamp);
} }
else { else {
await cleanProfileTarget(dirName, packages); await cleanProfileTarget(dirName, packages, checkTimestamp);
} }
} }
catch { } catch { }
@ -64617,21 +64618,15 @@ async function cleanTargetDir(targetDir, packages) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
async function cleanProfileTarget(profileDir, packages) { async function cleanProfileTarget(profileDir, packages, checkTimestamp = false) {
await rmRF(external_path_default().join(profileDir, "examples")); lib_core.debug(`cleaning profile directory "${profileDir}"`);
await rmRF(external_path_default().join(profileDir, "incremental")); let keepProfile = new Set(["build", ".fingerprint", "deps"]);
let dir; await rmExcept(profileDir, keepProfile);
// remove all *files* from the profile directory
dir = await external_fs_default().promises.opendir(profileDir);
for await (const dirent of dir) {
if (dirent.isFile()) {
await rm(dir.path, dirent);
}
}
const keepPkg = new Set(packages.map((p) => p.name)); const keepPkg = new Set(packages.map((p) => p.name));
await rmExcept(external_path_default().join(profileDir, "build"), keepPkg); await rmExcept(external_path_default().join(profileDir, "build"), keepPkg, checkTimestamp);
await rmExcept(external_path_default().join(profileDir, ".fingerprint"), keepPkg); await rmExcept(external_path_default().join(profileDir, ".fingerprint"), keepPkg, checkTimestamp);
const keepDeps = new Set(packages.flatMap((p) => { const keepDeps = new Set(packages.flatMap((p) => {
const names = []; const names = [];
for (const n of [p.name, ...p.targets]) { for (const n of [p.name, ...p.targets]) {
@ -64640,7 +64635,7 @@ async function cleanProfileTarget(profileDir, packages) {
} }
return names; return names;
})); }));
await rmExcept(external_path_default().join(profileDir, "deps"), keepDeps); await rmExcept(external_path_default().join(profileDir, "deps"), keepDeps, checkTimestamp);
} }
async function getCargoBins() { async function getCargoBins() {
const bins = new Set(); const bins = new Set();
@ -64667,6 +64662,7 @@ async function cleanBin() {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
async function cleanRegistry(packages) { async function cleanRegistry(packages) {
// `.cargo/registry/src` // `.cargo/registry/src`
@ -64683,9 +64679,11 @@ async function cleanRegistry(packages) {
if (await exists(path.join(dir.path, ".git"))) { if (await exists(path.join(dir.path, ".git"))) {
await rmRF(path.join(dir.path, ".cache")); await rmRF(path.join(dir.path, ".cache"));
} }
await dir.close();
// TODO: else, clean `.cache` based on the `packages` // TODO: else, clean `.cache` based on the `packages`
} }
} }
await indexDir.close();
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`)); const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
// `.cargo/registry/cache` // `.cargo/registry/cache`
const cacheDir = await fs.promises.opendir(path.join(CARGO_HOME, "registry", "cache")); const cacheDir = await fs.promises.opendir(path.join(CARGO_HOME, "registry", "cache"));
@ -64700,8 +64698,10 @@ async function cleanRegistry(packages) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
} }
await cacheDir.close();
} }
async function cleanGit(packages) { async function cleanGit(packages) {
const coPath = path.join(CARGO_HOME, "git", "checkouts"); const coPath = path.join(CARGO_HOME, "git", "checkouts");
@ -64722,16 +64722,20 @@ async function cleanGit(packages) {
} }
// we have to keep both the clone, and the checkout, removing either will // we have to keep both the clone, and the checkout, removing either will
// trigger a rebuild // trigger a rebuild
let dir;
// clean the db // clean the db
dir = await fs.promises.opendir(dbPath); try {
let dir = await fs.promises.opendir(dbPath);
for await (const dirent of dir) { for await (const dirent of dir) {
if (!repos.has(dirent.name)) { if (!repos.has(dirent.name)) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
}
catch { }
// clean the checkouts // clean the checkouts
dir = await fs.promises.opendir(coPath); try {
let dir = await fs.promises.opendir(coPath);
for await (const dirent of dir) { for await (const dirent of dir) {
const refs = repos.get(dirent.name); const refs = repos.get(dirent.name);
if (!refs) { if (!refs) {
@ -64747,24 +64751,42 @@ async function cleanGit(packages) {
await rm(refsDir.path, dirent); await rm(refsDir.path, dirent);
} }
} }
await refsDir.close();
} }
await dir.close();
}
catch { }
} }
const ONE_WEEK = 7 * 24 * 3600 * 1000; const ONE_WEEK = 7 * 24 * 3600 * 1000;
async function rmExcept(dirName, keepPrefix) { /**
* Removes all files or directories in `dirName`, except the ones matching
* any string in the `keepPrefix` set.
*
* The matching strips and trailing `-$hash` suffix.
*
* When the `checkTimestamp` flag is set, this will also remove anything older
* than one week.
*/
async function rmExcept(dirName, keepPrefix, checkTimestamp = false) {
const dir = await external_fs_default().promises.opendir(dirName); const dir = await external_fs_default().promises.opendir(dirName);
for await (const dirent of dir) { for await (const dirent of dir) {
let name = dirent.name; let name = dirent.name;
// strip the trailing hash
const idx = name.lastIndexOf("-"); const idx = name.lastIndexOf("-");
if (idx !== -1) { if (idx !== -1) {
name = name.slice(0, idx); name = name.slice(0, idx);
} }
let isOutdated = false;
if (checkTimestamp) {
const fileName = external_path_default().join(dir.path, dirent.name); const fileName = external_path_default().join(dir.path, dirent.name);
const { mtime } = await external_fs_default().promises.stat(fileName); const { mtime } = await external_fs_default().promises.stat(fileName);
// we dont really know isOutdated = Date.now() - mtime.getTime() > ONE_WEEK;
if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > ONE_WEEK) { }
if (!keepPrefix.has(name) || isOutdated) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
async function rm(parent, dirent) { async function rm(parent, dirent) {
try { try {
@ -64774,13 +64796,13 @@ async function rm(parent, dirent) {
await external_fs_default().promises.unlink(fileName); await external_fs_default().promises.unlink(fileName);
} }
else if (dirent.isDirectory()) { else if (dirent.isDirectory()) {
await io.rmRF(fileName); await lib_io.rmRF(fileName);
} }
} }
catch { } catch { }
} }
async function rmRF(dirName) { async function rmRF(dirName) {
lib_core.debug(`deleting "${dirName}"`); core.debug(`deleting "${dirName}"`);
await io.rmRF(dirName); await io.rmRF(dirName);
} }
async function exists(path) { async function exists(path) {
@ -64832,7 +64854,7 @@ async function run() {
for (const workspace of config.workspaces) { for (const workspace of config.workspaces) {
try { try {
const packages = await workspace.getPackages(); const packages = await workspace.getPackages();
await cleanTargetDir(workspace.target, packages); await cleanTargetDir(workspace.target, packages, true);
} }
catch { } catch { }
} }

74
dist/save/index.js vendored
View file

@ -64394,6 +64394,7 @@ async function getCmdOutput(cmd, args = [], options = {}) {
;// CONCATENATED MODULE: ./src/workspace.ts ;// CONCATENATED MODULE: ./src/workspace.ts
const SAVE_TARGETS = new Set(["lib", "proc-macro"]);
class Workspace { class Workspace {
constructor(root, target) { constructor(root, target) {
this.root = root; this.root = root;
@ -64409,7 +64410,7 @@ class Workspace {
if (pkg.manifest_path.startsWith(this.root)) { if (pkg.manifest_path.startsWith(this.root)) {
continue; continue;
} }
const targets = pkg.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name); const targets = pkg.targets.filter((t) => t.kind.some((kind) => SAVE_TARGETS.has(kind))).map((t) => t.name);
packages.push({ name: pkg.name, version: pkg.version, targets, path: external_path_default().dirname(pkg.manifest_path) }); packages.push({ name: pkg.name, version: pkg.version, targets, path: external_path_default().dirname(pkg.manifest_path) });
} }
} }
@ -64594,10 +64595,10 @@ async function getRustVersion() {
async function cleanTargetDir(targetDir, packages) { async function cleanTargetDir(targetDir, packages, checkTimestamp = false) {
let dir; core.debug(`cleaning target directory "${targetDir}"`);
// remove all *files* from the profile directory // remove all *files* from the profile directory
dir = await external_fs_default().promises.opendir(targetDir); let dir = await external_fs_default().promises.opendir(targetDir);
for await (const dirent of dir) { for await (const dirent of dir) {
if (dirent.isDirectory()) { if (dirent.isDirectory()) {
let dirName = external_path_default().join(dir.path, dirent.name); let dirName = external_path_default().join(dir.path, dirent.name);
@ -64605,10 +64606,10 @@ async function cleanTargetDir(targetDir, packages) {
let isNestedTarget = (await exists(external_path_default().join(dirName, "CACHEDIR.TAG"))) || (await exists(external_path_default().join(dirName, ".rustc_info.json"))); let isNestedTarget = (await exists(external_path_default().join(dirName, "CACHEDIR.TAG"))) || (await exists(external_path_default().join(dirName, ".rustc_info.json")));
try { try {
if (isNestedTarget) { if (isNestedTarget) {
await cleanTargetDir(dirName, packages); await cleanTargetDir(dirName, packages, checkTimestamp);
} }
else { else {
await cleanProfileTarget(dirName, packages); await cleanProfileTarget(dirName, packages, checkTimestamp);
} }
} }
catch { } catch { }
@ -64617,21 +64618,15 @@ async function cleanTargetDir(targetDir, packages) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
async function cleanProfileTarget(profileDir, packages) { async function cleanProfileTarget(profileDir, packages, checkTimestamp = false) {
await rmRF(external_path_default().join(profileDir, "examples")); core.debug(`cleaning profile directory "${profileDir}"`);
await rmRF(external_path_default().join(profileDir, "incremental")); let keepProfile = new Set(["build", ".fingerprint", "deps"]);
let dir; await rmExcept(profileDir, keepProfile);
// remove all *files* from the profile directory
dir = await external_fs_default().promises.opendir(profileDir);
for await (const dirent of dir) {
if (dirent.isFile()) {
await rm(dir.path, dirent);
}
}
const keepPkg = new Set(packages.map((p) => p.name)); const keepPkg = new Set(packages.map((p) => p.name));
await rmExcept(external_path_default().join(profileDir, "build"), keepPkg); await rmExcept(external_path_default().join(profileDir, "build"), keepPkg, checkTimestamp);
await rmExcept(external_path_default().join(profileDir, ".fingerprint"), keepPkg); await rmExcept(external_path_default().join(profileDir, ".fingerprint"), keepPkg, checkTimestamp);
const keepDeps = new Set(packages.flatMap((p) => { const keepDeps = new Set(packages.flatMap((p) => {
const names = []; const names = [];
for (const n of [p.name, ...p.targets]) { for (const n of [p.name, ...p.targets]) {
@ -64640,7 +64635,7 @@ async function cleanProfileTarget(profileDir, packages) {
} }
return names; return names;
})); }));
await rmExcept(external_path_default().join(profileDir, "deps"), keepDeps); await rmExcept(external_path_default().join(profileDir, "deps"), keepDeps, checkTimestamp);
} }
async function getCargoBins() { async function getCargoBins() {
const bins = new Set(); const bins = new Set();
@ -64667,6 +64662,7 @@ async function cleanBin() {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
async function cleanRegistry(packages) { async function cleanRegistry(packages) {
// `.cargo/registry/src` // `.cargo/registry/src`
@ -64683,9 +64679,11 @@ async function cleanRegistry(packages) {
if (await exists(external_path_default().join(dir.path, ".git"))) { if (await exists(external_path_default().join(dir.path, ".git"))) {
await rmRF(external_path_default().join(dir.path, ".cache")); await rmRF(external_path_default().join(dir.path, ".cache"));
} }
await dir.close();
// TODO: else, clean `.cache` based on the `packages` // TODO: else, clean `.cache` based on the `packages`
} }
} }
await indexDir.close();
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`)); const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
// `.cargo/registry/cache` // `.cargo/registry/cache`
const cacheDir = await external_fs_default().promises.opendir(external_path_default().join(CARGO_HOME, "registry", "cache")); const cacheDir = await external_fs_default().promises.opendir(external_path_default().join(CARGO_HOME, "registry", "cache"));
@ -64700,8 +64698,10 @@ async function cleanRegistry(packages) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
} }
await cacheDir.close();
} }
async function cleanGit(packages) { async function cleanGit(packages) {
const coPath = external_path_default().join(CARGO_HOME, "git", "checkouts"); const coPath = external_path_default().join(CARGO_HOME, "git", "checkouts");
@ -64722,16 +64722,20 @@ async function cleanGit(packages) {
} }
// we have to keep both the clone, and the checkout, removing either will // we have to keep both the clone, and the checkout, removing either will
// trigger a rebuild // trigger a rebuild
let dir;
// clean the db // clean the db
dir = await external_fs_default().promises.opendir(dbPath); try {
let dir = await external_fs_default().promises.opendir(dbPath);
for await (const dirent of dir) { for await (const dirent of dir) {
if (!repos.has(dirent.name)) { if (!repos.has(dirent.name)) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
}
catch { }
// clean the checkouts // clean the checkouts
dir = await external_fs_default().promises.opendir(coPath); try {
let dir = await external_fs_default().promises.opendir(coPath);
for await (const dirent of dir) { for await (const dirent of dir) {
const refs = repos.get(dirent.name); const refs = repos.get(dirent.name);
if (!refs) { if (!refs) {
@ -64747,24 +64751,42 @@ async function cleanGit(packages) {
await rm(refsDir.path, dirent); await rm(refsDir.path, dirent);
} }
} }
await refsDir.close();
} }
await dir.close();
}
catch { }
} }
const ONE_WEEK = 7 * 24 * 3600 * 1000; const ONE_WEEK = 7 * 24 * 3600 * 1000;
async function rmExcept(dirName, keepPrefix) { /**
* Removes all files or directories in `dirName`, except the ones matching
* any string in the `keepPrefix` set.
*
* The matching strips and trailing `-$hash` suffix.
*
* When the `checkTimestamp` flag is set, this will also remove anything older
* than one week.
*/
async function rmExcept(dirName, keepPrefix, checkTimestamp = false) {
const dir = await external_fs_default().promises.opendir(dirName); const dir = await external_fs_default().promises.opendir(dirName);
for await (const dirent of dir) { for await (const dirent of dir) {
let name = dirent.name; let name = dirent.name;
// strip the trailing hash
const idx = name.lastIndexOf("-"); const idx = name.lastIndexOf("-");
if (idx !== -1) { if (idx !== -1) {
name = name.slice(0, idx); name = name.slice(0, idx);
} }
let isOutdated = false;
if (checkTimestamp) {
const fileName = external_path_default().join(dir.path, dirent.name); const fileName = external_path_default().join(dir.path, dirent.name);
const { mtime } = await external_fs_default().promises.stat(fileName); const { mtime } = await external_fs_default().promises.stat(fileName);
// we dont really know isOutdated = Date.now() - mtime.getTime() > ONE_WEEK;
if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > ONE_WEEK) { }
if (!keepPrefix.has(name) || isOutdated) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
async function rm(parent, dirent) { async function rm(parent, dirent) {
try { try {

View file

@ -6,10 +6,11 @@ import path from "path";
import { CARGO_HOME, STATE_BINS } from "./config"; import { CARGO_HOME, STATE_BINS } from "./config";
import { Packages } from "./workspace"; import { Packages } from "./workspace";
export async function cleanTargetDir(targetDir: string, packages: Packages) { export async function cleanTargetDir(targetDir: string, packages: Packages, checkTimestamp = false) {
let dir: fs.Dir; core.debug(`cleaning target directory "${targetDir}"`);
// remove all *files* from the profile directory // remove all *files* from the profile directory
dir = await fs.promises.opendir(targetDir); let dir = await fs.promises.opendir(targetDir);
for await (const dirent of dir) { for await (const dirent of dir) {
if (dirent.isDirectory()) { if (dirent.isDirectory()) {
let dirName = path.join(dir.path, dirent.name); let dirName = path.join(dir.path, dirent.name);
@ -19,33 +20,27 @@ export async function cleanTargetDir(targetDir: string, packages: Packages) {
try { try {
if (isNestedTarget) { if (isNestedTarget) {
await cleanTargetDir(dirName, packages); await cleanTargetDir(dirName, packages, checkTimestamp);
} else { } else {
await cleanProfileTarget(dirName, packages); await cleanProfileTarget(dirName, packages, checkTimestamp);
} }
} catch {} } catch {}
} else if (dirent.name !== "CACHEDIR.TAG") { } else if (dirent.name !== "CACHEDIR.TAG") {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
async function cleanProfileTarget(profileDir: string, packages: Packages) { async function cleanProfileTarget(profileDir: string, packages: Packages, checkTimestamp = false) {
await rmRF(path.join(profileDir, "examples")); core.debug(`cleaning profile directory "${profileDir}"`);
await rmRF(path.join(profileDir, "incremental"));
let dir: fs.Dir; let keepProfile = new Set(["build", ".fingerprint", "deps"]);
// remove all *files* from the profile directory await rmExcept(profileDir, keepProfile);
dir = await fs.promises.opendir(profileDir);
for await (const dirent of dir) {
if (dirent.isFile()) {
await rm(dir.path, dirent);
}
}
const keepPkg = new Set(packages.map((p) => p.name)); const keepPkg = new Set(packages.map((p) => p.name));
await rmExcept(path.join(profileDir, "build"), keepPkg); await rmExcept(path.join(profileDir, "build"), keepPkg, checkTimestamp);
await rmExcept(path.join(profileDir, ".fingerprint"), keepPkg); await rmExcept(path.join(profileDir, ".fingerprint"), keepPkg, checkTimestamp);
const keepDeps = new Set( const keepDeps = new Set(
packages.flatMap((p) => { packages.flatMap((p) => {
@ -57,7 +52,7 @@ async function cleanProfileTarget(profileDir: string, packages: Packages) {
return names; return names;
}), }),
); );
await rmExcept(path.join(profileDir, "deps"), keepDeps); await rmExcept(path.join(profileDir, "deps"), keepDeps, checkTimestamp);
} }
export async function getCargoBins(): Promise<Set<string>> { export async function getCargoBins(): Promise<Set<string>> {
@ -89,6 +84,7 @@ export async function cleanBin() {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
export async function cleanRegistry(packages: Packages) { export async function cleanRegistry(packages: Packages) {
@ -108,9 +104,11 @@ export async function cleanRegistry(packages: Packages) {
if (await exists(path.join(dir.path, ".git"))) { if (await exists(path.join(dir.path, ".git"))) {
await rmRF(path.join(dir.path, ".cache")); await rmRF(path.join(dir.path, ".cache"));
} }
await dir.close();
// TODO: else, clean `.cache` based on the `packages` // TODO: else, clean `.cache` based on the `packages`
} }
} }
await indexDir.close();
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`)); const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
@ -127,8 +125,10 @@ export async function cleanRegistry(packages: Packages) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
} }
await cacheDir.close();
} }
export async function cleanGit(packages: Packages) { export async function cleanGit(packages: Packages) {
@ -151,17 +151,20 @@ export async function cleanGit(packages: Packages) {
// we have to keep both the clone, and the checkout, removing either will // we have to keep both the clone, and the checkout, removing either will
// trigger a rebuild // trigger a rebuild
let dir: fs.Dir;
// clean the db // clean the db
dir = await fs.promises.opendir(dbPath); try {
let dir = await fs.promises.opendir(dbPath);
for await (const dirent of dir) { for await (const dirent of dir) {
if (!repos.has(dirent.name)) { if (!repos.has(dirent.name)) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} catch {}
// clean the checkouts // clean the checkouts
dir = await fs.promises.opendir(coPath); try {
let dir = await fs.promises.opendir(coPath);
for await (const dirent of dir) { for await (const dirent of dir) {
const refs = repos.get(dirent.name); const refs = repos.get(dirent.name);
if (!refs) { if (!refs) {
@ -177,26 +180,46 @@ export async function cleanGit(packages: Packages) {
await rm(refsDir.path, dirent); await rm(refsDir.path, dirent);
} }
} }
await refsDir.close();
} }
await dir.close();
} catch {}
} }
const ONE_WEEK = 7 * 24 * 3600 * 1000; const ONE_WEEK = 7 * 24 * 3600 * 1000;
async function rmExcept(dirName: string, keepPrefix: Set<string>) { /**
* Removes all files or directories in `dirName`, except the ones matching
* any string in the `keepPrefix` set.
*
* The matching strips and trailing `-$hash` suffix.
*
* When the `checkTimestamp` flag is set, this will also remove anything older
* than one week.
*/
async function rmExcept(dirName: string, keepPrefix: Set<string>, checkTimestamp = false) {
const dir = await fs.promises.opendir(dirName); const dir = await fs.promises.opendir(dirName);
for await (const dirent of dir) { for await (const dirent of dir) {
let name = dirent.name; let name = dirent.name;
// strip the trailing hash
const idx = name.lastIndexOf("-"); const idx = name.lastIndexOf("-");
if (idx !== -1) { if (idx !== -1) {
name = name.slice(0, idx); name = name.slice(0, idx);
} }
let isOutdated = false;
if (checkTimestamp) {
const fileName = path.join(dir.path, dirent.name); const fileName = path.join(dir.path, dirent.name);
const { mtime } = await fs.promises.stat(fileName); const { mtime } = await fs.promises.stat(fileName);
// we dont really know isOutdated = Date.now() - mtime.getTime() > ONE_WEEK;
if (!keepPrefix.has(name) || Date.now() - mtime.getTime() > ONE_WEEK) { }
if (!keepPrefix.has(name) || isOutdated) {
await rm(dir.path, dirent); await rm(dir.path, dirent);
} }
} }
await dir.close();
} }
async function rm(parent: string, dirent: fs.Dirent) { async function rm(parent: string, dirent: fs.Dirent) {

View file

@ -45,7 +45,7 @@ async function run() {
try { try {
const packages = await workspace.getPackages(); const packages = await workspace.getPackages();
await cleanTargetDir(workspace.target, packages); await cleanTargetDir(workspace.target, packages, true);
} catch {} } catch {}
} }
} }

View file

@ -2,6 +2,8 @@ import path from "path";
import { getCmdOutput } from "./utils"; import { getCmdOutput } from "./utils";
const SAVE_TARGETS = new Set(["lib", "proc-macro"]);
export class Workspace { export class Workspace {
constructor(public root: string, public target: string) {} constructor(public root: string, public target: string) {}
@ -17,7 +19,7 @@ export class Workspace {
if (pkg.manifest_path.startsWith(this.root)) { if (pkg.manifest_path.startsWith(this.root)) {
continue; continue;
} }
const targets = pkg.targets.filter((t) => t.kind[0] === "lib").map((t) => t.name); const targets = pkg.targets.filter((t) => t.kind.some((kind) => SAVE_TARGETS.has(kind))).map((t) => t.name);
packages.push({ name: pkg.name, version: pkg.version, targets, path: path.dirname(pkg.manifest_path) }); packages.push({ name: pkg.name, version: pkg.version, targets, path: path.dirname(pkg.manifest_path) });
} }
} catch {} } catch {}