diff --git a/src/modules/directoryCleaner.ts b/src/modules/directoryCleaner.ts index 3e9d13a9c..9a9577164 100644 --- a/src/modules/directoryCleaner.ts +++ b/src/modules/directoryCleaner.ts @@ -63,10 +63,14 @@ export default class DirectoryCleaner extends Module { } try { - const emptyDirs = await DirectoryCleaner.getEmptyDirs(dirsToClean); - await this.progressBar.reset(emptyDirs.length); - this.progressBar.logTrace(`cleaning ${emptyDirs.length.toLocaleString()} empty director${emptyDirs.length !== 1 ? 'ies' : 'y'}`); - await this.trashOrDelete(emptyDirs); + let emptyDirs = await DirectoryCleaner.getEmptyDirs(dirsToClean); + while (emptyDirs.length > 0) { + await this.progressBar.reset(emptyDirs.length); + this.progressBar.logTrace(`cleaning ${emptyDirs.length.toLocaleString()} empty director${emptyDirs.length !== 1 ? 'ies' : 'y'}`); + await this.trashOrDelete(emptyDirs); + // Deleting some empty directories could leave others newly empty + emptyDirs = await DirectoryCleaner.getEmptyDirs(dirsToClean); + } } catch (error) { this.progressBar.logError(`failed to clean empty directories: ${error}`); } diff --git a/src/polyfill/fsPoly.ts b/src/polyfill/fsPoly.ts index 3c9996fb1..cb227128f 100644 --- a/src/polyfill/fsPoly.ts +++ b/src/polyfill/fsPoly.ts @@ -306,11 +306,11 @@ export default class FsPoly { try { await fs.promises.access(pathLike); // throw if file doesn't exist - } catch (error) { + } catch { if (optionsWithRetry?.force) { return; } - throw error; + throw new Error(`can't rm, path doesn't exist: ${pathLike}`); } if (await this.isDirectory(pathLike)) { diff --git a/test/modules/directoryCleaner.test.ts b/test/modules/directoryCleaner.test.ts index 1026fc339..dd19f45de 100644 --- a/test/modules/directoryCleaner.test.ts +++ b/test/modules/directoryCleaner.test.ts @@ -41,7 +41,7 @@ async function runOutputCleaner( const after = await fsPoly.walk(tempDir); // Test cleanup - await fsPoly.rm(tempDir, { recursive: true }); + await fsPoly.rm(tempDir, { recursive: true, force: true }); return after .map((pathLike) => pathLike.replace(tempDir + path.sep, ''))