-
Notifications
You must be signed in to change notification settings - Fork 189
/
local-queries.ts
590 lines (548 loc) · 18.4 KB
/
local-queries.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import {
ProgressCallback,
ProgressUpdate,
withProgress,
} from "../common/vscode/progress";
import {
CancellationToken,
CancellationTokenSource,
QuickPickItem,
Range,
TabInputText,
Uri,
window,
} from "vscode";
import {
TeeLogger,
showAndLogErrorMessage,
showAndLogWarningMessage,
} from "../common/logging";
import { isCanary, MAX_QUERIES } from "../config";
import { gatherQlFiles } from "../common/files";
import { basename } from "path";
import { showBinaryChoiceDialog } from "../common/vscode/dialog";
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
import { displayQuickQuery } from "./quick-query";
import { CoreCompletedQuery, QueryRunner } from "../query-server";
import { QueryHistoryManager } from "../query-history/query-history-manager";
import { DatabaseUI } from "../databases/local-databases-ui";
import { ResultsView } from "./results-view";
import { DatabaseItem, DatabaseManager } from "../databases/local-databases";
import {
createInitialQueryInfo,
createTimestampFile,
getQuickEvalContext,
QueryOutputDir,
saveBeforeStart,
SelectedQuery,
validateQueryUri,
} from "../run-queries-shared";
import { CompletedLocalQueryInfo, LocalQueryInfo } from "../query-results";
import { WebviewReveal } from "./webview";
import { asError, getErrorMessage } from "../common/helpers-pure";
import { CliVersionConstraint, CodeQLCliServer } from "../codeql-cli/cli";
import { LocalQueryCommands } from "../common/commands";
import { App } from "../common/app";
import { DisposableObject } from "../common/disposable-object";
import { SkeletonQueryWizard } from "./skeleton-query-wizard";
import { LocalQueryRun } from "./local-query-run";
import { createMultiSelectionCommand } from "../common/vscode/selection-commands";
import { findLanguage } from "../codeql-cli/query-language";
import type { QueryTreeViewItem } from "../queries-panel/query-tree-view-item";
import { tryGetQueryLanguage } from "../common/query-language";
interface DatabaseQuickPickItem extends QuickPickItem {
databaseItem: DatabaseItem;
}
export enum QuickEvalType {
None,
QuickEval,
QuickEvalCount,
}
export class LocalQueries extends DisposableObject {
public constructor(
private readonly app: App,
private readonly queryRunner: QueryRunner,
private readonly queryHistoryManager: QueryHistoryManager,
private readonly databaseManager: DatabaseManager,
private readonly cliServer: CodeQLCliServer,
private readonly databaseUI: DatabaseUI,
private readonly localQueryResultsView: ResultsView,
private readonly queryStorageDir: string,
) {
super();
}
public getCommands(): LocalQueryCommands {
return {
"codeQL.runQuery": this.runQuery.bind(this),
"codeQL.runQueryContextEditor": this.runQuery.bind(this),
"codeQL.runQueryOnMultipleDatabases":
this.runQueryOnMultipleDatabases.bind(this),
"codeQL.runQueryOnMultipleDatabasesContextEditor":
this.runQueryOnMultipleDatabases.bind(this),
"codeQLQueries.runLocalQueryFromQueriesPanel":
this.runQueryFromQueriesPanel.bind(this),
"codeQLQueries.runLocalQueryContextMenu":
this.runQueryFromQueriesPanel.bind(this),
"codeQLQueries.runLocalQueriesContextMenu":
this.runQueriesFromQueriesPanel.bind(this),
"codeQLQueries.runLocalQueriesFromPanel":
this.runQueriesFromQueriesPanel.bind(this),
"codeQLQueries.createQuery": this.createSkeletonQuery.bind(this),
"codeQL.runLocalQueryFromFileTab": this.runQuery.bind(this),
"codeQL.runQueries": createMultiSelectionCommand(
this.runQueries.bind(this),
),
"codeQL.quickEval": this.quickEval.bind(this),
"codeQL.quickEvalCount": this.quickEvalCount.bind(this),
"codeQL.quickEvalContextEditor": this.quickEval.bind(this),
"codeQL.codeLensQuickEval": this.codeLensQuickEval.bind(this),
"codeQL.quickQuery": this.quickQuery.bind(this),
"codeQL.getCurrentQuery": () => {
// When invoked as a command, such as when resolving variables in a debug configuration,
// always allow ".qll" files, because we don't know if the configuration will be for
// quickeval yet. The debug configuration code will do further validation once it knows for
// sure.
return this.getCurrentQuery(true);
},
"codeQL.createQuery": this.createSkeletonQuery.bind(this),
};
}
private async runQueryFromQueriesPanel(
queryTreeViewItem: QueryTreeViewItem,
): Promise<void> {
if (queryTreeViewItem.path !== undefined) {
await this.runQuery(Uri.file(queryTreeViewItem.path));
}
}
private async runQueriesFromQueriesPanel(
queryTreeViewItem: QueryTreeViewItem,
): Promise<void> {
const uris = [];
for (const child of queryTreeViewItem.children) {
if (child.path !== undefined) {
uris.push(Uri.file(child.path));
}
}
await this.runQueries(uris);
}
private async runQuery(uri: Uri | undefined): Promise<void> {
await withProgress(
async (progress, token) => {
await this.compileAndRunQuery(
QuickEvalType.None,
uri,
progress,
token,
undefined,
);
},
{
title: "Running query",
cancellable: true,
},
);
}
private async runQueryOnMultipleDatabases(
uri: Uri | undefined,
): Promise<void> {
await withProgress(
async (progress, token) =>
await this.compileAndRunQueryOnMultipleDatabases(progress, token, uri),
{
title: "Running query on selected databases",
cancellable: true,
},
);
}
private async runQueries(fileURIs: Uri[]): Promise<void> {
await withProgress(
async (progress, token) => {
const maxQueryCount = MAX_QUERIES.getValue() as number;
const [files, dirFound] = await gatherQlFiles(
fileURIs.map((uri) => uri.fsPath),
);
if (files.length > maxQueryCount) {
throw new Error(
`You tried to run ${files.length} queries, but the maximum is ${maxQueryCount}. Try selecting fewer queries or changing the 'codeQL.runningQueries.maxQueries' setting.`,
);
}
// warn user and display selected files when a directory is selected because some ql
// files may be hidden from the user.
if (dirFound) {
const fileString = files.map((file) => basename(file)).join(", ");
const res = await showBinaryChoiceDialog(
`You are about to run ${files.length} queries: ${fileString} Do you want to continue?`,
);
if (!res) {
return;
}
}
const queryUris = files.map((path) => Uri.parse(`file:${path}`, true));
// Use a wrapped progress so that messages appear with the queries remaining in it.
let queriesRemaining = queryUris.length;
function wrappedProgress(update: ProgressUpdate) {
const message =
queriesRemaining > 1
? `${queriesRemaining} remaining. ${update.message}`
: update.message;
progress({
...update,
message,
});
}
wrappedProgress({
maxStep: queryUris.length,
step: queryUris.length - queriesRemaining,
message: "",
});
await Promise.all(
queryUris.map(async (uri) =>
this.compileAndRunQuery(
QuickEvalType.None,
uri,
wrappedProgress,
token,
undefined,
).then(() => queriesRemaining--),
),
);
},
{
title: "Running queries",
cancellable: true,
},
);
}
private async quickEval(uri: Uri): Promise<void> {
await withProgress(
async (progress, token) => {
await this.compileAndRunQuery(
QuickEvalType.QuickEval,
uri,
progress,
token,
undefined,
);
},
{
title: "Running query",
cancellable: true,
},
);
}
private async quickEvalCount(uri: Uri): Promise<void> {
await withProgress(
async (progress, token) => {
if (!(await this.cliServer.cliConstraints.supportsQuickEvalCount())) {
throw new Error(
`Quick evaluation count is only supported by CodeQL CLI v${CliVersionConstraint.CLI_VERSION_WITH_QUICK_EVAL_COUNT} or later.`,
);
}
await this.compileAndRunQuery(
QuickEvalType.QuickEvalCount,
uri,
progress,
token,
undefined,
);
},
{
title: "Running query",
cancellable: true,
},
);
}
private async codeLensQuickEval(uri: Uri, range: Range): Promise<void> {
await withProgress(
async (progress, token) =>
await this.compileAndRunQuery(
QuickEvalType.QuickEval,
uri,
progress,
token,
undefined,
range,
),
{
title: "Running query",
cancellable: true,
},
);
}
private async quickQuery(): Promise<void> {
await withProgress(
async (progress, token) =>
displayQuickQuery(
this.app,
this.cliServer,
this.databaseUI,
progress,
token,
),
{
title: "Run Quick Query",
},
);
}
/**
* Gets the current active query. This is the query that is open in the active tab.
*/
public async getCurrentQuery(allowLibraryFiles: boolean): Promise<string> {
const input = window.tabGroups.activeTabGroup.activeTab?.input;
if (input === undefined || !isTabInputText(input)) {
throw new Error(
"No query was selected. Please select a query and try again.",
);
}
return validateQueryUri(input.uri, allowLibraryFiles);
}
private async createSkeletonQuery(): Promise<void> {
await withProgress(
async (progress: ProgressCallback) => {
const credentials = isCanary() ? this.app.credentials : undefined;
const contextStoragePath =
this.app.workspaceStoragePath || this.app.globalStoragePath;
const skeletonQueryWizard = new SkeletonQueryWizard(
this.cliServer,
progress,
credentials,
this.app.logger,
this.databaseManager,
contextStoragePath,
);
await skeletonQueryWizard.execute();
},
{
title: "Create Query",
},
);
}
/**
* Creates a new `LocalQueryRun` object to track a query evaluation. This creates a timestamp
* file in the query's output directory, creates a `LocalQueryInfo` object, and registers that
* object with the query history manager.
*
* Once the evaluation is complete, the client must call `complete()` on the `LocalQueryRun`
* object to update the UI based on the results of the query.
*/
public async createLocalQueryRun(
selectedQuery: SelectedQuery,
dbItem: DatabaseItem,
outputDir: QueryOutputDir,
tokenSource: CancellationTokenSource,
): Promise<LocalQueryRun> {
await createTimestampFile(outputDir.querySaveDir);
if (this.queryRunner.customLogDirectory) {
void showAndLogWarningMessage(
this.app.logger,
`Custom log directories are no longer supported. The "codeQL.runningQueries.customLogDirectory" setting is deprecated. Unset the setting to stop seeing this message. Query logs saved to ${outputDir.logPath}`,
);
}
const initialInfo = await createInitialQueryInfo(selectedQuery, {
databaseUri: dbItem.databaseUri.toString(),
name: dbItem.name,
language: tryGetQueryLanguage(dbItem.language),
});
// When cancellation is requested from the query history view, we just stop the debug session.
const queryInfo = new LocalQueryInfo(initialInfo, tokenSource);
this.queryHistoryManager.addQuery(queryInfo);
const logger = new TeeLogger(this.queryRunner.logger, outputDir.logPath);
return new LocalQueryRun(
outputDir,
this,
queryInfo,
dbItem,
logger,
this.queryHistoryManager,
this.cliServer,
);
}
public async compileAndRunQuery(
quickEval: QuickEvalType,
queryUri: Uri | undefined,
progress: ProgressCallback,
token: CancellationToken,
databaseItem: DatabaseItem | undefined,
range?: Range,
templates?: Record<string, string>,
): Promise<void> {
await this.compileAndRunQueryInternal(
quickEval,
queryUri,
progress,
token,
databaseItem,
range,
templates,
);
}
/** Used by tests */
public async compileAndRunQueryInternal(
quickEval: QuickEvalType,
queryUri: Uri | undefined,
progress: ProgressCallback,
token: CancellationToken,
databaseItem: DatabaseItem | undefined,
range?: Range,
templates?: Record<string, string>,
): Promise<CoreCompletedQuery> {
await saveBeforeStart();
let queryPath: string;
if (queryUri !== undefined) {
// The query URI is provided by the command, most likely because the command was run from an
// editor context menu. Use the provided URI, but make sure it's a valid query.
queryPath = validateQueryUri(queryUri, quickEval !== QuickEvalType.None);
} else {
// Use the currently selected query.
queryPath = await this.getCurrentQuery(quickEval !== QuickEvalType.None);
}
const selectedQuery: SelectedQuery = {
queryPath,
quickEval: quickEval
? await getQuickEvalContext(
range,
quickEval === QuickEvalType.QuickEvalCount,
)
: undefined,
};
// If no databaseItem is specified, use the database currently selected in the Databases UI
databaseItem =
databaseItem ?? (await this.databaseUI.getDatabaseItem(progress, token));
if (databaseItem === undefined) {
throw new Error("Can't run query without a selected database");
}
const additionalPacks = getOnDiskWorkspaceFolders();
const extensionPacks = await this.getDefaultExtensionPacks(additionalPacks);
const coreQueryRun = this.queryRunner.createQueryRun(
databaseItem.databaseUri.fsPath,
{
queryPath: selectedQuery.queryPath,
quickEvalPosition: selectedQuery.quickEval?.quickEvalPosition,
quickEvalCountOnly: selectedQuery.quickEval?.quickEvalCount,
},
true,
additionalPacks,
extensionPacks,
{},
this.queryStorageDir,
undefined,
templates,
);
// handle cancellation from the history view.
const source = new CancellationTokenSource();
try {
token.onCancellationRequested(() => source.cancel());
const localQueryRun = await this.createLocalQueryRun(
selectedQuery,
databaseItem,
coreQueryRun.outputDir,
source,
);
try {
const results = await coreQueryRun.evaluate(
progress,
source.token,
localQueryRun.logger,
);
await localQueryRun.complete(results);
return results;
} catch (e) {
// It's odd that we have two different ways for a query evaluation to fail: by throwing an
// exception, and by returning a result with a failure code. This is how the code worked
// before the refactoring, so it's been preserved, but we should probably figure out how
// to unify both error handling paths.
const err = asError(e);
await localQueryRun.fail(err);
throw e;
}
} finally {
source.dispose();
}
}
private async compileAndRunQueryOnMultipleDatabases(
progress: ProgressCallback,
token: CancellationToken,
uri: Uri | undefined,
): Promise<void> {
let filteredDBs = this.databaseManager.databaseItems;
if (filteredDBs.length === 0) {
void showAndLogErrorMessage(
this.app.logger,
"No databases found. Please add a suitable database to your workspace.",
);
return;
}
// If possible, only show databases with the right language (otherwise show all databases).
const queryLanguage = await findLanguage(this.cliServer, uri);
if (queryLanguage) {
filteredDBs = this.databaseManager.databaseItems.filter(
(db) => db.language === queryLanguage,
);
if (filteredDBs.length === 0) {
void showAndLogErrorMessage(
this.app.logger,
`No databases found for language ${queryLanguage}. Please add a suitable database to your workspace.`,
);
return;
}
}
const quickPickItems = filteredDBs.map<DatabaseQuickPickItem>((dbItem) => ({
databaseItem: dbItem,
label: dbItem.name,
description: dbItem.language,
}));
/**
* Databases that were selected in the quick pick menu.
*/
const quickpick = await window.showQuickPick<DatabaseQuickPickItem>(
quickPickItems,
{ canPickMany: true, ignoreFocusOut: true },
);
if (quickpick !== undefined) {
// Collect all skipped databases and display them at the end (instead of popping up individual errors)
const skippedDatabases = [];
const errors = [];
for (const item of quickpick) {
try {
await this.compileAndRunQuery(
QuickEvalType.None,
uri,
progress,
token,
item.databaseItem,
);
} catch (e) {
skippedDatabases.push(item.label);
errors.push(getErrorMessage(e));
}
}
if (skippedDatabases.length > 0) {
void this.app.logger.log(`Errors:\n${errors.join("\n")}`);
void showAndLogWarningMessage(
this.app.logger,
`The following databases were skipped:\n${skippedDatabases.join(
"\n",
)}.\nFor details about the errors, see the logs.`,
);
}
} else {
void showAndLogErrorMessage(this.app.logger, "No databases selected.");
}
}
public async showResultsForCompletedQuery(
query: CompletedLocalQueryInfo,
forceReveal: WebviewReveal,
): Promise<void> {
await this.localQueryResultsView.showResults(query, forceReveal, false);
}
public async getDefaultExtensionPacks(
additionalPacks: string[],
): Promise<string[]> {
return (await this.cliServer.useExtensionPacks())
? Object.keys(await this.cliServer.resolveQlpacks(additionalPacks, true))
: [];
}
}
function isTabInputText(input: any): input is TabInputText {
return input?.uri !== undefined;
}