From c082a38b6bd491ea4e054c24c3a94f69f85ab830 Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Thu, 1 Apr 2021 14:12:13 -0700 Subject: [PATCH] Add a canary setting to avoid caching AST viewer queries (#818) When codeql library developers are working on PrintAST queries, it is not easy to use the AST Viewer. The AST Viewer caches results so that multiple calls to view the AST of the same file are nearly instantaneous. However, this breaks down if you are changing the actual queries that perform AST viewing. In this case, you do not want the cache to be active. This commit adds an undocumented setting that prevents caching. To enable, set: ``` "codeQL.isCanary": true, "codeQL.astViewer.disableCache": true ``` Note that *both* settings must be true for this to work. This behaviour and all canary behaviour should be documented somewhere. I will add that later. --- extensions/ql-vscode/src/config.ts | 6 ++++++ extensions/ql-vscode/src/contextual/templateProvider.ts | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/extensions/ql-vscode/src/config.ts b/extensions/ql-vscode/src/config.ts index 598c3f6f8..9621a9c72 100644 --- a/extensions/ql-vscode/src/config.ts +++ b/extensions/ql-vscode/src/config.ts @@ -41,6 +41,7 @@ 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 GLOBAL_TELEMETRY_SETTING = new Setting('telemetry'); export const LOG_TELEMETRY = new Setting('logTelemetry', TELEMETRY_SETTING); @@ -279,3 +280,8 @@ export const CANARY_FEATURES = new Setting('canary', ROOT_SETTING); export function isCanary() { return !!CANARY_FEATURES.getValue(); } + +/** + * 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); diff --git a/extensions/ql-vscode/src/contextual/templateProvider.ts b/extensions/ql-vscode/src/contextual/templateProvider.ts index b90a4a0cc..41302ddf6 100644 --- a/extensions/ql-vscode/src/contextual/templateProvider.ts +++ b/extensions/ql-vscode/src/contextual/templateProvider.ts @@ -25,6 +25,7 @@ import { } from './keyType'; import { FullLocationLink, getLocationsForUriString, TEMPLATE_NAME } from './locationFinder'; import { qlpackOfDatabase, resolveQueries } from './queryResolver'; +import { isCanary, NO_CACHE_AST_VIEWER } from '../config'; /** * Run templated CodeQL queries to find definitions and references in @@ -141,7 +142,9 @@ export class TemplatePrintAstProvider { if (!document) { throw new Error('Cannot view the AST. Please select a valid source file inside a CodeQL database.'); } - const queryResults = await this.cache.get(document.uri.toString(), progress, token); + const queryResults = this.shouldCache() + ? await this.cache.get(document.uri.toString(), progress, token) + : await this.getAst(document.uri.toString(), progress, token); return new AstBuilder( queryResults, this.cli, @@ -150,6 +153,10 @@ export class TemplatePrintAstProvider { ); } + private shouldCache() { + return !(isCanary() && NO_CACHE_AST_VIEWER.getValue()); + } + private async getAst( uriString: string, progress: ProgressCallback,