@@ -17,6 +17,7 @@ import {
1717} from "../../common/vscode/archive-filesystem-provider" ;
1818import type { CodeQLCliServer } from "../../codeql-cli/cli" ;
1919import type { DatabaseManager } from "../../databases/local-databases" ;
20+ import { DatabaseEventKind } from "../../databases/local-databases" ;
2021import { CachedOperation } from "./cached-operation" ;
2122import type { ProgressCallback } from "../../common/vscode/progress" ;
2223import { withProgress } from "../../common/vscode/progress" ;
@@ -59,16 +60,25 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
5960 private queryStorageDir : string ,
6061 ) {
6162 this . cache = new CachedOperation ( this . getDefinitions . bind ( this ) ) ;
63+ this . dbm . onDidChangeDatabaseItem ( ( { kind } ) => {
64+ if ( kind === DatabaseEventKind . Remove ) {
65+ this . cache . reset ( ) ;
66+ }
67+ } ) ;
6268 }
6369
6470 async provideDefinition (
6571 document : TextDocument ,
6672 position : Position ,
6773 token : CancellationToken ,
6874 ) : Promise < LocationLink [ ] > {
75+ const uriString = document . uri . toString ( ) ;
76+ if ( this . shouldUseCache ( ) && this . cache . has ( uriString ) ) {
77+ await maybeReimportTestDatabase ( this . dbm , document . uri ) ;
78+ }
6979 const fileLinks = this . shouldUseCache ( )
70- ? await this . cache . get ( document . uri . toString ( ) , token )
71- : await this . getDefinitions ( document . uri . toString ( ) , token ) ;
80+ ? await this . cache . get ( uriString , token )
81+ : await this . getDefinitions ( uriString , token ) ;
7282
7383 const locLinks : LocationLink [ ] = [ ] ;
7484 for ( const link of fileLinks ) {
@@ -119,6 +129,11 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
119129 private queryStorageDir : string ,
120130 ) {
121131 this . cache = new CachedOperation ( this . getReferences . bind ( this ) ) ;
132+ this . dbm . onDidChangeDatabaseItem ( ( { kind } ) => {
133+ if ( kind === DatabaseEventKind . Remove ) {
134+ this . cache . reset ( ) ;
135+ }
136+ } ) ;
122137 }
123138
124139 async provideReferences (
@@ -127,9 +142,13 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
127142 _context : ReferenceContext ,
128143 token : CancellationToken ,
129144 ) : Promise < Location [ ] > {
145+ const uriString = document . uri . toString ( ) ;
146+ if ( this . shouldUseCache ( ) && this . cache . has ( uriString ) ) {
147+ await maybeReimportTestDatabase ( this . dbm , document . uri ) ;
148+ }
130149 const fileLinks = this . shouldUseCache ( )
131- ? await this . cache . get ( document . uri . toString ( ) , token )
132- : await this . getReferences ( document . uri . toString ( ) , token ) ;
150+ ? await this . cache . get ( uriString , token )
151+ : await this . getReferences ( uriString , token ) ;
133152
134153 const locLinks : Location [ ] = [ ] ;
135154 for ( const link of fileLinks ) {
@@ -193,6 +212,11 @@ export class TemplatePrintAstProvider {
193212 private queryStorageDir : string ,
194213 ) {
195214 this . cache = new CachedOperation ( this . getAst . bind ( this ) ) ;
215+ this . dbm . onDidChangeDatabaseItem ( ( { kind } ) => {
216+ if ( kind === DatabaseEventKind . Remove ) {
217+ this . cache . reset ( ) ;
218+ }
219+ } ) ;
196220 }
197221
198222 async provideAst (
@@ -205,9 +229,13 @@ export class TemplatePrintAstProvider {
205229 "Cannot view the AST. Please select a valid source file inside a CodeQL database." ,
206230 ) ;
207231 }
232+ const uriString = fileUri . toString ( ) ;
233+ if ( this . shouldUseCache ( ) && this . cache . has ( uriString ) ) {
234+ await maybeReimportTestDatabase ( this . dbm , fileUri ) ;
235+ }
208236 const completedQuery = this . shouldUseCache ( )
209- ? await this . cache . get ( fileUri . toString ( ) , progress , token )
210- : await this . getAst ( fileUri . toString ( ) , progress , token ) ;
237+ ? await this . cache . get ( uriString , progress , token )
238+ : await this . getAst ( uriString , progress , token ) ;
211239
212240 const queryResults = Array . from ( completedQuery . results . values ( ) ) ;
213241 if ( queryResults . length !== 1 ) {
@@ -296,6 +324,11 @@ export class TemplatePrintCfgProvider {
296324 private dbm : DatabaseManager ,
297325 ) {
298326 this . cache = new CachedOperation ( this . getCfgUri . bind ( this ) ) ;
327+ this . dbm . onDidChangeDatabaseItem ( ( { kind } ) => {
328+ if ( kind === DatabaseEventKind . Remove ) {
329+ this . cache . reset ( ) ;
330+ }
331+ } ) ;
299332 }
300333
301334 async provideCfgUri (
@@ -369,3 +402,20 @@ export class TemplatePrintCfgProvider {
369402 return [ queryUri , templates ] ;
370403 }
371404}
405+
406+ async function maybeReimportTestDatabase (
407+ dbm : DatabaseManager ,
408+ uri : Uri ,
409+ ) : Promise < void > {
410+ if ( uri . scheme !== zipArchiveScheme ) {
411+ return ;
412+ }
413+ const zippedArchive = decodeSourceArchiveUri ( uri ) ;
414+ const sourceArchiveUri = encodeArchiveBasePath (
415+ zippedArchive . sourceArchiveZipPath ,
416+ ) ;
417+ const db = dbm . findDatabaseItemBySourceArchive ( sourceArchiveUri ) ;
418+ if ( db ) {
419+ await dbm . maybeReimportTestDatabase ( db . databaseUri ) ;
420+ }
421+ }
0 commit comments