Skip to content

Commit d7a82cc

Browse files
authored
Merge pull request #3616 from github/aeisenberg/flush-cache-on-db-remove
Fix bug with reimporting test cases
2 parents 68c159f + 39ad5b2 commit d7a82cc

File tree

7 files changed

+58
-31
lines changed

7 files changed

+58
-31
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+
- Fix a bug when re-importing test databases that erroneously showed old source code. [#3616](https://github.com/github/vscode-codeql/pull/3616)
6+
57
## 1.13.0 - 1 May 2024
68

79
- Add Ruby support to the CodeQL Model Editor. [#3584](https://github.com/github/vscode-codeql/pull/3584)

extensions/ql-vscode/src/common/vscode/archive-filesystem-provider.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {
2626
// All path operations in this file must be on paths *within* the zip
2727
// archive.
2828
import { posix } from "path";
29+
import { DatabaseEventKind } from "../../databases/local-databases/database-events";
30+
import type { DatabaseManager } from "../../databases/local-databases/database-manager";
2931

3032
const path = posix;
3133

@@ -242,15 +244,8 @@ export class ArchiveFileSystemProvider implements FileSystemProvider {
242244

243245
root = new Directory("");
244246

245-
constructor() {
246-
// When a file system archive is removed from the workspace, we should
247-
// also remove it from our cache.
248-
workspace.onDidChangeWorkspaceFolders((event) => {
249-
for (const removed of event.removed) {
250-
const zipPath = removed.uri.fsPath;
251-
this.archives.delete(zipPath);
252-
}
253-
});
247+
flushCache(zipPath: string) {
248+
this.archives.delete(zipPath);
254249
}
255250

256251
// metadata
@@ -366,15 +361,35 @@ export class ArchiveFileSystemProvider implements FileSystemProvider {
366361
*/
367362
export const zipArchiveScheme = "codeql-zip-archive";
368363

369-
export function activate(ctx: ExtensionContext) {
364+
export function activate(ctx: ExtensionContext, dbm?: DatabaseManager) {
365+
const afsp = new ArchiveFileSystemProvider();
366+
if (dbm) {
367+
ctx.subscriptions.push(
368+
dbm.onDidChangeDatabaseItem(async ({ kind, item: db }) => {
369+
if (kind === DatabaseEventKind.Remove) {
370+
if (db?.sourceArchive) {
371+
afsp.flushCache(db.sourceArchive.fsPath);
372+
}
373+
}
374+
}),
375+
);
376+
}
377+
370378
ctx.subscriptions.push(
371-
workspace.registerFileSystemProvider(
372-
zipArchiveScheme,
373-
new ArchiveFileSystemProvider(),
374-
{
375-
isCaseSensitive: true,
376-
isReadonly: true,
377-
},
378-
),
379+
// When a file system archive is removed from the workspace, we should
380+
// also remove it from our cache.
381+
workspace.onDidChangeWorkspaceFolders((event) => {
382+
for (const removed of event.removed) {
383+
const zipPath = removed.uri.fsPath;
384+
afsp.flushCache(zipPath);
385+
}
386+
}),
387+
);
388+
389+
ctx.subscriptions.push(
390+
workspace.registerFileSystemProvider(zipArchiveScheme, afsp, {
391+
isCaseSensitive: true,
392+
isReadonly: true,
393+
}),
379394
);
380395
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ class DatabaseTreeDataProvider
109109
// Note that events from the database manager are instances of DatabaseChangedEvent
110110
// and events fired by the UI are instances of DatabaseItem
111111

112-
// When event.item is undefined, then the entire tree is refreshed.
113-
// When event.item is a db item, then only that item is refreshed.
114-
this._onDidChangeTreeData.fire(event.item);
112+
// When a full refresh has occurred, then all items are refreshed by passing undefined.
113+
this._onDidChangeTreeData.fire(event.fullRefresh ? undefined : event.item);
115114
}
116115

117116
private handleDidChangeCurrentDatabaseItem(

extensions/ql-vscode/src/databases/local-databases/database-events.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ export enum DatabaseEventKind {
1616
export interface DatabaseChangedEvent {
1717
kind: DatabaseEventKind;
1818
item: DatabaseItem | undefined;
19+
// If true, event handlers should consider the database manager
20+
// to have been fully refreshed. Any state managed by the
21+
// event handler should be fully refreshed as well.
22+
fullRefresh: boolean;
1923
}

extensions/ql-vscode/src/databases/local-databases/database-manager.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ export class DatabaseManager extends DisposableObject {
613613
this._onDidChangeCurrentDatabaseItem.fire({
614614
item,
615615
kind: DatabaseEventKind.Change,
616+
fullRefresh: false,
616617
});
617618
}
618619
}
@@ -662,18 +663,19 @@ export class DatabaseManager extends DisposableObject {
662663
}
663664
// note that we use undefined as the item in order to reset the entire tree
664665
this._onDidChangeDatabaseItem.fire({
665-
item: undefined,
666+
item,
666667
kind: DatabaseEventKind.Add,
668+
fullRefresh: true,
667669
});
668670
}
669671

670672
public async renameDatabaseItem(item: DatabaseItem, newName: string) {
671673
item.name = newName;
672674
await this.updatePersistedDatabaseList();
673675
this._onDidChangeDatabaseItem.fire({
674-
// pass undefined so that the entire tree is rebuilt in order to re-sort
675-
item: undefined,
676+
item,
676677
kind: DatabaseEventKind.Rename,
678+
fullRefresh: true,
677679
});
678680
}
679681

@@ -720,10 +722,10 @@ export class DatabaseManager extends DisposableObject {
720722
);
721723
}
722724

723-
// note that we use undefined as the item in order to reset the entire tree
724725
this._onDidChangeDatabaseItem.fire({
725-
item: undefined,
726+
item,
726727
kind: DatabaseEventKind.Remove,
728+
fullRefresh: true,
727729
});
728730
}
729731

@@ -776,6 +778,7 @@ export class DatabaseManager extends DisposableObject {
776778
this._onDidChangeDatabaseItem.fire({
777779
kind: DatabaseEventKind.Refresh,
778780
item: databaseItem,
781+
fullRefresh: false,
779782
});
780783
}
781784
}

extensions/ql-vscode/src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ async function activateWithInstalledDistribution(
947947
ctx.subscriptions.push(compareView);
948948

949949
void extLogger.log("Initializing source archive filesystem provider.");
950-
archiveFilesystemProvider_activate(ctx);
950+
archiveFilesystemProvider_activate(ctx, dbm);
951951

952952
const qhelpTmpDir = dirSync({
953953
prefix: "qhelp_",

extensions/ql-vscode/test/vscode-tests/minimal-workspace/local-queries/local-databases.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ describe("local databases", () => {
140140
},
141141
]);
142142
expect(onDidChangeDatabaseItem).toHaveBeenCalledWith({
143-
item: undefined,
143+
fullRefresh: true,
144+
item: mockDbItem,
144145
kind: DatabaseEventKind.Add,
145146
});
146147

@@ -152,7 +153,8 @@ describe("local databases", () => {
152153
expect((databaseManager as any)._databaseItems).toEqual([]);
153154
expect(updateSpy).toHaveBeenCalledWith("databaseList", []);
154155
expect(onDidChangeDatabaseItem).toHaveBeenCalledWith({
155-
item: undefined,
156+
fullRefresh: true,
157+
item: mockDbItem,
156158
kind: DatabaseEventKind.Remove,
157159
});
158160
});
@@ -175,7 +177,8 @@ describe("local databases", () => {
175177
]);
176178

177179
expect(onDidChangeDatabaseItem).toHaveBeenCalledWith({
178-
item: undefined,
180+
fullRefresh: true,
181+
item: mockDbItem,
179182
kind: DatabaseEventKind.Rename,
180183
});
181184
});
@@ -198,7 +201,8 @@ describe("local databases", () => {
198201
]);
199202

200203
const mockEvent = {
201-
item: undefined,
204+
fullRefresh: true,
205+
item: mockDbItem,
202206
kind: DatabaseEventKind.Add,
203207
};
204208
expect(onDidChangeDatabaseItem).toHaveBeenCalledWith(mockEvent);

0 commit comments

Comments
 (0)