Skip to content

Commit dc168dc

Browse files
committed
fix: Clean only current cache version
1 parent b0c9252 commit dc168dc

2 files changed

Lines changed: 40 additions & 53 deletions

File tree

packages/project/lib/build/cache/CacheManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const log = getLogger("build:cache:CacheManager");
1010
const cacheManagerInstances = new Map();
1111

1212
// Cache version for compatibility management
13-
const CACHE_VERSION = "v0_7";
13+
export const CACHE_VERSION = "v0_7";
1414

1515
/**
1616
* Manages persistence for the build cache using a unified SQLite-backed storage

packages/project/lib/cache/CacheCleanup.js

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "node:path";
22
import fs from "node:fs/promises";
33
import BuildCacheStorage from "../build/cache/BuildCacheStorage.js";
4+
import {CACHE_VERSION} from "../build/cache/CacheManager.js";
45

56
// ========================================
67
// SHARED UTILITIES
@@ -99,89 +100,75 @@ async function cleanFrameworkCache(ui5DataDir, frameworkInfo) {
99100

100101
/**
101102
* Check if build cache exists and get its info.
103+
* Only checks the current known cache version to avoid processing unknown future versions.
102104
*
103105
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
104106
* @returns {Promise<{path: string, size: number, type: string}|null>} Build cache info or null
105107
*/
106108
async function getBuildCacheInfo(ui5DataDir) {
107109
const buildCacheDir = path.join(ui5DataDir, "buildCache");
108-
try {
109-
await fs.access(buildCacheDir);
110-
const versionDirs = await fs.readdir(buildCacheDir, {withFileTypes: true});
110+
const dbDir = path.join(buildCacheDir, CACHE_VERSION);
111111

112-
let hasAnyRecords = false;
113-
for (const versionDir of versionDirs) {
114-
if (!versionDir.isDirectory()) {
115-
continue;
116-
}
112+
try {
113+
await fs.access(dbDir);
114+
} catch {
115+
// Current version directory doesn't exist
116+
return null;
117+
}
117118

118-
const dbDir = path.join(buildCacheDir, versionDir.name);
119-
try {
120-
const storage = new BuildCacheStorage(dbDir);
121-
if (storage.hasRecords()) {
122-
hasAnyRecords = true;
123-
storage.close();
124-
break;
125-
}
126-
storage.close();
127-
} catch {
128-
// Skip if database can't be opened
119+
try {
120+
const storage = new BuildCacheStorage(dbDir);
121+
try {
122+
if (storage.hasRecords()) {
123+
const size = await getDirectorySize(buildCacheDir);
124+
return {
125+
path: `buildCache/${CACHE_VERSION} (database records)`,
126+
size,
127+
type: "database"
128+
};
129129
}
130-
}
131-
132-
if (hasAnyRecords) {
133-
const size = await getDirectorySize(buildCacheDir);
134-
return {
135-
path: "buildCache/ (database records)",
136-
size,
137-
type: "database"
138-
};
130+
} finally {
131+
storage.close();
139132
}
140133
} catch {
141-
// Directory doesn't exist
134+
// Skip if database can't be opened
142135
}
143136
return null;
144137
}
145138

146139
/**
147-
* Clean build cache by clearing all records from SQLite databases.
140+
* Clean build cache by clearing all records from SQLite database.
141+
* Only cleans the current known cache version to avoid processing unknown future versions.
148142
*
149143
* @param {string} ui5DataDir Resolved absolute path to UI5 data directory
150144
* @returns {Promise<Array<{path: string, type: string, size: number}>>} Removed entries
151145
*/
152146
async function cleanBuildCache(ui5DataDir) {
153147
const buildCacheDir = path.join(ui5DataDir, "buildCache");
148+
const dbDir = path.join(buildCacheDir, CACHE_VERSION);
154149
const removed = [];
155150

156151
try {
157-
await fs.access(buildCacheDir);
152+
await fs.access(dbDir);
158153
} catch {
154+
// Current version directory doesn't exist
159155
return removed;
160156
}
161157

162-
let cacheVersionDirs;
163158
try {
164-
cacheVersionDirs = await fs.readdir(buildCacheDir, {withFileTypes: true});
165-
} catch {
166-
return removed;
167-
}
168-
169-
for (const versionDir of cacheVersionDirs) {
170-
if (!versionDir.isDirectory()) {
171-
continue;
172-
}
173-
174-
const dbDir = path.join(buildCacheDir, versionDir.name);
175-
176159
const storage = new BuildCacheStorage(dbDir);
177-
const freedSize = storage.clearAllRecords();
178-
storage.close();
179-
180-
removed.push({
181-
path: `buildCache/${versionDir.name}`,
182-
type: "buildCache",
183-
size: freedSize,
184-
});
160+
try {
161+
const freedSize = storage.clearAllRecords();
162+
removed.push({
163+
path: `buildCache/${CACHE_VERSION}`,
164+
type: "buildCache",
165+
size: freedSize,
166+
});
167+
} finally {
168+
storage.close();
169+
}
170+
} catch {
171+
// Skip if database can't be cleared
185172
}
186173

187174
return removed;

0 commit comments

Comments
 (0)