From 5ce45761a0d58730161eb44d8d5a4a154d71ca59 Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Thu, 27 Apr 2023 21:00:39 +0000 Subject: [PATCH] Add `codeQL.contextualQueries.disableCache` An internal option to help library authours to run and debug the find references and find dependencies contetextual queries without relying on the implicit cache. --- extensions/ql-vscode/src/config.ts | 14 ++++++- .../contextual/template-provider.ts | 37 ++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/extensions/ql-vscode/src/config.ts b/extensions/ql-vscode/src/config.ts index dc9222c07..c807e818d 100644 --- a/extensions/ql-vscode/src/config.ts +++ b/extensions/ql-vscode/src/config.ts @@ -69,6 +69,10 @@ const ROOT_SETTING = new Setting("codeQL"); // Global configuration const TELEMETRY_SETTING = new Setting("telemetry", ROOT_SETTING); const AST_VIEWER_SETTING = new Setting("astViewer", ROOT_SETTING); +const CONTEXTUAL_QUERIES_SETTINGS = new Setting( + "contextualQueries", + ROOT_SETTING, +); const GLOBAL_TELEMETRY_SETTING = new Setting("telemetry"); const LOG_INSIGHTS_SETTING = new Setting("logInsights", ROOT_SETTING); @@ -479,13 +483,21 @@ export function joinOrderWarningThreshold(): number { } /** - * Avoids caching in the AST viewer if the user is also a canary user. + * Hidden setting: Avoids caching in the AST viewer if the user is also a canary user. */ export const NO_CACHE_AST_VIEWER = new Setting( "disableCache", AST_VIEWER_SETTING, ); +/** + * Hidden setting: Avoids caching in jump to def and find refs contextual queries if the user is also a canary user. + */ +export const NO_CACHE_CONTEXTUAL_QUERIES = new Setting( + "disableCache", + CONTEXTUAL_QUERIES_SETTINGS, +); + // Settings for variant analysis const VARIANT_ANALYSIS_SETTING = new Setting("variantAnalysis", ROOT_SETTING); diff --git a/extensions/ql-vscode/src/language-support/contextual/template-provider.ts b/extensions/ql-vscode/src/language-support/contextual/template-provider.ts index dc7ea86ca..48f675c6f 100644 --- a/extensions/ql-vscode/src/language-support/contextual/template-provider.ts +++ b/extensions/ql-vscode/src/language-support/contextual/template-provider.ts @@ -30,7 +30,11 @@ import { resolveQueries, runContextualQuery, } from "./query-resolver"; -import { isCanary, NO_CACHE_AST_VIEWER } from "../../config"; +import { + isCanary, + NO_CACHE_AST_VIEWER, + NO_CACHE_CONTEXTUAL_QUERIES, +} from "../../config"; import { CoreCompletedQuery, QueryRunner } from "../../query-server"; import { AstBuilder } from "../ast-viewer/ast-builder"; @@ -59,7 +63,10 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider { position: Position, _token: CancellationToken, ): Promise { - const fileLinks = await this.cache.get(document.uri.toString()); + const fileLinks = this.shouldUseCache() + ? await this.cache.get(document.uri.toString()) + : await this.getDefinitions(document.uri.toString()); + const locLinks: LocationLink[] = []; for (const link of fileLinks) { if (link.originSelectionRange!.contains(position)) { @@ -69,6 +76,10 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider { return locLinks; } + private shouldUseCache() { + return !(isCanary() && NO_CACHE_CONTEXTUAL_QUERIES.getValue()); + } + private async getDefinitions(uriString: string): Promise { return withProgress( async (progress, token) => { @@ -118,7 +129,10 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider { _context: ReferenceContext, _token: CancellationToken, ): Promise { - const fileLinks = await this.cache.get(document.uri.toString()); + const fileLinks = this.shouldUseCache() + ? await this.cache.get(document.uri.toString()) + : await this.getReferences(document.uri.toString()); + const locLinks: Location[] = []; for (const link of fileLinks) { if (link.targetRange!.contains(position)) { @@ -131,6 +145,10 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider { return locLinks; } + private shouldUseCache() { + return !(isCanary() && NO_CACHE_CONTEXTUAL_QUERIES.getValue()); + } + private async getReferences(uriString: string): Promise { return withProgress( async (progress, token) => { @@ -182,7 +200,7 @@ export class TemplatePrintAstProvider { "Cannot view the AST. Please select a valid source file inside a CodeQL database.", ); } - const completedQuery = this.shouldCache() + const completedQuery = this.shouldUseCache() ? await this.cache.get(fileUri.toString(), progress, token) : await this.getAst(fileUri.toString(), progress, token); @@ -194,7 +212,7 @@ export class TemplatePrintAstProvider { ); } - private shouldCache() { + private shouldUseCache() { return !(isCanary() && NO_CACHE_AST_VIEWER.getValue()); } @@ -271,7 +289,14 @@ export class TemplatePrintCfgProvider { if (!document) { return; } - return await this.cache.get(document.uri.toString()); + + return this.shouldUseCache() + ? await this.cache.get(document.uri.toString()) + : await this.getCfgUri(document.uri.toString()); + } + + private shouldUseCache() { + return !(isCanary() && NO_CACHE_AST_VIEWER.getValue()); } private async getCfgUri(