Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: make multiple passes to delete empty directories #1197

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/modules/directoryCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/polyfill/fsPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion test/modules/directoryCleaner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, ''))
Expand Down
Loading