Skip to content

Commit a426f82

Browse files
authored
Merge pull request #4082 from d10c/d10c/cache-cleanup-overlay
New command: expose `--cache-cleanup=overlay` base cache cleaning
2 parents bb2f7d0 + 8fbb6b3 commit a426f82

File tree

8 files changed

+149
-0
lines changed

8 files changed

+149
-0
lines changed

extensions/ql-vscode/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [UNRELEASED]
44

5+
- Add new command "CodeQL: Trim Overlay Base Cache" that returns a database to the state prior to overlay evaluation, leaving only base predicates and types that may later be referenced during overlay evaluation. [#4082](https://github.com/github/vscode-codeql/pull/4082)
6+
57
## 1.17.4 - 10 July 2025
68

79
- Fix variant analysis pack creation on some Windows systems [#4068](https://github.com/github/vscode-codeql/pull/4068)

extensions/ql-vscode/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,10 @@
790790
"command": "codeQL.trimCache",
791791
"title": "CodeQL: Trim Cache"
792792
},
793+
{
794+
"command": "codeQL.trimOverlayBaseCache",
795+
"title": "CodeQL: Trim Overlay Base Cache"
796+
},
793797
{
794798
"command": "codeQL.installPackDependencies",
795799
"title": "CodeQL: Install Pack Dependencies"
@@ -1817,6 +1821,10 @@
18171821
},
18181822
{
18191823
"command": "codeQL.trimCache"
1824+
},
1825+
{
1826+
"command": "codeQL.trimOverlayBaseCache",
1827+
"when": "codeQL.cliFeatures.queryServerTrimCacheWithMode"
18201828
}
18211829
],
18221830
"editor/context": [

extensions/ql-vscode/src/common/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export type LocalDatabasesCommands = {
221221
"codeQL.upgradeCurrentDatabase": () => Promise<void>;
222222
"codeQL.clearCache": () => Promise<void>;
223223
"codeQL.trimCache": () => Promise<void>;
224+
"codeQL.trimOverlayBaseCache": () => Promise<void>;
224225

225226
// Explorer context menu
226227
"codeQL.setCurrentDatabase": (uri: Uri) => Promise<void>;

extensions/ql-vscode/src/databases/local-databases-ui.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ export class DatabaseUI extends DisposableObject {
284284
this.handleUpgradeCurrentDatabase.bind(this),
285285
"codeQL.clearCache": this.handleClearCache.bind(this),
286286
"codeQL.trimCache": this.handleTrimCache.bind(this),
287+
"codeQL.trimOverlayBaseCache": this.handleTrimOverlayBaseCache.bind(this),
287288
"codeQLDatabases.chooseDatabaseFolder":
288289
this.handleChooseDatabaseFolder.bind(this),
289290
"codeQLDatabases.chooseDatabaseArchive":
@@ -688,6 +689,25 @@ export class DatabaseUI extends DisposableObject {
688689
);
689690
}
690691

692+
private async handleTrimOverlayBaseCache(): Promise<void> {
693+
return withProgress(
694+
async () => {
695+
if (
696+
this.queryServer !== undefined &&
697+
this.databaseManager.currentDatabaseItem !== undefined
698+
) {
699+
await this.queryServer.trimCacheWithModeInDatabase(
700+
this.databaseManager.currentDatabaseItem,
701+
"overlay",
702+
);
703+
}
704+
},
705+
{
706+
title: "Removing all overlay-dependent data from cache",
707+
},
708+
);
709+
}
710+
691711
private async handleGetCurrentDatabase(): Promise<string | undefined> {
692712
const dbItem = await this.getDatabaseItemInternal(undefined);
693713
return dbItem?.databaseUri.fsPath;

extensions/ql-vscode/src/extension.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,25 @@ export async function activate(
463463
);
464464
unsupportedWarningShown = true;
465465
});
466+
467+
// Expose the CodeQL CLI features to the extension context under `codeQL.cliFeatures.*`.
468+
let cliFeatures: { [feature: string]: boolean | undefined } = {};
469+
codeQlExtension.cliServer.addVersionChangedListener(async (ver) => {
470+
for (const feat of Object.keys(cliFeatures)) {
471+
cliFeatures[feat] = false;
472+
}
473+
cliFeatures = {
474+
...cliFeatures,
475+
...(ver?.features ?? {}),
476+
};
477+
for (const feat of Object.keys(cliFeatures)) {
478+
await app.commands.execute(
479+
"setContext",
480+
`codeQL.cliFeatures.${feat}`,
481+
cliFeatures[feat] ?? false,
482+
);
483+
}
484+
});
466485
}
467486

468487
return codeQlExtension;

extensions/ql-vscode/src/query-server/messages.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ export interface TrimCacheParams {
4242
db: string;
4343
}
4444

45+
/**
46+
* Parameters for trimming the cache of a dataset with a specific mode.
47+
*/
48+
export interface TrimCacheWithModeParams {
49+
/**
50+
* The dataset that we want to trim the cache of.
51+
*/
52+
db: string;
53+
/**
54+
* The cache cleanup mode to use.
55+
*/
56+
mode: ClearCacheMode;
57+
}
58+
59+
export type ClearCacheMode = "clear" | "trim" | "fit" | "overlay";
60+
4561
/**
4662
* The result of trimming or clearing the cache.
4763
*/
@@ -193,6 +209,14 @@ export const trimCache = new RequestType<
193209
ClearCacheResult,
194210
void
195211
>("evaluation/trimCache");
212+
/**
213+
* Trim the cache of a dataset with a specific mode.
214+
*/
215+
export const trimCacheWithMode = new RequestType<
216+
WithProgressId<TrimCacheWithModeParams>,
217+
ClearCacheResult,
218+
void
219+
>("evaluation/trimCacheWithMode");
196220

197221
/**
198222
* Clear the pack cache

extensions/ql-vscode/src/query-server/query-runner.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ import { UserCancellationException } from "../common/vscode/progress";
66
import type { DatabaseItem } from "../databases/local-databases/database-item";
77
import { QueryOutputDir } from "../local-queries/query-output-dir";
88
import type {
9+
ClearCacheMode,
910
ClearCacheParams,
1011
Position,
1112
QueryResultType,
1213
TrimCacheParams,
14+
TrimCacheWithModeParams,
1315
} from "./messages";
1416
import {
1517
clearCache,
1618
clearPackCache,
1719
deregisterDatabases,
1820
registerDatabases,
1921
trimCache,
22+
trimCacheWithMode,
2023
upgradeDatabase,
2124
} from "./messages";
2225
import type { BaseLogger, Logger } from "../common/logging";
@@ -142,6 +145,22 @@ export class QueryRunner {
142145
await this.qs.sendRequest(trimCache, params);
143146
}
144147

148+
async trimCacheWithModeInDatabase(
149+
dbItem: DatabaseItem,
150+
mode: ClearCacheMode,
151+
): Promise<void> {
152+
if (dbItem.contents === undefined) {
153+
throw new Error("Can't clean the cache in an invalid database.");
154+
}
155+
156+
const db = dbItem.databaseUri.fsPath;
157+
const params: TrimCacheWithModeParams = {
158+
db,
159+
mode,
160+
};
161+
await this.qs.sendRequest(trimCacheWithMode, params);
162+
}
163+
145164
public async compileAndRunQueryAgainstDatabaseCore(
146165
dbPath: string,
147166
queries: CoreQueryTarget[],

extensions/ql-vscode/test/vscode-tests/cli-integration/query-server/query-server-client.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { dirSync } from "tmp";
33
import { CancellationTokenSource } from "vscode-jsonrpc";
44
import type { RunQueryParams } from "../../../../src/query-server/messages";
55
import {
6+
clearCache,
67
QueryResultType,
78
registerDatabases,
89
runQuery,
10+
trimCache,
11+
trimCacheWithMode,
912
} from "../../../../src/query-server/messages";
1013
import type { CodeQLCliServer } from "../../../../src/codeql-cli/cli";
1114
import type { BqrsCellValue } from "../../../../src/common/bqrs-cli-types";
@@ -198,5 +201,58 @@ describeWithCodeQL()("using the query server", () => {
198201
);
199202
}
200203
});
204+
205+
it("should invoke codeQL.trimOverlayBaseCache command when queryServerTrimCacheWithMode is enabled", async () => {
206+
const features = (await cliServer.getFeatures()) as {
207+
[feature: string]: boolean | undefined;
208+
};
209+
210+
// Register the database first (if not already done)
211+
await qs.sendRequest(registerDatabases, { databases: [db] });
212+
213+
try {
214+
// Send the trimCacheWithMode request
215+
const params = {
216+
db,
217+
mode: "overlay",
218+
};
219+
const result = await qs.sendRequest(
220+
trimCacheWithMode,
221+
params,
222+
token,
223+
() => {},
224+
);
225+
226+
// The result should contain a deletionMessage string
227+
expect(result).toHaveProperty("deletionMessage");
228+
expect(typeof result.deletionMessage).toBe("string");
229+
expect(features.queryServerTrimCacheWithMode).toBeTruthy();
230+
} catch (e) {
231+
expect(features.queryServerTrimCacheWithMode).toBeFalsy();
232+
expect((e as Error).message).toContain(
233+
"Unsupported request method: evaluation/trimCacheWithMode",
234+
);
235+
}
236+
});
237+
238+
it("should invoke trimCache command and receive a deletionMessage", async () => {
239+
// Register the database first (if not already done)
240+
await qs.sendRequest(registerDatabases, { databases: [db] });
241+
242+
const params = { db };
243+
const result = await qs.sendRequest(trimCache, params, token, () => {});
244+
expect(result).toHaveProperty("deletionMessage");
245+
expect(typeof result.deletionMessage).toBe("string");
246+
});
247+
248+
it("should invoke clearCache command and receive a deletionMessage", async () => {
249+
// Register the database first (if not already done)
250+
await qs.sendRequest(registerDatabases, { databases: [db] });
251+
252+
const params = { db, dryRun: false };
253+
const result = await qs.sendRequest(clearCache, params, token, () => {});
254+
expect(result).toHaveProperty("deletionMessage");
255+
expect(typeof result.deletionMessage).toBe("string");
256+
});
201257
}
202258
});

0 commit comments

Comments
 (0)